diff --git a/java/app.go b/java/app.go index 21ee34e7c..2b52eab15 100755 --- a/java/app.go +++ b/java/app.go @@ -638,7 +638,21 @@ func (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) { } certificates := processMainCert(a.ModuleBase, a.getCertString(ctx), certificateDeps, ctx) - a.certificate = certificates[0] + + // This can be reached with an empty certificate list if AllowMissingDependencies is set + // and the certificate property for this module is a module reference to a missing module. + if len(certificates) > 0 { + a.certificate = certificates[0] + } else { + if !ctx.Config().AllowMissingDependencies() && len(ctx.GetMissingDependencies()) > 0 { + panic("Should only get here if AllowMissingDependencies set and there are missing dependencies") + } + // Set a certificate to avoid panics later when accessing it. + a.certificate = Certificate{ + Key: android.PathForModuleOut(ctx, "missing.pk8"), + Pem: android.PathForModuleOut(ctx, "missing.pem"), + } + } // Build a final signed app package. packageFile := android.PathForModuleOut(ctx, a.installApkName+".apk") diff --git a/java/app_test.go b/java/app_test.go index 08baf5434..6a4508cd6 100644 --- a/java/app_test.go +++ b/java/app_test.go @@ -2948,3 +2948,24 @@ func TestTargetSdkVersionManifestFixer(t *testing.T) { android.AssertStringDoesContain(t, testCase.name, manifestFixerArgs, "--targetSdkVersion "+testCase.targetSdkVersionExpected) } } + +func TestAppMissingCertificateAllowMissingDependencies(t *testing.T) { + result := android.GroupFixturePreparers( + PrepareForTestWithJavaDefaultModules, + android.PrepareForTestWithAllowMissingDependencies, + android.PrepareForTestWithAndroidMk, + ).RunTestWithBp(t, ` + android_app { + name: "foo", + srcs: ["a.java"], + certificate: ":missing_certificate", + sdk_version: "current", + }`) + + foo := result.ModuleForTests("foo", "android_common") + fooApk := foo.Output("foo.apk") + if fooApk.Rule != android.ErrorRule { + t.Fatalf("expected ErrorRule for foo.apk, got %s", fooApk.Rule.String()) + } + android.AssertStringDoesContain(t, "expected error rule message", fooApk.Args["error"], "missing dependencies: missing_certificate\n") +}