Add annotations.zip support to java_sdk_library
The annotations zip file is produced by the "main" sdk build and is
primarily consumed by android studio.
In order to support building the main SDK without requiring the sources
of all modules, we are adding module SDK artifacts that allows
reconstructing these outputs. The annotations zip contains XML files
which should be fairly easy to merge from all the individual parts.
Bug: 187397779
Test: unit tests in this CL
Test: m sdkextensions-sdk and inspect output
Merged-In: I955cae720e6f1382936836ee1d8fb11003f51b7d
Change-Id: I955cae720e6f1382936836ee1d8fb11003f51b7d
diff --git a/java/droidstubs.go b/java/droidstubs.go
index c756815..9d383ce 100644
--- a/java/droidstubs.go
+++ b/java/droidstubs.go
@@ -153,6 +153,7 @@
// Provider of information about API stubs, used by java_sdk_library.
type ApiStubsProvider interface {
+ AnnotationsZip() android.Path
ApiFilePath
RemovedApiFilePath() android.Path
@@ -207,6 +208,10 @@
}
}
+func (d *Droidstubs) AnnotationsZip() android.Path {
+ return d.annotationsZip
+}
+
func (d *Droidstubs) ApiFilePath() android.Path {
return d.apiFilePath
}
diff --git a/java/sdk_library.go b/java/sdk_library.go
index 12353c9..751a71d 100644
--- a/java/sdk_library.go
+++ b/java/sdk_library.go
@@ -550,6 +550,9 @@
// The stubs source jar.
stubsSrcJar android.OptionalPath
+
+ // Extracted annotations.
+ annotationsZip android.OptionalPath
}
func (paths *scopePaths) extractStubsLibraryInfoFromDependency(ctx android.ModuleContext, dep android.Module) error {
@@ -585,6 +588,7 @@
}
func (paths *scopePaths) extractApiInfoFromApiStubsProvider(provider ApiStubsProvider) {
+ paths.annotationsZip = android.OptionalPathForPath(provider.AnnotationsZip())
paths.currentApiFilePath = android.OptionalPathForPath(provider.ApiFilePath())
paths.removedApiFilePath = android.OptionalPathForPath(provider.RemovedApiFilePath())
}
@@ -739,6 +743,8 @@
apiTxtComponentName = "api.txt"
removedApiTxtComponentName = "removed-api.txt"
+
+ annotationsComponentName = "annotations.zip"
)
// A regular expression to match tags that reference a specific stubs component.
@@ -757,7 +763,7 @@
scopesRegexp := choice(allScopeNames...)
// Regular expression to match one of the components.
- componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName)
+ componentsRegexp := choice(stubsSourceComponentName, apiTxtComponentName, removedApiTxtComponentName, annotationsComponentName)
// Regular expression to match any combination of one scope and one component.
return regexp.MustCompile(fmt.Sprintf(`^\.(%s)\.(%s)$`, scopesRegexp, componentsRegexp))
@@ -765,9 +771,7 @@
// For OutputFileProducer interface
//
-// .<scope>.stubs.source
-// .<scope>.api.txt
-// .<scope>.removed-api.txt
+// .<scope>.<component name>, for all ComponentNames (for example: .public.removed-api.txt)
func (c *commonToSdkLibraryAndImport) commonOutputFiles(tag string) (android.Paths, error) {
if groups := tagSplitter.FindStringSubmatch(tag); groups != nil {
scopeName := groups[1]
@@ -794,6 +798,11 @@
if paths.removedApiFilePath.Valid() {
return android.Paths{paths.removedApiFilePath.Path()}, nil
}
+
+ case annotationsComponentName:
+ if paths.annotationsZip.Valid() {
+ return android.Paths{paths.annotationsZip.Path()}, nil
+ }
}
return nil, fmt.Errorf("%s not available for api scope %s", component, scopeName)
@@ -1888,6 +1897,9 @@
// The removed.txt
Removed_api *string `android:"path"`
+
+ // Annotation zip
+ Annotations *string `android:"path"`
}
type sdkLibraryImportProperties struct {
@@ -2189,6 +2201,7 @@
}
paths := module.getScopePathsCreateIfNeeded(apiScope)
+ paths.annotationsZip = android.OptionalPathForModuleSrc(ctx, scopeProperties.Annotations)
paths.currentApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Current_api)
paths.removedApiFilePath = android.OptionalPathForModuleSrc(ctx, scopeProperties.Removed_api)
}
@@ -2529,6 +2542,7 @@
StubsSrcJar android.Path
CurrentApiFile android.Path
RemovedApiFile android.Path
+ AnnotationsZip android.Path
SdkVersion string
}
@@ -2554,6 +2568,10 @@
if paths.removedApiFilePath.Valid() {
properties.RemovedApiFile = paths.removedApiFilePath.Path()
}
+ // The annotations zip is only available for modules that set annotations_enabled: true.
+ if paths.annotationsZip.Valid() {
+ properties.AnnotationsZip = paths.annotationsZip.Path()
+ }
s.Scopes[apiScope] = properties
}
}
@@ -2618,6 +2636,12 @@
scopeSet.AddProperty("removed_api", removedApiSnapshotPath)
}
+ if properties.AnnotationsZip != nil {
+ annotationsSnapshotPath := filepath.Join(scopeDir, ctx.Name()+"_annotations.zip")
+ ctx.SnapshotBuilder().CopyToSnapshot(properties.AnnotationsZip, annotationsSnapshotPath)
+ scopeSet.AddProperty("annotations", annotationsSnapshotPath)
+ }
+
if properties.SdkVersion != "" {
scopeSet.AddProperty("sdk_version", properties.SdkVersion)
}
diff --git a/java/sdk_library_test.go b/java/sdk_library_test.go
index 65af953..3cf4987 100644
--- a/java/sdk_library_test.go
+++ b/java/sdk_library_test.go
@@ -247,7 +247,7 @@
}
}
-func TestJavaSdkLibrary_UseSourcesFromAnotherSdkLibrary(t *testing.T) {
+func TestJavaSdkLibrary_AccessOutputFiles(t *testing.T) {
android.GroupFixturePreparers(
prepareForJavaTest,
PrepareForTestWithJavaSdkLibraryFiles,
@@ -257,6 +257,31 @@
name: "foo",
srcs: ["a.java"],
api_packages: ["foo"],
+ annotations_enabled: true,
+ public: {
+ enabled: true,
+ },
+ }
+ java_library {
+ name: "bar",
+ srcs: ["b.java", ":foo{.public.stubs.source}"],
+ java_resources: [":foo{.public.annotations.zip}"],
+ }
+ `)
+}
+
+func TestJavaSdkLibrary_AccessOutputFiles_NoAnnotations(t *testing.T) {
+ android.GroupFixturePreparers(
+ prepareForJavaTest,
+ PrepareForTestWithJavaSdkLibraryFiles,
+ FixtureWithLastReleaseApis("foo"),
+ ).
+ ExtendWithErrorHandler(android.FixtureExpectsAtLeastOneErrorMatchingPattern(`module "bar" variant "android_common": path dependency ":foo{.public.annotations.zip}": annotations.zip not available for api scope public`)).
+ RunTestWithBp(t, `
+ java_sdk_library {
+ name: "foo",
+ srcs: ["a.java"],
+ api_packages: ["foo"],
public: {
enabled: true,
},
@@ -265,6 +290,7 @@
java_library {
name: "bar",
srcs: ["b.java", ":foo{.public.stubs.source}"],
+ java_resources: [":foo{.public.annotations.zip}"],
}
`)
}
@@ -328,6 +354,7 @@
stub_srcs: ["a.java"],
current_api: "api/current.txt",
removed_api: "api/removed.txt",
+ annotations: "x/annotations.zip",
},
}
@@ -337,6 +364,7 @@
java_resources: [
":foo{.public.api.txt}",
":foo{.public.removed-api.txt}",
+ ":foo{.public.annotations.zip}",
],
}
`)
diff --git a/java/testing.go b/java/testing.go
index 8860b45..d5c464d 100644
--- a/java/testing.go
+++ b/java/testing.go
@@ -280,6 +280,7 @@
"kotlin-stdlib-jdk7",
"kotlin-stdlib-jdk8",
"kotlin-annotations",
+ "stub-annotations",
}
for _, extra := range extraModules {