Merge "Add support for transitive resources to android_library_import" into main

This commit is contained in:
Treehugger Robot 2024-04-12 22:04:27 +00:00 committed by Gerrit Code Review
commit c5c54803e6
2 changed files with 39 additions and 12 deletions

View file

@ -978,6 +978,7 @@ type AARImport struct {
headerJarFile android.WritablePath
implementationJarFile android.WritablePath
implementationAndResourcesJarFile android.WritablePath
proguardFlags android.WritablePath
exportPackage android.WritablePath
transitiveAaptResourcePackagesFile android.Path
@ -1013,7 +1014,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)
}
@ -1155,8 +1156,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")
@ -1272,6 +1274,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)
@ -1279,6 +1282,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)
@ -1287,18 +1291,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
@ -1306,9 +1331,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
@ -1346,7 +1372,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())