diff options
author | 2017-09-22 11:57:16 -0400 | |
---|---|---|
committer | 2017-09-25 09:37:26 -0400 | |
commit | 0d00f700c71718043942d211ee73dcefe19d0854 (patch) | |
tree | 012e32120baf4b75bfd3abf974817e9670cbecbc | |
parent | e8d3c223185a8e1c0b659a5c70d2987460460d06 (diff) |
Create AndroidManifest.xml symlink if user provides a manifest not named
"AndroidManifest.xml".
aapt requires the manifest to be named "AndroidManifest.xml" (unknown commit)
This allows users to provide manifests with custom names.
RELNOTES: Android rules no longer restrict the manifest file to be named "AndroidManifest.xml".
PiperOrigin-RevId: 169695062
4 files changed, 46 insertions, 6 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/BazelAndroidSemantics.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/BazelAndroidSemantics.java index 84ed3a141f..ac6a6bc7f3 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/android/BazelAndroidSemantics.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/android/BazelAndroidSemantics.java @@ -18,11 +18,13 @@ import com.google.devtools.build.lib.actions.Artifact; import com.google.devtools.build.lib.analysis.RuleContext; import com.google.devtools.build.lib.analysis.actions.CustomCommandLine; import com.google.devtools.build.lib.analysis.actions.SpawnAction; +import com.google.devtools.build.lib.analysis.actions.SymlinkAction; import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget.Mode; import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException; import com.google.devtools.build.lib.rules.android.AndroidCommon; import com.google.devtools.build.lib.rules.android.AndroidConfiguration; import com.google.devtools.build.lib.rules.android.AndroidIdeInfoProvider; +import com.google.devtools.build.lib.rules.android.AndroidRuleClasses; import com.google.devtools.build.lib.rules.android.AndroidSemantics; import com.google.devtools.build.lib.rules.android.ApplicationManifest; import com.google.devtools.build.lib.rules.android.ResourceApk; @@ -46,14 +48,25 @@ public class BazelAndroidSemantics implements AndroidSemantics { AndroidIdeInfoProvider.Builder ideInfoProviderBuilder) {} @Override - public ApplicationManifest getManifestForRule(RuleContext ruleContext) throws RuleErrorException { + public ApplicationManifest getManifestForRule(RuleContext ruleContext) + throws RuleErrorException, InterruptedException { ApplicationManifest result = ApplicationManifest.fromRule(ruleContext); - if (!result.getManifest().getExecPath().getBaseName().equals("AndroidManifest.xml")) { - ruleContext.attributeError("manifest", "The manifest must be called 'AndroidManifest.xml'"); - throw new RuleErrorException(); + Artifact manifest = result.getManifest(); + if (manifest.getFilename().equals("AndroidManifest.xml")) { + return result; + } else { + /** + * If the manifest file is not named AndroidManifest.xml, we create a symlink named + * AndroidManifest.xml to it. aapt requires the manifest to be named as such. + */ + Artifact manifestSymlink = + ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_SYMLINKED_MANIFEST); + SymlinkAction symlinkAction = + new SymlinkAction( + ruleContext.getActionOwner(), manifest, manifestSymlink, "Renaming Android manifest"); + ruleContext.registerAction(symlinkAction); + return ApplicationManifest.fromExplicitManifest(ruleContext, manifestSymlink); } - - return result; } @Override diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidRuleClasses.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidRuleClasses.java index 763a134cba..93cf9eab34 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidRuleClasses.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidRuleClasses.java @@ -137,6 +137,8 @@ public final class AndroidRuleClasses { fromTemplates("%{name}_symbols/merged.bin"); public static final SafeImplicitOutputsFunction ANDROID_COMPILED_SYMBOLS = fromTemplates("%{name}_symbols/symbols.zip"); + public static final SafeImplicitOutputsFunction ANDROID_SYMLINKED_MANIFEST = + fromTemplates("%{name}_symlinked_manifest/AndroidManifest.xml"); public static final ImplicitOutputsFunction ANDROID_PROCESSED_MANIFEST = fromTemplates("%{name}_processed_manifest/AndroidManifest.xml"); public static final SafeImplicitOutputsFunction MOBILE_INSTALL_STUB_APPLICATION_MANIFEST = diff --git a/src/test/java/com/google/devtools/build/lib/rules/android/AndroidBinaryTest.java b/src/test/java/com/google/devtools/build/lib/rules/android/AndroidBinaryTest.java index 686ddc1a66..22ec0170cc 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/android/AndroidBinaryTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/android/AndroidBinaryTest.java @@ -142,6 +142,18 @@ public class AndroidBinaryTest extends AndroidBuildViewTestCase { } @Test + public void testAndroidManifestWithCustomName() throws Exception { + scratchConfiguredTarget( + "java/a", + "a", + "android_binary(", + " name = 'a',", + " srcs = ['A.java'],", + " manifest = 'SomeOtherAndroidManifest.xml')"); + assertNoEvents(); + } + + @Test public void testMainDexProguardSpecs() throws Exception { useConfiguration("--noincremental_dexing"); ConfiguredTarget ct = scratchConfiguredTarget("java/a", "a", diff --git a/src/test/shell/bazel/android/android_integration_test.sh b/src/test/shell/bazel/android/android_integration_test.sh index 29a0da060d..dc35d87eda 100755 --- a/src/test/shell/bazel/android/android_integration_test.sh +++ b/src/test/shell/bazel/android/android_integration_test.sh @@ -186,6 +186,19 @@ function test_android_sdk_repository_returns_null_if_env_vars_missing() { ANDROID_HOME=$ANDROID_SDK bazel build @androidsdk//:files || "Build failed" } +function test_allow_custom_manifest_name() { + create_new_workspace + setup_android_sdk_support + create_android_binary + mv java/bazel/AndroidManifest.xml java/bazel/SomeOtherName.xml + + # macOS requires an argument for the backup file extension. + sed -i'' -e 's/AndroidManifest/SomeOtherName/' java/bazel/BUILD + + bazel build //java/bazel:bin || fail "Build failed" \ + "Failed to build android_binary with custom Android manifest file name" +} + if [[ ! -d "${TEST_SRCDIR}/androidsdk" ]]; then echo "Not running Android tests due to lack of an Android SDK." exit 0 |