diff --git a/bp2build/android_app_conversion_test.go b/bp2build/android_app_conversion_test.go index 25494a162..f2ee322e6 100644 --- a/bp2build/android_app_conversion_test.go +++ b/bp2build/android_app_conversion_test.go @@ -47,6 +47,11 @@ android_app { name: "TestApp", srcs: ["app.java"], sdk_version: "current", + optimize: { + shrink: true, + optimize: true, + obfuscate: true, + }, } `, ExpectedBazelTargets: []string{ @@ -75,17 +80,25 @@ func TestAndroidAppAllSupportedFields(t *testing.T) { }, Blueprint: simpleModuleDoNotConvertBp2build("android_app", "static_lib_dep") + ` android_app { - name: "TestApp", - srcs: ["app.java"], - sdk_version: "current", - package_name: "com.google", - resource_dirs: ["resa", "resb"], - manifest: "manifest/AndroidManifest.xml", - static_libs: ["static_lib_dep"], - java_version: "7", - certificate: "foocert", - required: ["static_lib_dep"], - asset_dirs: ["assets_"], + name: "TestApp", + srcs: ["app.java"], + sdk_version: "current", + package_name: "com.google", + resource_dirs: ["resa", "resb"], + manifest: "manifest/AndroidManifest.xml", + static_libs: ["static_lib_dep"], + java_version: "7", + certificate: "foocert", + required: ["static_lib_dep"], + asset_dirs: ["assets_"], + optimize: { + enabled: true, + optimize: false, + proguard_flags_files: ["proguard.flags"], + shrink: false, + obfuscate: false, + ignore_warnings: true, + }, } `, ExpectedBazelTargets: []string{ @@ -103,6 +116,14 @@ android_app { "java_version": `"7"`, "sdk_version": `"current"`, "certificate_name": `"foocert"`, + "proguard_specs": `[ + "proguard.flags", + ":TestApp_proguard_flags", + ]`, + }), + MakeBazelTarget("genrule", "TestApp_proguard_flags", AttrNameToString{ + "outs": `["TestApp_proguard.flags"]`, + "cmd": `"echo -ignorewarning -dontshrink -dontoptimize -dontobfuscate > $(OUTS)"`, }), }}) } @@ -129,7 +150,10 @@ android_app { x86: { srcs: ["x86.java"], } - } + }, + optimize: { + enabled: false, + }, } `, ExpectedBazelTargets: []string{ @@ -142,6 +166,7 @@ android_app { "manifest": `"AndroidManifest.xml"`, "resource_files": `["res/res.png"]`, "sdk_version": `"current"`, + "optimize": `False`, }), }}) } @@ -157,6 +182,9 @@ android_app { name: "TestApp", certificate: ":foocert", sdk_version: "current", + optimize: { + enabled: false, + }, } `, ExpectedBazelTargets: []string{ @@ -165,6 +193,7 @@ android_app { "manifest": `"AndroidManifest.xml"`, "resource_files": `[]`, "sdk_version": `"current"`, // use as default + "optimize": `False`, }), }}) } @@ -182,6 +211,9 @@ android_app { name: "TestApp", certificate: "foocert", sdk_version: "current", + optimize: { + enabled: false, + }, } `, ExpectedBazelTargets: []string{ @@ -190,6 +222,7 @@ android_app { "manifest": `"AndroidManifest.xml"`, "resource_files": `[]`, "sdk_version": `"current"`, // use as default + "optimize": `False`, }), }}) } @@ -207,6 +240,9 @@ android_app { name: "TestApp", certificate: "foocert", sdk_version: "current", + optimize: { + enabled: false, + }, } `, ExpectedBazelTargets: []string{ @@ -215,6 +251,7 @@ android_app { "manifest": `"AndroidManifest.xml"`, "resource_files": `[]`, "sdk_version": `"current"`, // use as default + "optimize": `False`, }), }}) } @@ -225,12 +262,14 @@ func TestAndroidAppLibs(t *testing.T) { ModuleTypeUnderTest: "android_app", ModuleTypeUnderTestFactory: java.AndroidAppFactory, Filesystem: map[string]string{}, - Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "foocert") + - simpleModuleDoNotConvertBp2build("java_library", "barLib") + ` + Blueprint: simpleModuleDoNotConvertBp2build("java_library", "barLib") + ` android_app { name: "foo", libs: ["barLib"], sdk_version: "current", + optimize: { + enabled: false, + }, } `, ExpectedBazelTargets: []string{ @@ -239,6 +278,7 @@ android_app { "resource_files": `[]`, "deps": `[":barLib-neverlink"]`, "sdk_version": `"current"`, // use as default + "optimize": `False`, }), }}) } @@ -260,6 +300,9 @@ android_app { manifest: "fooManifest.xml", libs: ["barLib"], sdk_version: "current", + optimize: { + enabled: false, + }, } `, ExpectedBazelTargets: []string{ @@ -278,6 +321,7 @@ android_app { "certificate": `":foocert"`, "manifest": `"fooManifest.xml"`, "sdk_version": `"current"`, // use as default + "optimize": `False`, }), }}) } @@ -290,15 +334,17 @@ func TestAndroidAppCommonSrcs(t *testing.T) { Filesystem: map[string]string{ "res/res.png": "", }, - Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "foocert") + ` + Blueprint: ` android_app { name: "foo", srcs: ["a.java"], common_srcs: ["b.kt"], - certificate: "foocert", manifest: "fooManifest.xml", libs: ["barLib"], sdk_version: "current", + optimize: { + enabled: false, + }, } java_library{ name: "barLib", @@ -315,10 +361,10 @@ java_library{ "sdk_version": `"current"`, // use as default }), MakeBazelTarget("android_binary", "foo", AttrNameToString{ - "deps": `[":foo_kt"]`, - "certificate_name": `"foocert"`, - "manifest": `"fooManifest.xml"`, - "sdk_version": `"current"`, // use as default + "deps": `[":foo_kt"]`, + "manifest": `"fooManifest.xml"`, + "sdk_version": `"current"`, // use as default + "optimize": `False`, }), }}) } @@ -331,14 +377,16 @@ func TestAndroidAppKotlinCflags(t *testing.T) { Filesystem: map[string]string{ "res/res.png": "", }, - Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "foocert") + ` + Blueprint: ` android_app { name: "foo", srcs: ["a.java", "b.kt"], - certificate: ":foocert", manifest: "fooManifest.xml", kotlincflags: ["-flag1", "-flag2"], sdk_version: "current", + optimize: { + enabled: false, + }, } `, ExpectedBazelTargets: []string{ @@ -357,9 +405,9 @@ android_app { }), MakeBazelTarget("android_binary", "foo", AttrNameToString{ "deps": `[":foo_kt"]`, - "certificate": `":foocert"`, "manifest": `"fooManifest.xml"`, "sdk_version": `"current"`, + "optimize": `False`, }), }}) } @@ -370,13 +418,16 @@ func TestAndroidAppManifestSdkVersionsProvided(t *testing.T) { ModuleTypeUnderTest: "android_app", ModuleTypeUnderTestFactory: java.AndroidAppFactory, Filesystem: map[string]string{}, - Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "foocert") + ` + Blueprint: ` android_app { name: "foo", sdk_version: "current", min_sdk_version: "24", max_sdk_version: "30", target_sdk_version: "29", + optimize: { + enabled: false, + }, } `, ExpectedBazelTargets: []string{ @@ -389,6 +440,7 @@ android_app { "targetSdkVersion": "29", }`, "sdk_version": `"current"`, + "optimize": `False`, }), }}) } @@ -399,10 +451,13 @@ func TestAndroidAppMinAndTargetSdkDefaultToSdkVersion(t *testing.T) { ModuleTypeUnderTest: "android_app", ModuleTypeUnderTestFactory: java.AndroidAppFactory, Filesystem: map[string]string{}, - Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "foocert") + ` + Blueprint: ` android_app { name: "foo", sdk_version: "30", + optimize: { + enabled: false, + }, } `, ExpectedBazelTargets: []string{ @@ -414,6 +469,7 @@ android_app { "targetSdkVersion": "30", }`, "sdk_version": `"30"`, + "optimize": `False`, }), }}) } diff --git a/genrule/genrule.go b/genrule/genrule.go index 8e3f2780d..d1c2f1378 100644 --- a/genrule/genrule.go +++ b/genrule/genrule.go @@ -912,7 +912,7 @@ type genRuleProperties struct { Out []string } -type bazelGenruleAttributes struct { +type BazelGenruleAttributes struct { Srcs bazel.LabelListAttribute Outs []string Tools bazel.LabelListAttribute @@ -1036,7 +1036,7 @@ func (m *Module) ConvertWithBp2build(ctx android.TopDownMutatorContext) { break } } - attrs := &bazelGenruleAttributes{ + attrs := &BazelGenruleAttributes{ Srcs: srcs, Outs: outs, Cmd: cmdProp, diff --git a/java/app.go b/java/app.go index dadae4827..d71cd777c 100755 --- a/java/app.go +++ b/java/app.go @@ -29,6 +29,7 @@ import ( "android/soong/bazel" "android/soong/cc" "android/soong/dexpreopt" + "android/soong/genrule" "android/soong/tradefed" ) @@ -1614,6 +1615,8 @@ type bazelAndroidAppAttributes struct { Certificate bazel.LabelAttribute Certificate_name bazel.StringAttribute Manifest_values *manifestValueAttribute + Optimize *bool + Proguard_specs bazel.LabelListAttribute } // ConvertWithBp2build is used to convert android_app to Bazel. @@ -1665,6 +1668,41 @@ func (a *AndroidApp) ConvertWithBp2build(ctx android.TopDownMutatorContext) { Manifest_values: manifestValues, } + if !BoolDefault(a.dexProperties.Optimize.Enabled, true) { + appAttrs.Optimize = proptools.BoolPtr(false) + } else { + handCraftedFlags := "" + if Bool(a.dexProperties.Optimize.Ignore_warnings) { + handCraftedFlags += "-ignorewarning " + } + if !Bool(a.dexProperties.Optimize.Shrink) { + handCraftedFlags += "-dontshrink " + } + if !Bool(a.dexProperties.Optimize.Optimize) { + handCraftedFlags += "-dontoptimize " + } + if !Bool(a.dexProperties.Optimize.Obfuscate) { + handCraftedFlags += "-dontobfuscate " + } + appAttrs.Proguard_specs = bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, a.dexProperties.Optimize.Proguard_flags_files)) + if handCraftedFlags != "" { + generatedFlagFileRuleName := a.Name() + "_proguard_flags" + ctx.CreateBazelTargetModule(bazel.BazelTargetModuleProperties{ + Rule_class: "genrule", + }, android.CommonAttributes{ + Name: generatedFlagFileRuleName, + SkipData: proptools.BoolPtr(true), + }, &genrule.BazelGenruleAttributes{ + Outs: []string{a.Name() + "_proguard.flags"}, + Cmd: bazel.StringAttribute{ + Value: proptools.StringPtr("echo " + handCraftedFlags + "> $(OUTS)"), + }, + }) + appAttrs.Proguard_specs.Add(bazel.MakeLabelAttribute(":" + generatedFlagFileRuleName)) + } + + } + props := bazel.BazelTargetModuleProperties{ Rule_class: "android_binary", Bzl_load_location: "//build/bazel/rules/android:android_binary.bzl",