diff --git a/java/aar.go b/java/aar.go index 479b5e0be..c3d4fbe9c 100644 --- a/java/aar.go +++ b/java/aar.go @@ -805,8 +805,11 @@ func (a *AndroidLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) a.linter.manifest = a.aapt.manifestPath a.linter.resources = a.aapt.resourceFiles - a.Module.extraProguardFlagFiles = append(a.Module.extraProguardFlagFiles, - a.proguardOptionsFile) + proguardSpecInfo := a.collectProguardSpecInfo(ctx) + ctx.SetProvider(ProguardSpecInfoProvider, proguardSpecInfo) + a.exportedProguardFlagFiles = proguardSpecInfo.ProguardFlagsFiles.ToList() + a.extraProguardFlagFiles = append(a.extraProguardFlagFiles, a.exportedProguardFlagFiles...) + a.extraProguardFlagFiles = append(a.extraProguardFlagFiles, a.proguardOptionsFile) var extraSrcJars android.Paths var extraCombinedJars android.Paths @@ -832,10 +835,6 @@ func (a *AndroidLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) ctx.CheckbuildFile(a.aarFile) } - proguardSpecInfo := a.collectProguardSpecInfo(ctx) - ctx.SetProvider(ProguardSpecInfoProvider, proguardSpecInfo) - a.exportedProguardFlagFiles = proguardSpecInfo.ProguardFlagsFiles.ToList() - prebuiltJniPackages := android.Paths{} ctx.VisitDirectDeps(func(module android.Module) { if info, ok := ctx.OtherModuleProvider(module, JniPackageProvider).(JniPackageInfo); ok { diff --git a/java/dex.go b/java/dex.go index 9ce5053fd..aa017834d 100644 --- a/java/dex.go +++ b/java/dex.go @@ -300,6 +300,8 @@ func (d *dexer) r8Flags(ctx android.ModuleContext, flags javaBuilderFlags) (r8Fl flagFiles = append(flagFiles, android.PathsForModuleSrc(ctx, opt.Proguard_flags_files)...) + flagFiles = android.FirstUniquePaths(flagFiles) + r8Flags = append(r8Flags, android.JoinWithPrefix(flagFiles.Strings(), "-include ")) r8Deps = append(r8Deps, flagFiles...) diff --git a/java/dex_test.go b/java/dex_test.go index ec1ef1516..1ecdae0c5 100644 --- a/java/dex_test.go +++ b/java/dex_test.go @@ -385,13 +385,53 @@ func TestProguardFlagsInheritanceStatic(t *testing.T) { func TestProguardFlagsInheritance(t *testing.T) { directDepFlagsFileName := "direct_dep.flags" transitiveDepFlagsFileName := "transitive_dep.flags" - bp := ` - android_app { - name: "app", - static_libs: ["androidlib"], // this must be static_libs to initate dexing - platform_apis: true, - } + topLevelModules := []struct { + name string + definition string + }{ + { + name: "android_app", + definition: ` + android_app { + name: "app", + static_libs: ["androidlib"], // this must be static_libs to initate dexing + platform_apis: true, + } + `, + }, + { + name: "android_library", + definition: ` + android_library { + name: "app", + static_libs: ["androidlib"], // this must be static_libs to initate dexing + installable: true, + optimize: { + enabled: true, + shrink: true, + }, + } + `, + }, + { + name: "java_library", + definition: ` + java_library { + name: "app", + static_libs: ["androidlib"], // this must be static_libs to initate dexing + srcs: ["Foo.java"], + installable: true, + optimize: { + enabled: true, + shrink: true, + }, + } + `, + }, + } + + bp := ` android_library { name: "androidlib", static_libs: ["app_dep"], @@ -558,43 +598,46 @@ func TestProguardFlagsInheritance(t *testing.T) { }, } - for _, tc := range testcases { - t.Run(tc.name, func(t *testing.T) { - result := android.GroupFixturePreparers( - PrepareForTestWithJavaDefaultModules, - android.FixtureMergeMockFs(android.MockFS{ - directDepFlagsFileName: nil, - transitiveDepFlagsFileName: nil, - }), - ).RunTestWithBp(t, - fmt.Sprintf( - bp, - tc.depType, - tc.transitiveDepType, - tc.depExportsFlagsFiles, - tc.transitiveDepExportsFlagsFiles, - ), - ) - appR8 := result.ModuleForTests("app", "android_common").Rule("r8") + for _, topLevelModuleDef := range topLevelModules { + for _, tc := range testcases { + t.Run(topLevelModuleDef.name+"-"+tc.name, func(t *testing.T) { + result := android.GroupFixturePreparers( + PrepareForTestWithJavaDefaultModules, + android.FixtureMergeMockFs(android.MockFS{ + directDepFlagsFileName: nil, + transitiveDepFlagsFileName: nil, + }), + ).RunTestWithBp(t, + topLevelModuleDef.definition+ + fmt.Sprintf( + bp, + tc.depType, + tc.transitiveDepType, + tc.depExportsFlagsFiles, + tc.transitiveDepExportsFlagsFiles, + ), + ) + appR8 := result.ModuleForTests("app", "android_common").Rule("r8") - shouldHaveDepFlags := android.InList(directDepFlagsFileName, tc.expectedFlagsFiles) - if shouldHaveDepFlags { - android.AssertStringDoesContain(t, "expected deps's proguard flags", - appR8.Args["r8Flags"], directDepFlagsFileName) - } else { - android.AssertStringDoesNotContain(t, "app did not expect deps's proguard flags", - appR8.Args["r8Flags"], directDepFlagsFileName) - } + shouldHaveDepFlags := android.InList(directDepFlagsFileName, tc.expectedFlagsFiles) + if shouldHaveDepFlags { + android.AssertStringDoesContain(t, "expected deps's proguard flags", + appR8.Args["r8Flags"], directDepFlagsFileName) + } else { + android.AssertStringDoesNotContain(t, "app did not expect deps's proguard flags", + appR8.Args["r8Flags"], directDepFlagsFileName) + } - shouldHaveTransitiveDepFlags := android.InList(transitiveDepFlagsFileName, tc.expectedFlagsFiles) - if shouldHaveTransitiveDepFlags { - android.AssertStringDoesContain(t, "expected transitive deps's proguard flags", - appR8.Args["r8Flags"], transitiveDepFlagsFileName) - } else { - android.AssertStringDoesNotContain(t, "app did not expect transitive deps's proguard flags", - appR8.Args["r8Flags"], transitiveDepFlagsFileName) - } - }) + shouldHaveTransitiveDepFlags := android.InList(transitiveDepFlagsFileName, tc.expectedFlagsFiles) + if shouldHaveTransitiveDepFlags { + android.AssertStringDoesContain(t, "expected transitive deps's proguard flags", + appR8.Args["r8Flags"], transitiveDepFlagsFileName) + } else { + android.AssertStringDoesNotContain(t, "app did not expect transitive deps's proguard flags", + appR8.Args["r8Flags"], transitiveDepFlagsFileName) + } + }) + } } } @@ -606,11 +649,6 @@ func TestProguardFlagsInheritanceAppImport(t *testing.T) { platform_apis: true, } - android_library { - name: "androidlib", - static_libs: ["aarimport"], - } - android_library_import { name: "aarimport", aars: ["import.aar"], diff --git a/java/java.go b/java/java.go index bc240508b..55efb5f17 100644 --- a/java/java.go +++ b/java/java.go @@ -697,6 +697,11 @@ func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) { j.stem = proptools.StringDefault(j.overridableDeviceProperties.Stem, ctx.ModuleName()) + proguardSpecInfo := j.collectProguardSpecInfo(ctx) + ctx.SetProvider(ProguardSpecInfoProvider, proguardSpecInfo) + j.exportedProguardFlagFiles = proguardSpecInfo.ProguardFlagsFiles.ToList() + j.extraProguardFlagFiles = append(j.extraProguardFlagFiles, j.exportedProguardFlagFiles...) + apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo) if !apexInfo.IsForPlatform() { j.hideApexVariantFromMake = true @@ -741,10 +746,6 @@ func (j *Library) GenerateAndroidBuildActions(ctx android.ModuleContext) { } j.installFile = ctx.InstallFile(installDir, j.Stem()+".jar", j.outputFile, extraInstallDeps...) } - - proguardSpecInfo := j.collectProguardSpecInfo(ctx) - ctx.SetProvider(ProguardSpecInfoProvider, proguardSpecInfo) - j.exportedProguardFlagFiles = proguardSpecInfo.ProguardFlagsFiles.ToList() } func (j *Library) DepsMutator(ctx android.BottomUpMutatorContext) {