Add gen_notice module.

Refactor notices to support notices for multiple modules.

Enforce visibility and handle missing dependencies.

Test: m cts dist reportmissinglicenses

Test: m droid dist

Bug: 213388645
Bug: 240342946

Change-Id: Id6a81987f087419ad37d0cce57a71e8a7c4cd6e0
Merged-in: Id6a81987f087419ad37d0cce57a71e8a7c4cd6e0
Merged-in: I07963c83eaddfe363a04871e813b56fe7f1465ad
Merged-in: Ib2f8d0ad46ffe795d392166a74a7c6309b1246e2
Merged-in: I1d630c3d14d27b7605ab13d204d34b6851a24d33
diff --git a/java/app.go b/java/app.go
index 41419ba..94e6fb9 100755
--- a/java/app.go
+++ b/java/app.go
@@ -589,16 +589,6 @@
 
 	a.classLoaderContexts = a.usesLibrary.classLoaderContextForUsesLibDeps(ctx)
 
-	var noticeAssetPath android.WritablePath
-	if Bool(a.appProperties.Embed_notices) || ctx.Config().IsEnvTrue("ALWAYS_EMBED_NOTICES") {
-		// The rule to create the notice file can't be generated yet, as the final output path
-		// for the apk isn't known yet.  Add the path where the notice file will be generated to the
-		// aapt rules now before calling aaptBuildActions, the rule to create the notice file will
-		// be generated later.
-		noticeAssetPath = android.PathForModuleOut(ctx, "NOTICE", "NOTICE.html.gz")
-		a.aapt.noticeFile = android.OptionalPathForPath(noticeAssetPath)
-	}
-
 	// Process all building blocks, from AAPT to certificates.
 	a.aaptBuildActions(ctx)
 
@@ -673,8 +663,7 @@
 		a.extraOutputFiles = append(a.extraOutputFiles, v4SignatureFile)
 	}
 
-	if a.aapt.noticeFile.Valid() {
-		// Generating the notice file rule has to be here after a.outputFile is known.
+	if Bool(a.appProperties.Embed_notices) || ctx.Config().IsEnvTrue("ALWAYS_EMBED_NOTICES") {
 		noticeFile := android.PathForModuleOut(ctx, "NOTICE.html.gz")
 		android.BuildNoticeHtmlOutputFromLicenseMetadata(
 			ctx, noticeFile, "", "",
@@ -683,11 +672,13 @@
 				android.PathForModuleInstall(ctx).String() + "/",
 				a.outputFile.String(),
 			})
+		noticeAssetPath := android.PathForModuleOut(ctx, "NOTICE", "NOTICE.html.gz")
 		builder := android.NewRuleBuilder(pctx, ctx)
 		builder.Command().Text("cp").
 			Input(noticeFile).
 			Output(noticeAssetPath)
 		builder.Build("notice_dir", "Building notice dir")
+		a.aapt.noticeFile = android.OptionalPathForPath(noticeAssetPath)
 	}
 
 	for _, split := range a.aapt.splits {