Add exclude_files and exclude_dirs properties to java_import
Prebuilt jars sometime contain files that we don't want. In Make we would delete everything in META-INF when importing jars, but that caused problems when there were necessary files in there, so we added LOCAL_DONT_DELETE_JAR_META_INF. Soong does the opposite, keeping everything by default. Add properties to allow explicitly stripping unwanted files instead. Bug: 111389216 Test: m checkbuild Change-Id: I6d07f519ebc7d0e1bf0af93416bb569e3c2b1500
This commit is contained in:
@@ -318,7 +318,8 @@ func TransformResourcesToJar(ctx android.ModuleContext, outputFile android.Writa
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TransformJarsToJar(ctx android.ModuleContext, outputFile android.WritablePath, desc string,
|
func TransformJarsToJar(ctx android.ModuleContext, outputFile android.WritablePath, desc string,
|
||||||
jars android.Paths, manifest android.OptionalPath, stripDirs bool, dirsToStrip []string) {
|
jars android.Paths, manifest android.OptionalPath, stripDirEntries bool, filesToStrip []string,
|
||||||
|
dirsToStrip []string) {
|
||||||
|
|
||||||
var deps android.Paths
|
var deps android.Paths
|
||||||
|
|
||||||
@@ -328,22 +329,19 @@ func TransformJarsToJar(ctx android.ModuleContext, outputFile android.WritablePa
|
|||||||
deps = append(deps, manifest.Path())
|
deps = append(deps, manifest.Path())
|
||||||
}
|
}
|
||||||
|
|
||||||
if dirsToStrip != nil {
|
|
||||||
for _, dir := range dirsToStrip {
|
for _, dir := range dirsToStrip {
|
||||||
jarArgs = append(jarArgs, "-stripDir ", dir)
|
jarArgs = append(jarArgs, "-stripDir ", dir)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, file := range filesToStrip {
|
||||||
|
jarArgs = append(jarArgs, "-stripFile ", file)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove any module-info.class files that may have come from prebuilt jars, they cause problems
|
// Remove any module-info.class files that may have come from prebuilt jars, they cause problems
|
||||||
// for downstream tools like desugar.
|
// for downstream tools like desugar.
|
||||||
jarArgs = append(jarArgs, "-stripFile module-info.class")
|
jarArgs = append(jarArgs, "-stripFile module-info.class")
|
||||||
|
|
||||||
// Remove any kotlin-reflect related files
|
if stripDirEntries {
|
||||||
// TODO(pszczepaniak): Support kotlin-reflect
|
|
||||||
jarArgs = append(jarArgs, "-stripFile \"*.kotlin_module\"")
|
|
||||||
jarArgs = append(jarArgs, "-stripFile \"*.kotlin_builtin\"")
|
|
||||||
|
|
||||||
if stripDirs {
|
|
||||||
jarArgs = append(jarArgs, "-D")
|
jarArgs = append(jarArgs, "-D")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
33
java/java.go
33
java/java.go
@@ -995,6 +995,8 @@ func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var stripFiles []string
|
||||||
|
|
||||||
if srcFiles.HasExt(".kt") {
|
if srcFiles.HasExt(".kt") {
|
||||||
// If there are kotlin files, compile them first but pass all the kotlin and java files
|
// If there are kotlin files, compile them first but pass all the kotlin and java files
|
||||||
// kotlinc will use the java files to resolve types referenced by the kotlin files, but
|
// kotlinc will use the java files to resolve types referenced by the kotlin files, but
|
||||||
@@ -1027,9 +1029,13 @@ func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path
|
|||||||
// Jar kotlin classes into the final jar after javac
|
// Jar kotlin classes into the final jar after javac
|
||||||
jars = append(jars, kotlinJar)
|
jars = append(jars, kotlinJar)
|
||||||
|
|
||||||
// Don't add kotlin-stdlib if using (on-device) renamed stdlib
|
if Bool(j.properties.Renamed_kotlin_stdlib) {
|
||||||
|
// Remove any kotlin-reflect related files
|
||||||
|
// TODO(pszczepaniak): Support kotlin-reflect
|
||||||
|
stripFiles = append(stripFiles, "*.kotlin_module", "*.kotlin_builtin")
|
||||||
|
} else {
|
||||||
|
// Only add kotlin-stdlib if not using (on-device) renamed stdlib
|
||||||
// (it's expected to be on device bootclasspath)
|
// (it's expected to be on device bootclasspath)
|
||||||
if !Bool(j.properties.Renamed_kotlin_stdlib) {
|
|
||||||
jars = append(jars, deps.kotlinStdlib...)
|
jars = append(jars, deps.kotlinStdlib...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1143,7 +1149,8 @@ func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path
|
|||||||
outputFile = jars[0]
|
outputFile = jars[0]
|
||||||
} else {
|
} else {
|
||||||
combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
|
combinedJar := android.PathForModuleOut(ctx, "combined", jarName)
|
||||||
TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest, false, nil)
|
TransformJarsToJar(ctx, combinedJar, "for javac", jars, manifest,
|
||||||
|
false, stripFiles, nil)
|
||||||
outputFile = combinedJar
|
outputFile = combinedJar
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1219,7 +1226,8 @@ func (j *Module) compileJavaHeader(ctx android.ModuleContext, srcFiles, srcJars
|
|||||||
// we cannot skip the combine step for now if there is only one jar
|
// we cannot skip the combine step for now if there is only one jar
|
||||||
// since we have to strip META-INF/TRANSITIVE dir from turbine.jar
|
// since we have to strip META-INF/TRANSITIVE dir from turbine.jar
|
||||||
combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
|
combinedJar := android.PathForModuleOut(ctx, "turbine-combined", jarName)
|
||||||
TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{}, false, []string{"META-INF"})
|
TransformJarsToJar(ctx, combinedJar, "for turbine", jars, android.OptionalPath{},
|
||||||
|
false, nil, []string{"META-INF"})
|
||||||
headerJar = combinedJar
|
headerJar = combinedJar
|
||||||
|
|
||||||
if j.properties.Jarjar_rules != nil {
|
if j.properties.Jarjar_rules != nil {
|
||||||
@@ -1477,6 +1485,12 @@ type ImportProperties struct {
|
|||||||
|
|
||||||
// List of shared java libs that this module has dependencies to
|
// List of shared java libs that this module has dependencies to
|
||||||
Libs []string
|
Libs []string
|
||||||
|
|
||||||
|
// List of files to remove from the jar file(s)
|
||||||
|
Exclude_files []string
|
||||||
|
|
||||||
|
// List of directories to remove from the jar file(s)
|
||||||
|
Exclude_dirs []string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Import struct {
|
type Import struct {
|
||||||
@@ -1485,7 +1499,6 @@ type Import struct {
|
|||||||
|
|
||||||
properties ImportProperties
|
properties ImportProperties
|
||||||
|
|
||||||
classpathFiles android.Paths
|
|
||||||
combinedClasspathFile android.Path
|
combinedClasspathFile android.Path
|
||||||
exportedSdkLibs []string
|
exportedSdkLibs []string
|
||||||
}
|
}
|
||||||
@@ -1511,14 +1524,16 @@ func (j *Import) Name() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
|
func (j *Import) DepsMutator(ctx android.BottomUpMutatorContext) {
|
||||||
|
android.ExtractSourcesDeps(ctx, j.properties.Jars)
|
||||||
ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...)
|
ctx.AddDependency(ctx.Module(), libTag, j.properties.Libs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
|
func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
|
||||||
j.classpathFiles = android.PathsForModuleSrc(ctx, j.properties.Jars)
|
jars := ctx.ExpandSources(j.properties.Jars, nil)
|
||||||
|
|
||||||
outputFile := android.PathForModuleOut(ctx, "classes.jar")
|
outputFile := android.PathForModuleOut(ctx, "classes.jar")
|
||||||
TransformJarsToJar(ctx, outputFile, "for prebuilts", j.classpathFiles, android.OptionalPath{}, false, nil)
|
TransformJarsToJar(ctx, outputFile, "for prebuilts", jars, android.OptionalPath{},
|
||||||
|
false, j.properties.Exclude_files, j.properties.Exclude_dirs)
|
||||||
j.combinedClasspathFile = outputFile
|
j.combinedClasspathFile = outputFile
|
||||||
|
|
||||||
ctx.VisitDirectDeps(func(module android.Module) {
|
ctx.VisitDirectDeps(func(module android.Module) {
|
||||||
@@ -1547,11 +1562,11 @@ func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) {
|
|||||||
var _ Dependency = (*Import)(nil)
|
var _ Dependency = (*Import)(nil)
|
||||||
|
|
||||||
func (j *Import) HeaderJars() android.Paths {
|
func (j *Import) HeaderJars() android.Paths {
|
||||||
return j.classpathFiles
|
return android.Paths{j.combinedClasspathFile}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *Import) ImplementationJars() android.Paths {
|
func (j *Import) ImplementationJars() android.Paths {
|
||||||
return j.classpathFiles
|
return android.Paths{j.combinedClasspathFile}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (j *Import) AidlIncludeDirs() android.Paths {
|
func (j *Import) AidlIncludeDirs() android.Paths {
|
||||||
|
@@ -598,14 +598,15 @@ func TestPrebuilts(t *testing.T) {
|
|||||||
|
|
||||||
javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
|
javac := ctx.ModuleForTests("foo", "android_common").Rule("javac")
|
||||||
combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
|
combineJar := ctx.ModuleForTests("foo", "android_common").Description("for javac")
|
||||||
|
barJar := ctx.ModuleForTests("bar", "android_common").Rule("combineJar").Output
|
||||||
|
bazJar := ctx.ModuleForTests("baz", "android_common").Rule("combineJar").Output
|
||||||
|
|
||||||
bar := "a.jar"
|
if !strings.Contains(javac.Args["classpath"], barJar.String()) {
|
||||||
if !strings.Contains(javac.Args["classpath"], bar) {
|
t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], barJar.String())
|
||||||
t.Errorf("foo classpath %v does not contain %q", javac.Args["classpath"], bar)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != "b.jar" {
|
if len(combineJar.Inputs) != 2 || combineJar.Inputs[1].String() != bazJar.String() {
|
||||||
t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, "b.jar")
|
t.Errorf("foo combineJar inputs %v does not contain %q", combineJar.Inputs, bazJar.String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user