diff options
5 files changed, 18 insertions, 569 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidBinary.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidBinary.java index e66fca7247..5cede5999f 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidBinary.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidBinary.java @@ -192,8 +192,6 @@ public abstract class AndroidBinary implements RuleConfiguredTargetFactory { resourceDeps, ApplicationManifest.getManifestValues(ruleContext), ApplicationManifest.useLegacyMerging(ruleContext)); - final ApplicationManifest applicationManifest = - ApplicationManifest.fromExplicitManifest(ruleContext, manifest.getManifest()); AndroidAaptVersion aaptVersion = AndroidAaptVersion.chooseTargetAaptVersion(ruleContext); final ResourceApk resourceApk = @@ -203,7 +201,7 @@ public abstract class AndroidBinary implements RuleConfiguredTargetFactory { manifest, /* conditionalKeepRules = */ shouldShrinkResourceCycles( dataContext.getAndroidConfig(), ruleContext, shrinkResources), - applicationManifest.getManifestValues(), + ApplicationManifest.getManifestValues(ruleContext), aaptVersion, AndroidResources.from(ruleContext, "resource_files"), AndroidAssets.from(ruleContext), @@ -301,7 +299,7 @@ public abstract class AndroidBinary implements RuleConfiguredTargetFactory { MobileInstallResourceApks mobileInstallResourceApks = AndroidBinaryMobileInstall.createMobileInstallResourceApks( - ruleContext, dataContext, applicationManifest); + ruleContext, dataContext, manifest); return createAndroidBinary( ruleContext, @@ -314,7 +312,6 @@ public abstract class AndroidBinary implements RuleConfiguredTargetFactory { javaSemantics, androidSemantics, nativeLibs, - applicationManifest, resourceApk, mobileInstallResourceApks, shrinkResources, @@ -337,7 +334,6 @@ public abstract class AndroidBinary implements RuleConfiguredTargetFactory { JavaSemantics javaSemantics, AndroidSemantics androidSemantics, NativeLibs nativeLibs, - ApplicationManifest applicationManifest, ResourceApk resourceApk, @Nullable MobileInstallResourceApks mobileInstallResourceApks, boolean shrinkResources, @@ -354,7 +350,7 @@ public abstract class AndroidBinary implements RuleConfiguredTargetFactory { dataContext, androidSemantics, resourceApk.getResourceProguardConfig(), - applicationManifest.getManifest(), + resourceApk.getManifest(), ruleContext.attributes().has(ProguardHelper.PROGUARD_SPECS, BuildType.LABEL_LIST) ? ruleContext .getPrerequisiteArtifacts(ProguardHelper.PROGUARD_SPECS, Mode.TARGET) @@ -526,7 +522,7 @@ public abstract class AndroidBinary implements RuleConfiguredTargetFactory { FilesToRunProvider checker = ruleContext.getExecutablePrerequisite("$instrumentation_test_check", Mode.HOST); Artifact targetManifest = targetApkProvider.getMergedManifest(); - Artifact instrumentationManifest = applicationManifest.getManifest(); + Artifact instrumentationManifest = resourceApk.getManifest(); Artifact checkOutput = ruleContext.getImplicitOutputArtifact( AndroidRuleClasses.INSTRUMENTATION_TEST_CHECK_RESULTS); @@ -585,8 +581,7 @@ public abstract class AndroidBinary implements RuleConfiguredTargetFactory { resourceExtractor, nativeLibsAar, signingKey, - additionalMergedManifests, - applicationManifest); + additionalMergedManifests); } return builder @@ -608,7 +603,7 @@ public abstract class AndroidBinary implements RuleConfiguredTargetFactory { zipAlignedApk, unsignedApk, androidCommon.getInstrumentedJar(), - applicationManifest.getManifest(), + resourceApk.getManifest(), AndroidCommon.getApkDebugSigningKey(ruleContext))) .addNativeDeclaredProvider(new AndroidPreDexJarProvider(jarToDex)) .addNativeDeclaredProvider( diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidBinaryMobileInstall.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidBinaryMobileInstall.java index 348698f062..29529bbfc0 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidBinaryMobileInstall.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidBinaryMobileInstall.java @@ -56,16 +56,13 @@ public final class AndroidBinaryMobileInstall { } static MobileInstallResourceApks createMobileInstallResourceApks( - RuleContext ruleContext, - AndroidDataContext dataContext, - ApplicationManifest applicationManifest) + RuleContext ruleContext, AndroidDataContext dataContext, StampedAndroidManifest manifest) throws RuleErrorException, InterruptedException { final ResourceApk incrementalResourceApk; final ResourceApk splitResourceApk; - StampedAndroidManifest manifest = - new StampedAndroidManifest( - applicationManifest.getManifest(), /* pkg = */ null, /* exported = */ true); + + Map<String, String> manifestValues = ApplicationManifest.getManifestValues(ruleContext); incrementalResourceApk = ProcessedAndroidData.processIncrementalBinaryDataFrom( @@ -76,7 +73,7 @@ public final class AndroidBinaryMobileInstall { AndroidRuleClasses.ANDROID_INCREMENTAL_RESOURCES_APK), getMobileInstallArtifact(ruleContext, "merged_incremental_resources.bin"), "incremental", - applicationManifest.getManifestValues()) + manifestValues) // Intentionally skip building an R class JAR - incremental binaries handle this // separately. .withValidatedResources(null); @@ -89,7 +86,7 @@ public final class AndroidBinaryMobileInstall { getMobileInstallArtifact(ruleContext, "android_resources.ap_"), getMobileInstallArtifact(ruleContext, "merged_split_resources.bin"), "incremental_split", - applicationManifest.getManifestValues()) + manifestValues) // Intentionally skip building an R class JAR - incremental binaries handle this // separately. .withValidatedResources(null); @@ -110,8 +107,7 @@ public final class AndroidBinaryMobileInstall { FilesToRunProvider resourceExtractor, NestedSet<Artifact> nativeLibsAar, Artifact signingKey, - ImmutableList<Artifact> additionalMergedManifests, - ApplicationManifest applicationManifest) + ImmutableList<Artifact> additionalMergedManifests) throws InterruptedException, RuleErrorException { Artifact incrementalApk = @@ -211,7 +207,7 @@ public final class AndroidBinaryMobileInstall { for (int i = 0; i < shardDexZips.size(); i++) { String splitName = "dex" + (i + 1); Artifact splitApkResources = - createSplitApkResources(ruleContext, applicationManifest, splitName, true); + createSplitApkResources(ruleContext, resourceApk.getProcessedManifest(), splitName, true); Artifact splitApk = getMobileInstallArtifact(ruleContext, splitName + ".apk"); ApkActionsBuilder.create("split dex apk " + (i + 1)) .setClassesDex(shardDexZips.get(i)) @@ -223,7 +219,7 @@ public final class AndroidBinaryMobileInstall { } Artifact nativeSplitApkResources = - createSplitApkResources(ruleContext, applicationManifest, "native", false); + createSplitApkResources(ruleContext, resourceApk.getProcessedManifest(), "native", false); Artifact nativeSplitApk = getMobileInstallArtifact(ruleContext, "native.apk"); ApkActionsBuilder.create("split native apk") .addInputZip(nativeSplitApkResources) @@ -234,7 +230,8 @@ public final class AndroidBinaryMobileInstall { splitApkSetBuilder.add(nativeSplitApk); Artifact javaSplitApkResources = - createSplitApkResources(ruleContext, applicationManifest, "java_resources", false); + createSplitApkResources( + ruleContext, resourceApk.getProcessedManifest(), "java_resources", false); Artifact javaSplitApk = getMobileInstallArtifact(ruleContext, "java_resources.apk"); ApkActionsBuilder.create("split Java resource apk") .addInputZip(javaSplitApkResources) @@ -471,7 +468,7 @@ public final class AndroidBinaryMobileInstall { private static Artifact createSplitApkResources( RuleContext ruleContext, - ApplicationManifest mainManifest, + ProcessedAndroidManifest mainManifest, String splitName, boolean hasCode) { Artifact splitManifest = diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidSemantics.java b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidSemantics.java index 3fee62ed6b..9e06e3750f 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/AndroidSemantics.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/AndroidSemantics.java @@ -35,17 +35,6 @@ import java.util.Optional; * to keep state. */ public interface AndroidSemantics { - /** - * Returns the manifest to be used when compiling a given rule. - * - * @throws InterruptedException - */ - default ApplicationManifest getManifestForRule(RuleContext ruleContext) - throws InterruptedException, RuleErrorException { - Artifact rawManifest = ApplicationManifest.getManifestFromAttributes(ruleContext); - return ApplicationManifest.fromExplicitManifest( - ruleContext, renameManifest(makeContextForNative(ruleContext), rawManifest)); - } default Artifact renameManifest(AndroidDataContext dataContext, Artifact rawManifest) throws InterruptedException { diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/ApplicationManifest.java b/src/main/java/com/google/devtools/build/lib/rules/android/ApplicationManifest.java index b0b08102d9..7268da113f 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/android/ApplicationManifest.java +++ b/src/main/java/com/google/devtools/build/lib/rules/android/ApplicationManifest.java @@ -17,7 +17,6 @@ import static com.google.common.base.Strings.isNullOrEmpty; import static com.google.devtools.build.lib.syntax.Type.STRING; import com.google.common.base.Joiner; -import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSortedMap; import com.google.devtools.build.lib.actions.Artifact; @@ -27,17 +26,11 @@ import com.google.devtools.build.lib.analysis.actions.CustomCommandLine; import com.google.devtools.build.lib.analysis.actions.FileWriteAction; 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.config.CompilationMode; import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget.Mode; import com.google.devtools.build.lib.cmdline.Label; -import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException; import com.google.devtools.build.lib.packages.RuleErrorConsumer; -import com.google.devtools.build.lib.rules.android.AndroidConfiguration.AndroidAaptVersion; import com.google.devtools.build.lib.rules.android.AndroidConfiguration.AndroidManifestMerger; -import com.google.devtools.build.lib.rules.android.DataBinding.DataBindingContext; -import com.google.devtools.build.lib.rules.android.ResourceContainer.Builder.JavaPackageSource; import com.google.devtools.build.lib.syntax.Type; -import java.util.List; import java.util.Map; import java.util.Optional; import java.util.TreeMap; @@ -46,12 +39,6 @@ import javax.annotation.Nullable; /** Represents a AndroidManifest, that may have been merged from dependencies. */ public final class ApplicationManifest { - public ApplicationManifest createSplitManifest( - RuleContext ruleContext, String splitName, boolean hasCode) { - Artifact result = createSplitManifest(ruleContext, manifest, splitName, hasCode); - return new ApplicationManifest(result, manifestValues, targetAaptVersion); - } - static Artifact createSplitManifest( RuleContext ruleContext, Artifact manifest, String splitName, boolean hasCode) { // aapt insists that manifests be called AndroidManifest.xml, even though they have to be @@ -87,12 +74,6 @@ public final class ApplicationManifest { return result; } - public ApplicationManifest addMobileInstallStubApplication(RuleContext ruleContext) - throws InterruptedException { - Artifact stubManifest = addMobileInstallStubApplication(ruleContext, manifest); - return new ApplicationManifest(stubManifest, manifestValues, targetAaptVersion); - } - static Artifact addMobileInstallStubApplication(RuleContext ruleContext, Artifact manifest) throws InterruptedException { @@ -133,21 +114,6 @@ public final class ApplicationManifest { return ruleContext.getPrerequisiteArtifact("manifest", Mode.TARGET); } - /** - * Gets the manifest specified in the "manifest" attribute, renaming it if needed. - * - * <p>Unlike {@link AndroidSemantics#getManifestForRule(RuleContext)}, this method will not - * perform AndroidSemantics-specific manifest processing. This method will do the same work - * regardless of the AndroidSemantics implementation being used; that method may do different work - * depending on the implementation. - */ - public static ApplicationManifest renamedFromRule( - RuleContext ruleContext, AndroidDataContext dataContext) - throws InterruptedException, RuleErrorException { - return fromExplicitManifest( - ruleContext, renameManifestIfNeeded(dataContext, getManifestFromAttributes(ruleContext))); - } - static Artifact renameManifestIfNeeded(AndroidDataContext dataContext, Artifact manifest) throws InterruptedException { if (manifest.getFilename().equals("AndroidManifest.xml")) { @@ -169,28 +135,6 @@ public final class ApplicationManifest { } } - public static ApplicationManifest fromExplicitManifest(RuleContext ruleContext, Artifact manifest) - throws RuleErrorException { - return new ApplicationManifest( - manifest, - getManifestValues(ruleContext), - AndroidAaptVersion.chooseTargetAaptVersion(ruleContext)); - } - - /** - * Generates an empty manifest for a rule that does not directly specify resources. - * - * <p><strong>Note:</strong> This generated manifest can then be used as the primary manifest when - * merging with dependencies. - * - * @return the generated ApplicationManifest - */ - public static ApplicationManifest generatedManifest(RuleContext ruleContext) - throws RuleErrorException { - return fromExplicitManifest( - ruleContext, generateManifest(ruleContext, AndroidCommon.getJavaPackage(ruleContext))); - } - /** * Creates an action to generate an empty manifest file with a specific package name. * @@ -229,39 +173,7 @@ public final class ApplicationManifest { return ImmutableMap.copyOf(manifestValues); } - public ImmutableMap<String, String> getManifestValues() { - return manifestValues; - } - - private final Artifact manifest; - private final ImmutableMap<String, String> manifestValues; - private final AndroidAaptVersion targetAaptVersion; - - private ApplicationManifest( - Artifact manifest, - ImmutableMap<String, String> manifestValues, - AndroidAaptVersion targetAaptVersion) { - this.manifest = manifest; - this.manifestValues = manifestValues; - this.targetAaptVersion = targetAaptVersion; - } - - public ApplicationManifest mergeWith( - RuleContext ruleContext, - AndroidDataContext dataContext, - AndroidSemantics androidSemantics, - ResourceDependencies resourceDeps) { - return maybeMergeWith( - dataContext, - androidSemantics, - manifest, - resourceDeps, - manifestValues, - useLegacyMerging(ruleContext), - AndroidCommon.getJavaPackage(ruleContext)) - .map(merged -> new ApplicationManifest(merged, manifestValues, targetAaptVersion)) - .orElse(this); - } + private ApplicationManifest() {} static Optional<Artifact> maybeMergeWith( AndroidDataContext dataContext, @@ -341,16 +253,6 @@ public final class ApplicationManifest { return builder.build(); } - public ApplicationManifest renamePackage(AndroidDataContext dataContext, String customPackage) { - Optional<Artifact> stamped = maybeSetManifestPackage(dataContext, manifest, customPackage); - - if (!stamped.isPresent()) { - return this; - } - - return new ApplicationManifest(stamped.get(), manifestValues, targetAaptVersion); - } - static Optional<Artifact> maybeSetManifestPackage( AndroidDataContext dataContext, Artifact manifest, String customPackage) { if (isNullOrEmpty(customPackage)) { @@ -367,408 +269,4 @@ public final class ApplicationManifest { return Optional.of(outputManifest); } - - public ResourceApk packTestWithDataAndResources( - RuleContext ruleContext, - AndroidDataContext dataContext, - DataBindingContext dataBindingContext, - Artifact resourceApk, - ResourceDependencies resourceDeps, - @Nullable Artifact rTxt, - boolean incremental, - Artifact proguardCfg, - Artifact mainDexProguardCfg, - @Nullable String packageUnderTest, - boolean hasLocalResourceFiles) - throws InterruptedException, RuleErrorException { - - ResourceContainer resourceContainer = - ResourceContainer.builderFromRule(ruleContext) - .setAndroidAssets(AndroidAssets.from(ruleContext)) - .setAndroidResources(AndroidResources.from(ruleContext, "local_resource_files")) - .setManifest(getManifest()) - .setApk(resourceApk) - .setRTxt(rTxt) - .build(); - - AndroidResourcesProcessorBuilder builder = - new AndroidResourcesProcessorBuilder() - .setLibrary(false) - .setApkOut(resourceContainer.getApk()) - .setUncompressedExtensions(ImmutableList.of()) - .setCrunchPng(true) - .setJavaPackage(resourceContainer.getJavaPackage()) - .setDebug(ruleContext.getConfiguration().getCompilationMode() != CompilationMode.OPT) - .withResourceDependencies(resourceDeps) - .setProguardOut(proguardCfg) - .setMainDexProguardOut(mainDexProguardCfg) - .setApplicationId(manifestValues.get("applicationId")) - .setVersionCode(manifestValues.get("versionCode")) - .setVersionName(manifestValues.get("versionName")) - .setThrowOnResourceConflict( - ruleContext - .getConfiguration() - .getFragment(AndroidConfiguration.class) - .throwOnResourceConflict()) - .setPackageUnderTest(packageUnderTest) - .setIsTestWithResources(hasLocalResourceFiles); - if (!incremental) { - builder - .targetAaptVersion(targetAaptVersion) - .setRTxtOut(resourceContainer.getRTxt()) - .setSymbols(resourceContainer.getSymbols()) - .setSourceJarOut(resourceContainer.getJavaSourceJar()); - } - ResourceContainer processed = - builder.build( - dataContext, - resourceContainer, - DataBinding.contextFrom(ruleContext, dataContext.getAndroidConfig())); - - ResourceContainer finalContainer = - new RClassGeneratorActionBuilder() - .targetAaptVersion(AndroidAaptVersion.chooseTargetAaptVersion(ruleContext)) - .withDependencies(resourceDeps) - .setClassJarOut( - ruleContext.getImplicitOutputArtifact( - AndroidRuleClasses.ANDROID_RESOURCES_CLASS_JAR)) - .build(dataContext, processed); - - return ResourceApk.of( - finalContainer, resourceDeps, proguardCfg, mainDexProguardCfg, dataBindingContext); - } - - /** Packages up the manifest with resource and assets from the LocalResourceContainer. */ - public ResourceApk packAarWithDataAndResources( - RuleContext ruleContext, - AndroidDataContext dataContext, - AndroidAssets assets, - AndroidResources resources, - ResourceDependencies resourceDeps, - Artifact rTxt, - Artifact symbols, - Artifact manifestOut, - Artifact mergedResources) - throws InterruptedException { - ResourceContainer resourceContainer = - ResourceContainer.builderFromRule(ruleContext) - .setRTxt(rTxt) - .setJavaPackageFrom(JavaPackageSource.MANIFEST) - .setManifestExported(true) - .setManifest(getManifest()) - .build(); - - // android_library should only build the APK one way (!incremental). - Artifact rJavaClassJar = - ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_RESOURCES_CLASS_JAR); - - resourceContainer = - new AndroidResourceParsingActionBuilder() - .setAssets(assets) - .setResources(resources) - .setOutput(symbols) - .buildAndUpdate(dataContext, resourceContainer); - - ResourceContainer merged = - new AndroidResourceMergingActionBuilder() - .setJavaPackage(resourceContainer.getJavaPackage()) - .withDependencies(resourceDeps) - .setMergedResourcesOut(mergedResources) - .setManifestOut(manifestOut) - .setClassJarOut(rJavaClassJar) - .setThrowOnResourceConflict( - ruleContext - .getConfiguration() - .getFragment(AndroidConfiguration.class) - .throwOnResourceConflict()) - .build(dataContext, resourceContainer); - - ResourceContainer processed = - new AndroidResourceValidatorActionBuilder() - .setJavaPackage(merged.getJavaPackage()) - .setDebug(ruleContext.getConfiguration().getCompilationMode() != CompilationMode.OPT) - .setMergedResources(mergedResources) - .setRTxtOut(merged.getRTxt()) - .setSourceJarOut(merged.getJavaSourceJar()) - .setApkOut(resourceContainer.getApk()) - // aapt2 related artifacts. Will be generated if the targetAaptVersion is AAPT2. - .withDependencies(resourceDeps) - .setCompiledSymbols(merged.getCompiledSymbols()) - .setAapt2RTxtOut(merged.getAapt2RTxt()) - .setAapt2SourceJarOut(merged.getAapt2JavaSourceJar()) - .setStaticLibraryOut(merged.getStaticLibrary()) - .build(dataContext, merged); - - return ResourceApk.of( - processed, - resourceDeps, - DataBinding.contextFrom(ruleContext, dataContext.getAndroidConfig())); - } - - /* Creates an incremental apk from assets and data. */ - public ResourceApk packIncrementalBinaryWithDataAndResources( - RuleContext ruleContext, - AndroidDataContext dataContext, - Artifact resourceApk, - ResourceDependencies resourceDeps, - List<String> uncompressedExtensions, - boolean crunchPng, - Artifact proguardCfg) - throws InterruptedException, RuleErrorException { - AndroidResources resources = AndroidResources.from(ruleContext, "resource_files"); - - // Filter the resources during analysis to prevent processing of dependencies on unwanted - // resources during execution. - ResourceFilterFactory resourceFilterFactory = - ResourceFilterFactory.fromRuleContextAndAttrs(ruleContext); - ResourceFilter resourceFilter = - resourceFilterFactory.getResourceFilter(ruleContext, resourceDeps, resources); - resources = resources.filterLocalResources(ruleContext, resourceFilter); - resourceDeps = resourceDeps.filter(ruleContext, resourceFilter); - - // Now that the LocalResourceContainer has been filtered, we can build a filtered resource - // container from it. - ResourceContainer resourceContainer = - ResourceContainer.builderFromRule(ruleContext) - .setApk(resourceApk) - .setManifest(getManifest()) - .setAndroidAssets(AndroidAssets.from(ruleContext)) - .setAndroidResources(resources) - .build(); - - ResourceContainer processed = - new AndroidResourcesProcessorBuilder() - .setLibrary(false) - .setApkOut(resourceContainer.getApk()) - .setResourceFilterFactory(resourceFilterFactory) - .setUncompressedExtensions(uncompressedExtensions) - .setCrunchPng(crunchPng) - .setJavaPackage(resourceContainer.getJavaPackage()) - .setDebug(ruleContext.getConfiguration().getCompilationMode() != CompilationMode.OPT) - .withResourceDependencies(resourceDeps) - .setProguardOut(proguardCfg) - .setApplicationId(manifestValues.get("applicationId")) - .setVersionCode(manifestValues.get("versionCode")) - .setVersionName(manifestValues.get("versionName")) - .setThrowOnResourceConflict( - ruleContext - .getConfiguration() - .getFragment(AndroidConfiguration.class) - .throwOnResourceConflict()) - .setPackageUnderTest(null) - .build( - dataContext, - resourceContainer, - DataBinding.contextFrom(ruleContext, dataContext.getAndroidConfig())); - - // Intentionally skip building an R class JAR - incremental binaries handle this separately. - - return ResourceApk.of( - processed, - resourceDeps, - proguardCfg, - null, - DataBinding.contextFrom(ruleContext, dataContext.getAndroidConfig())); - } - - /** Packages up the manifest with resource and assets from the rule and dependent resources. */ - // TODO(bazel-team): this method calls for some refactoring, 15+ params including some nullables. - public ResourceApk packBinaryWithDataAndResources( - RuleContext ruleContext, - AndroidDataContext dataContext, - DataBindingContext dataBindingContext, - Artifact resourceApk, - ResourceDependencies resourceDeps, - @Nullable Artifact rTxt, - ResourceFilterFactory resourceFilterFactory, - List<String> uncompressedExtensions, - boolean crunchPng, - Artifact proguardCfg, - @Nullable Artifact mainDexProguardCfg, - boolean conditionalKeepRules, - Artifact manifestOut, - Artifact mergedResources, - @Nullable Artifact featureOf, - @Nullable Artifact featureAfter) - throws InterruptedException, RuleErrorException { - - AndroidResources resources = AndroidResources.from(ruleContext, "resource_files"); - ResourceFilter resourceFilter = - resourceFilterFactory.getResourceFilter(ruleContext, resourceDeps, resources); - resources = resources.filterLocalResources(ruleContext, resourceFilter); - resourceDeps = resourceDeps.filter(ruleContext, resourceFilter); - - // Now that the LocalResourceContainer has been filtered, we can build a filtered resource - // container from it. - ResourceContainer resourceContainer = - ResourceContainer.builderFromRule(ruleContext) - .setAndroidAssets(AndroidAssets.from(ruleContext)) - .setAndroidResources(resources) - .setManifest(getManifest()) - .setRTxt(rTxt) - .setApk(resourceApk) - .build(); - - AndroidConfiguration androidConfiguration = - ruleContext.getConfiguration().getFragment(AndroidConfiguration.class); - - boolean skipParsingAction = - targetAaptVersion == AndroidAaptVersion.AAPT2 && androidConfiguration.skipParsingAction(); - - if (conditionalKeepRules && targetAaptVersion != AndroidAaptVersion.AAPT2) { - throw ruleContext.throwWithRuleError( - "resource cycle shrinking can only be enabled for builds with aapt2"); - } - - final AndroidResourcesProcessorBuilder androidResourcesProcessorBuilder = - new AndroidResourcesProcessorBuilder() - .setLibrary(false) - .setApkOut(resourceContainer.getApk()) - .setResourceFilterFactory(resourceFilterFactory) - .setUncompressedExtensions(uncompressedExtensions) - .setCrunchPng(crunchPng) - .setJavaPackage(resourceContainer.getJavaPackage()) - .setDebug(ruleContext.getConfiguration().getCompilationMode() != CompilationMode.OPT) - .setManifestOut(manifestOut) - .setMergedResourcesOut(mergedResources) - .withResourceDependencies(resourceDeps) - .setProguardOut(proguardCfg) - .setMainDexProguardOut(mainDexProguardCfg) - .conditionalKeepRules(conditionalKeepRules); - dataBindingContext.supplyLayoutInfo(androidResourcesProcessorBuilder::setDataBindingInfoZip); - ResourceContainer processed = - androidResourcesProcessorBuilder - .setApplicationId(manifestValues.get("applicationId")) - .setVersionCode(manifestValues.get("versionCode")) - .setVersionName(manifestValues.get("versionName")) - .setFeatureOf(featureOf) - .setFeatureAfter(featureAfter) - .setThrowOnResourceConflict(androidConfiguration.throwOnResourceConflict()) - .setUseCompiledResourcesForMerge(skipParsingAction) - .targetAaptVersion(targetAaptVersion) - .setRTxtOut(resourceContainer.getRTxt()) - .setSymbols(resourceContainer.getSymbols()) - .setSourceJarOut(resourceContainer.getJavaSourceJar()) - .build( - dataContext, - resourceContainer, - DataBinding.contextFrom(ruleContext, dataContext.getAndroidConfig())); - - ResourceContainer finalContainer = - new RClassGeneratorActionBuilder() - .targetAaptVersion(AndroidAaptVersion.chooseTargetAaptVersion(ruleContext)) - .withDependencies(resourceDeps) - .setClassJarOut( - ruleContext.getImplicitOutputArtifact( - AndroidRuleClasses.ANDROID_RESOURCES_CLASS_JAR)) - .build(dataContext, processed); - - return ResourceApk.of( - finalContainer, resourceDeps, proguardCfg, mainDexProguardCfg, dataBindingContext); - } - - public ResourceApk packLibraryWithDataAndResources( - RuleContext ruleContext, - AndroidDataContext dataContext, - ResourceDependencies resourceDeps, - Artifact rTxt, - Artifact symbols, - Artifact manifestOut, - Artifact mergedResources, - DataBindingContext dataBindingContext) - throws InterruptedException, RuleErrorException { - AndroidResources resources = AndroidResources.from(ruleContext, "resource_files"); - AndroidAssets assets = AndroidAssets.from(ruleContext); - - ResourceContainer.Builder builder = - ResourceContainer.builderFromRule(ruleContext) - .setManifest(getManifest()) - .setSymbols(symbols) - .setRTxt(rTxt) - // Request an APK so it can be inherited when a library is used in a binary's - // resources attr. - // TODO(b/30307842): Remove this once it is no longer needed for resources migration. - .setApk(ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_LIBRARY_APK)); - - if (targetAaptVersion == AndroidAaptVersion.AAPT2) { - builder - .setAapt2JavaSourceJar( - ruleContext.getImplicitOutputArtifact( - AndroidRuleClasses.ANDROID_RESOURCES_AAPT2_SOURCE_JAR)) - .setAapt2RTxt( - ruleContext.getImplicitOutputArtifact( - AndroidRuleClasses.ANDROID_RESOURCES_AAPT2_R_TXT)) - .setCompiledSymbols( - ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_COMPILED_SYMBOLS)) - .setStaticLibrary( - ruleContext.getImplicitOutputArtifact( - AndroidRuleClasses.ANDROID_RESOURCES_AAPT2_LIBRARY_APK)); - } - - final ResourceContainer resourceContainer = builder.build(); - - Artifact rJavaClassJar = - ruleContext.getImplicitOutputArtifact(AndroidRuleClasses.ANDROID_RESOURCES_CLASS_JAR); - - AndroidConfiguration androidConfiguration = - ruleContext.getConfiguration().getFragment(AndroidConfiguration.class); - - boolean skipParsingAction = - targetAaptVersion == AndroidAaptVersion.AAPT2 && androidConfiguration.skipParsingAction(); - - AndroidResourceParsingActionBuilder parsingBuilder = - new AndroidResourceParsingActionBuilder() - .setAssets(assets) - .setResources(resources) - .setOutput(resourceContainer.getSymbols()) - .setCompiledSymbolsOutput(resourceContainer.getCompiledSymbols()); - - if (resourceContainer.getCompiledSymbols() != null) { - // TODO(corysmith): Centralize the data binding processing and zipping into a single - // action. Data binding processing needs to be triggered here as well as the merger to - // avoid aapt2 from throwing an error during compilation. - dataBindingContext.supplyLayoutInfo( - layoutInfo -> - parsingBuilder - .setDataBindingInfoZip( - ruleContext.getUniqueDirectoryArtifact("dummydatabinding", "unused.zip")) - .setManifest(resourceContainer.getManifest()) - .setJavaPackage(resourceContainer.getJavaPackage())); - } - ResourceContainer parsedResourceContainer = - parsingBuilder.buildAndUpdate(dataContext, resourceContainer); - - ResourceContainer merged = - new AndroidResourceMergingActionBuilder() - .setJavaPackage(parsedResourceContainer.getJavaPackage()) - .withDependencies(resourceDeps) - .setThrowOnResourceConflict(androidConfiguration.throwOnResourceConflict()) - .setUseCompiledMerge(skipParsingAction) - .setMergedResourcesOut(mergedResources) - .setManifestOut(manifestOut) - .setClassJarOut(rJavaClassJar) - .build(dataContext, parsedResourceContainer); - - ResourceContainer processed = - new AndroidResourceValidatorActionBuilder() - .setJavaPackage(merged.getJavaPackage()) - .setDebug(ruleContext.getConfiguration().getCompilationMode() != CompilationMode.OPT) - .setMergedResources(mergedResources) - .setRTxtOut(merged.getRTxt()) - .setSourceJarOut(merged.getJavaSourceJar()) - .setApkOut(parsedResourceContainer.getApk()) - .withDependencies(resourceDeps) - // aapt2 related artifacts. Will be generated if the targetAaptVersion is AAPT2. - .setCompiledSymbols(merged.getCompiledSymbols()) - .setAapt2RTxtOut(merged.getAapt2RTxt()) - .setAapt2SourceJarOut(merged.getAapt2JavaSourceJar()) - .setStaticLibraryOut(merged.getStaticLibrary()) - .build(dataContext, merged); - - return ResourceApk.of(processed, resourceDeps, dataBindingContext); - } - - public Artifact getManifest() { - return manifest; - } } diff --git a/src/test/shell/bazel/android/android_instrumentation_test_integration_test.sh b/src/test/shell/bazel/android/android_instrumentation_test_integration_test.sh index 4f52867698..36f76b17ff 100755 --- a/src/test/shell/bazel/android/android_instrumentation_test_integration_test.sh +++ b/src/test/shell/bazel/android/android_instrumentation_test_integration_test.sh @@ -130,36 +130,6 @@ EOF "do not reference the same target package" } -function test_no_target_package_attribute_build_failure() { - create_new_workspace - setup_android_sdk_support - setup_android_instrumentation_test_env - - cat > javatests/com/bin/AndroidManifest.xml <<EOF -<?xml version="1.0" encoding="utf-8"?> -<manifest package='com.bin' xmlns:android='http://schemas.android.com/apk/res/android'> - <instrumentation /> -</manifest> -EOF - - assert_build_fails //javatests/com/bin:instr \ - "No <instrumentation> tag containing the targetPackage attribute" -} - -function test_target_package_no_package_specified_build_failure() { - create_new_workspace - setup_android_sdk_support - setup_android_instrumentation_test_env - - cat > java/com/bin/AndroidManifest.xml <<EOF -<?xml version="1.0" encoding="utf-8"?> -<manifest xmlns:android="http://schemas.android.com/apk/res/android" /> -EOF - - assert_build_fails //javatests/com/bin:instr \ - "needs to specify the package name" -} - function test_android_instrumentation_binary_class_filtering() { create_new_workspace setup_android_sdk_support |