Add support for transitive resources to android_library_import

Collect and propagate resource jars from static dependencies of
android_library_import modules.

Bug: 288358614
Test: TestAndroidLibraryOutputFilesRel
Change-Id: I076f3c8e4ce42f71a52b612b77eb0265fa1e974c
This commit is contained in:
Colin Cross 2024-04-12 13:44:45 -07:00
parent 5e87f349b8
commit 607bbd64dd
2 changed files with 39 additions and 12 deletions

View file

@ -976,6 +976,7 @@ type AARImport struct {
headerJarFile android.WritablePath
implementationJarFile android.WritablePath
implementationAndResourcesJarFile android.WritablePath
proguardFlags android.WritablePath
exportPackage android.WritablePath
transitiveAaptResourcePackagesFile android.Path
@ -1011,7 +1012,7 @@ func (a *AARImport) OutputFiles(tag string) (android.Paths, error) {
case ".aar":
return []android.Path{a.aarPath}, nil
case "":
return []android.Path{a.implementationJarFile}, nil
return []android.Path{a.implementationAndResourcesJarFile}, nil
default:
return nil, fmt.Errorf("unsupported module reference tag %q", tag)
}
@ -1153,8 +1154,9 @@ func (a *AARImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
TransformJetifier(ctx, a.aarPath.(android.WritablePath), inputFile)
}
jarName := ctx.ModuleName() + ".jar"
extractedAARDir := android.PathForModuleOut(ctx, "aar")
classpathFile := extractedAARDir.Join(ctx, ctx.ModuleName()+".jar")
classpathFile := extractedAARDir.Join(ctx, jarName)
a.manifest = extractedAARDir.Join(ctx, "AndroidManifest.xml")
a.rTxt = extractedAARDir.Join(ctx, "R.txt")
a.assetsPackage = android.PathForModuleOut(ctx, "assets.zip")
@ -1264,6 +1266,7 @@ func (a *AARImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
var staticJars android.Paths
var staticHeaderJars android.Paths
var staticResourceJars android.Paths
ctx.VisitDirectDeps(func(module android.Module) {
if dep, ok := android.OtherModuleProvider(ctx, module, JavaInfoProvider); ok {
tag := ctx.OtherModuleDependencyTag(module)
@ -1271,6 +1274,7 @@ func (a *AARImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
case staticLibTag:
staticJars = append(staticJars, dep.ImplementationJars...)
staticHeaderJars = append(staticHeaderJars, dep.HeaderJars...)
staticResourceJars = append(staticResourceJars, dep.ResourceJars...)
}
}
addCLCFromDep(ctx, module, a.classLoaderContexts)
@ -1279,18 +1283,39 @@ func (a *AARImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
var implementationJarFile android.OutputPath
if len(staticJars) > 0 {
combineJars := append(android.Paths{classpathFile}, staticJars...)
implementationJarFile = android.PathForModuleOut(ctx, "combined", ctx.ModuleName()+".jar").OutputPath
implementationJarFile = android.PathForModuleOut(ctx, "combined", jarName).OutputPath
TransformJarsToJar(ctx, implementationJarFile, "combine", combineJars, android.OptionalPath{}, false, nil, nil)
} else {
implementationJarFile = classpathFile
}
var resourceJarFile android.Path
if len(staticResourceJars) > 1 {
combinedJar := android.PathForModuleOut(ctx, "res-combined", jarName)
TransformJarsToJar(ctx, combinedJar, "for resources", staticResourceJars, android.OptionalPath{},
false, nil, nil)
resourceJarFile = combinedJar
} else if len(staticResourceJars) == 1 {
resourceJarFile = staticResourceJars[0]
}
// merge implementation jar with resources if necessary
implementationAndResourcesJar := implementationJarFile
if resourceJarFile != nil {
jars := android.Paths{resourceJarFile, implementationAndResourcesJar}
combinedJar := android.PathForModuleOut(ctx, "withres", jarName).OutputPath
TransformJarsToJar(ctx, combinedJar, "for resources", jars, android.OptionalPath{},
false, nil, nil)
implementationAndResourcesJar = combinedJar
}
a.implementationJarFile = implementationJarFile
// Save the output file with no relative path so that it doesn't end up in a subdirectory when used as a resource
a.implementationJarFile = implementationJarFile.WithoutRel()
a.implementationAndResourcesJarFile = implementationAndResourcesJar.WithoutRel()
if len(staticHeaderJars) > 0 {
combineJars := append(android.Paths{classpathFile}, staticHeaderJars...)
a.headerJarFile = android.PathForModuleOut(ctx, "turbine-combined", ctx.ModuleName()+".jar")
a.headerJarFile = android.PathForModuleOut(ctx, "turbine-combined", jarName)
TransformJarsToJar(ctx, a.headerJarFile, "combine header jars", combineJars, android.OptionalPath{}, false, nil, nil)
} else {
a.headerJarFile = classpathFile
@ -1298,9 +1323,10 @@ func (a *AARImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
android.SetProvider(ctx, JavaInfoProvider, JavaInfo{
HeaderJars: android.PathsIfNonNil(a.headerJarFile),
ResourceJars: android.PathsIfNonNil(resourceJarFile),
TransitiveLibsHeaderJars: a.transitiveLibsHeaderJars,
TransitiveStaticLibsHeaderJars: a.transitiveStaticLibsHeaderJars,
ImplementationAndResourcesJars: android.PathsIfNonNil(a.implementationJarFile),
ImplementationAndResourcesJars: android.PathsIfNonNil(a.implementationAndResourcesJarFile),
ImplementationJars: android.PathsIfNonNil(a.implementationJarFile),
StubsLinkType: Implementation,
// TransitiveAconfigFiles: // TODO(b/289117800): LOCAL_ACONFIG_FILES for prebuilts
@ -1338,7 +1364,7 @@ func (a *AARImport) HeaderJars() android.Paths {
}
func (a *AARImport) ImplementationAndResourcesJars() android.Paths {
return android.Paths{a.implementationJarFile}
return android.Paths{a.implementationAndResourcesJarFile}
}
func (a *AARImport) DexJarBuildPath(ctx android.ModuleErrorfContext) OptionalDexJarPath {

View file

@ -136,18 +136,19 @@ func TestAndroidLibraryOutputFilesRel(t *testing.T) {
android_library {
name: "foo",
srcs: ["a.java"],
java_resources: ["foo.txt"],
}
android_library_import {
name: "bar",
aars: ["bar.aar"],
aars: ["bar_prebuilt.aar"],
}
android_library_import {
name: "baz",
aars: ["baz.aar"],
static_libs: ["bar"],
aars: ["baz_prebuilt.aar"],
static_libs: ["foo", "bar"],
}
`)
@ -160,11 +161,11 @@ func TestAndroidLibraryOutputFilesRel(t *testing.T) {
bazOutputPath := android.OutputFileForModule(android.PathContext(nil), baz.Module(), "")
android.AssertPathRelativeToTopEquals(t, "foo output path",
"out/soong/.intermediates/foo/android_common/javac/foo.jar", fooOutputPath)
"out/soong/.intermediates/foo/android_common/withres/foo.jar", fooOutputPath)
android.AssertPathRelativeToTopEquals(t, "bar output path",
"out/soong/.intermediates/bar/android_common/aar/bar.jar", barOutputPath)
android.AssertPathRelativeToTopEquals(t, "baz output path",
"out/soong/.intermediates/baz/android_common/combined/baz.jar", bazOutputPath)
"out/soong/.intermediates/baz/android_common/withres/baz.jar", bazOutputPath)
android.AssertStringEquals(t, "foo relative output path",
"foo.jar", fooOutputPath.Rel())