Enable/disable optimize for android_test and android_test_helper_app by default.

Follow up to https://android-review.git.corp.google.com/c/platform/build/bazel/+/2727853/comment/f2c5802e_245fd302/

Test: New unit tests, presubmits
Test: `atest-dev HelloWorldTests --roboleaf-mode=dev`

Change-Id: I88efcf72fbcb717d05e71ba50ba7802c3bece816
This commit is contained in:
Jingwen Chen 2023-09-20 13:34:16 +00:00
parent 40c7bae1df
commit 65229207d1
2 changed files with 95 additions and 5 deletions

View file

@ -43,11 +43,44 @@ func TestMinimalAndroidTest(t *testing.T) {
"assets/asset.png": "",
},
Blueprint: `
android_test {
name: "TestApp",
srcs: ["app.java"],
sdk_version: "current",
}
`,
ExpectedBazelTargets: []string{
MakeBazelTarget("android_test", "TestApp", AttrNameToString{
"srcs": `["app.java"]`,
"manifest": `"AndroidManifest.xml"`,
"resource_files": `["res/res.png"]`,
"sdk_version": `"current"`,
"assets": `["assets/asset.png"]`,
"assets_dir": `"assets"`,
// no need for optimize = False because it's false for
// android_test by default
}),
}})
}
func TestAndroidTest_OptimizationEnabled(t *testing.T) {
runAndroidAppTestCase(t, Bp2buildTestCase{
Description: "Android test - simple example",
ModuleTypeUnderTest: "android_test",
ModuleTypeUnderTestFactory: java.AndroidTestFactory,
Filesystem: map[string]string{
"app.java": "",
"res/res.png": "",
"AndroidManifest.xml": "",
"assets/asset.png": "",
},
Blueprint: `
android_test {
name: "TestApp",
srcs: ["app.java"],
sdk_version: "current",
optimize: {
enabled: true,
shrink: true,
optimize: true,
obfuscate: true,
@ -62,6 +95,9 @@ android_test {
"sdk_version": `"current"`,
"assets": `["assets/asset.png"]`,
"assets_dir": `"assets"`,
// optimize = True because it's false for android_test by
// default
"optimize": `True`,
}),
}})
}
@ -98,6 +134,45 @@ android_test_helper_app {
"assets": `["assets/asset.png"]`,
"assets_dir": `"assets"`,
"testonly": `True`,
// no need for optimize = True because it's true for
// android_test_helper_app by default
}),
}})
}
func TestAndroidTestHelperApp_OptimizationDisabled(t *testing.T) {
runAndroidAppTestCase(t, Bp2buildTestCase{
Description: "Android test helper app - simple example",
ModuleTypeUnderTest: "android_test_helper_app",
ModuleTypeUnderTestFactory: java.AndroidTestHelperAppFactory,
Filesystem: map[string]string{
"app.java": "",
"res/res.png": "",
"AndroidManifest.xml": "",
"assets/asset.png": "",
},
Blueprint: `
android_test_helper_app {
name: "TestApp",
srcs: ["app.java"],
sdk_version: "current",
optimize: {
enabled: false,
},
}
`,
ExpectedBazelTargets: []string{
MakeBazelTarget("android_binary", "TestApp", AttrNameToString{
"srcs": `["app.java"]`,
"manifest": `"AndroidManifest.xml"`,
"resource_files": `["res/res.png"]`,
"sdk_version": `"current"`,
"assets": `["assets/asset.png"]`,
"assets_dir": `"assets"`,
"testonly": `True`,
// optimize = False because it's true for
// android_test_helper_app by default
"optimize": `False`,
}),
}})
}

View file

@ -1683,9 +1683,22 @@ func convertWithBp2build(ctx android.Bp2buildMutatorContext, a *AndroidApp) (boo
Updatable: a.appProperties.Updatable,
}
if !BoolDefault(a.dexProperties.Optimize.Enabled, true) {
appAttrs.Optimize = proptools.BoolPtr(false)
} else {
// Optimization is..
// - enabled by default for android_app, android_test_helper_app
// - disabled by default for android_test
//
// TODO(b/192032291): Disable android_test_helper_app optimization by
// default after auditing downstream usage.
if a.dexProperties.Optimize.Enabled == nil {
// Property was not explicitly defined.
a.dexProperties.Optimize.Enabled = &a.dexProperties.Optimize.EnabledByDefault
}
if Bool(a.dexProperties.Optimize.Enabled) {
if !a.dexProperties.Optimize.EnabledByDefault {
// explicitly enable optimize for module types that disable it by default
appAttrs.Optimize = proptools.BoolPtr(true)
}
handCraftedFlags := ""
if Bool(a.dexProperties.Optimize.Ignore_warnings) {
handCraftedFlags += "-ignorewarning "
@ -1715,6 +1728,9 @@ func convertWithBp2build(ctx android.Bp2buildMutatorContext, a *AndroidApp) (boo
})
appAttrs.Proguard_specs.Add(bazel.MakeLabelAttribute(":" + generatedFlagFileRuleName))
}
} else if a.dexProperties.Optimize.EnabledByDefault {
// explicitly disable optimize for module types that enable it by default
appAttrs.Optimize = proptools.BoolPtr(false)
}
commonAttrs, bp2BuildInfo, supported := a.convertLibraryAttrsBp2Build(ctx)
@ -1803,13 +1819,12 @@ func (atha *AndroidTestHelperApp) ConvertWithBp2build(ctx android.Bp2buildMutato
// an android_test_helper_app is an android_binary with testonly = True
commonAttrs.Testonly = proptools.BoolPtr(true)
// additionally, it sets default values differently to android_app,
// android_test_helper_app sets default values differently to android_app,
// https://cs.android.com/android/platform/superproject/main/+/main:build/soong/java/app.go;l=1273-1279;drc=e12c083198403ec694af6c625aed11327eb2bf7f
//
// installable: true (settable prop)
// use_embedded_native_libs: true (settable prop)
// lint.test: true (settable prop)
// optimize EnabledByDefault: true (blueprint mutated prop)
// AlwaysPackageNativeLibs: true (blueprint mutated prop)
// dexpreopt isTest: true (not prop)