From 53a70a4870067b68cdad71ff9bbd03db545086c5 Mon Sep 17 00:00:00 2001 From: Paul Duffin Date: Tue, 11 Jan 2022 14:35:55 +0000 Subject: [PATCH] Prevent using android_app_import in instrumentation_for property Previously, referencing a prebuilt android_app_import in an android_test's instrumentation_for property instead of the source android_app module (e.g. because the prebuilt is preferred) could result in compilation errors if the android_test sources depended on classes from the app. That is because while the android_app provides a classes jar file to be added to the android_test's classpath the android_app_import does not. This change reports that situation as an error. Bug: 207128192 Test: m nothing Change-Id: I26ea90e7a3f57707fc0c094e0d8b8dc9a9e135ac --- java/app_test.go | 25 ++++++++++++++++++++++++- java/base.go | 3 +++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/java/app_test.go b/java/app_test.go index 4da7c3dba..c8e1399cc 100644 --- a/java/app_test.go +++ b/java/app_test.go @@ -2168,10 +2168,33 @@ func TestAndroidTest_FixTestConfig(t *testing.T) { t.Errorf("test_config_fixer was not expected to run, but did: %q", params.RuleParams.Command) } } - } } +func TestInstrumentationTargetPrebuilt(t *testing.T) { + bp := ` + android_app_import { + name: "foo", + apk: "foo.apk", + presigned: true, + } + + android_test { + name: "bar", + srcs: ["a.java"], + instrumentation_for: "foo", + sdk_version: "current", + } + ` + + android.GroupFixturePreparers( + PrepareForTestWithJavaDefaultModules, + ).ExtendWithErrorHandler( + android.FixtureExpectsAtLeastOneErrorMatchingPattern( + "instrumentation_for: dependency \"foo\" of type \"android_app_import\" does not provide JavaInfo so is unsuitable for use with this property")). + RunTestWithBp(t, bp) +} + func TestStl(t *testing.T) { ctx, _ := testJava(t, cc.GatherRequiredDepsForTest(android.Android)+` cc_library { diff --git a/java/base.go b/java/base.go index 7cd71a270..8d5fe1302 100644 --- a/java/base.go +++ b/java/base.go @@ -1936,6 +1936,9 @@ func (j *Module) collectDeps(ctx android.ModuleContext) deps { sm := module.(SystemModulesProvider) outputDir, outputDeps := sm.OutputDirAndDeps() deps.systemModules = &systemModules{outputDir, outputDeps} + + case instrumentationForTag: + ctx.PropertyErrorf("instrumentation_for", "dependency %q of type %q does not provide JavaInfo so is unsuitable for use with this property", ctx.OtherModuleName(module), ctx.OtherModuleType(module)) } }