Ignore missing prebuilt_apis of java_sdk_library

Building java_sdk_library without defining prebuilt_apis has been
failing with weird error messages. So one have to touch empty txt files
and create prebuilt_apis module, even when the java_sdk_library is
brand-new and has no previous versions. This commit fixes it so that API
check against previous version is skipped, in the case of missing
prebuilt_apis.

Note that Current api txt files (placed under api/ directory) are still
needed (make update-api)

Bug: 126259114
Test: tries to build without touching empty api txt files.
Change-Id: I93630f4139cbf502621693ec315dc06c0d07d1c3
diff --git a/java/droiddoc.go b/java/droiddoc.go
index 85e4797..5611791 100644
--- a/java/droiddoc.go
+++ b/java/droiddoc.go
@@ -307,6 +307,10 @@
 		Last_released ApiToCheck
 
 		Current ApiToCheck
+
+		// do not perform API check against Last_released, in the case that both two specified API
+		// files by Last_released are modules which don't exist.
+		Ignore_missing_latest_api *bool `blueprint:"mutated"`
 	}
 
 	// if set to true, generate docs through Dokka instead of Doclava.
@@ -349,6 +353,10 @@
 		Last_released ApiToCheck
 
 		Current ApiToCheck
+
+		// do not perform API check against Last_released, in the case that both two specified API
+		// files by Last_released are modules which don't exist.
+		Ignore_missing_latest_api *bool `blueprint:"mutated"`
 	}
 
 	// user can specify the version of previous released API file in order to do compatibility check.
@@ -427,6 +435,25 @@
 	return false
 }
 
+func ignoreMissingModules(ctx android.BottomUpMutatorContext, apiToCheck *ApiToCheck) {
+	api_file := String(apiToCheck.Api_file)
+	removed_api_file := String(apiToCheck.Removed_api_file)
+
+	api_module := android.SrcIsModule(api_file)
+	removed_api_module := android.SrcIsModule(removed_api_file)
+
+	if api_module == "" || removed_api_module == "" {
+		return
+	}
+
+	if ctx.OtherModuleExists(api_module) || ctx.OtherModuleExists(removed_api_module) {
+		return
+	}
+
+	apiToCheck.Api_file = nil
+	apiToCheck.Removed_api_file = nil
+}
+
 type ApiFilePath interface {
 	ApiFilePath() android.Path
 }
@@ -839,6 +866,10 @@
 func (d *Droiddoc) DepsMutator(ctx android.BottomUpMutatorContext) {
 	d.Javadoc.addDeps(ctx)
 
+	if Bool(d.properties.Check_api.Ignore_missing_latest_api) {
+		ignoreMissingModules(ctx, &d.properties.Check_api.Last_released)
+	}
+
 	if String(d.properties.Custom_template) != "" {
 		ctx.AddDependency(ctx.Module(), droiddocTemplateTag, String(d.properties.Custom_template))
 	}
@@ -1283,6 +1314,10 @@
 func (d *Droidstubs) DepsMutator(ctx android.BottomUpMutatorContext) {
 	d.Javadoc.addDeps(ctx)
 
+	if Bool(d.properties.Check_api.Ignore_missing_latest_api) {
+		ignoreMissingModules(ctx, &d.properties.Check_api.Last_released)
+	}
+
 	if apiCheckEnabled(d.properties.Check_api.Current, "current") {
 		android.ExtractSourceDeps(ctx, d.properties.Check_api.Current.Api_file)
 		android.ExtractSourceDeps(ctx, d.properties.Check_api.Current.Removed_api_file)
diff --git a/java/sdk_library.go b/java/sdk_library.go
index df4e08b..6441c63 100644
--- a/java/sdk_library.go
+++ b/java/sdk_library.go
@@ -454,8 +454,9 @@
 		Merge_annotations_dirs           []string
 		Merge_inclusion_annotations_dirs []string
 		Check_api                        struct {
-			Current       ApiToCheck
-			Last_released ApiToCheck
+			Current                   ApiToCheck
+			Last_released             ApiToCheck
+			Ignore_missing_latest_api *bool
 		}
 		Aidl struct {
 			Include_dirs       []string
@@ -524,6 +525,7 @@
 		module.latestApiFilegroupName(apiScope))
 	props.Check_api.Last_released.Removed_api_file = proptools.StringPtr(
 		module.latestRemovedApiFilegroupName(apiScope))
+	props.Check_api.Ignore_missing_latest_api = proptools.BoolPtr(true)
 	props.Srcs_lib = module.sdkLibraryProperties.Srcs_lib
 	props.Srcs_lib_whitelist_dirs = module.sdkLibraryProperties.Srcs_lib_whitelist_dirs
 	props.Srcs_lib_whitelist_pkgs = module.sdkLibraryProperties.Srcs_lib_whitelist_pkgs