Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e42c5d96 authored by Spandan Das's avatar Spandan Das
Browse files

Add JNI libs support to device variants of java_binary

This adds jni_libs support back to device variants, which was removed in
https://r.android.com/3230584. These will be installed by the two packaging
systems in different ways
1. Kati: By being listed in `LOCAL_REQUIRED_MODULES` of the
   autogenerated Android.mk
2. Soong: Via the`jniInstallTag`, whose `InstallDepNeeded` value is true

This CL also makes it an error for a native library to be listed in
`required` of a `java_binary(_host)`.

Test: m nothing --no-skip-soong-tests
Test: Verified that there are no diff in
system_intermediates/file_list.txt (device variants)
Test: m installclean && m <host_java_bin> ; verified that the jni
library is installed in out/host/linux-x86/lib64 (host variants)

Bug: 370110572
Change-Id: I6aebc648191ab39f2da15a5f2807d46abab22cb0
parent 32325372
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -313,6 +313,7 @@ func (binary *Binary) AndroidMkEntries() []android.AndroidMkEntries {
			ExtraEntries: []android.AndroidMkExtraEntriesFunc{
				func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
					entries.SetBool("LOCAL_STRIP_MODULE", false)
					entries.AddStrings("LOCAL_REQUIRED_MODULES", binary.androidMkNamesOfJniLibs...)
				},
			},
			ExtraFooters: []android.AndroidMkExtraFootersFunc{
+20 −6
Original line number Diff line number Diff line
@@ -1795,8 +1795,7 @@ type binaryProperties struct {
	// Name of the class containing main to be inserted into the manifest as Main-Class.
	Main_class *string

	// Names of modules containing JNI libraries that should be installed alongside the host
	// variant of the binary.
	// Names of modules containing JNI libraries that should be installed alongside the binary.
	Jni_libs []string `android:"arch_variant"`
}

@@ -1809,6 +1808,8 @@ type Binary struct {

	wrapperFile android.Path
	binaryFile  android.InstallPath

	androidMkNamesOfJniLibs []string
}

func (j *Binary) HostToolPath() android.OptionalPath {
@@ -1880,6 +1881,21 @@ func (j *Binary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
			ctx.ModuleName()+ext, j.wrapperFile)

		setOutputFiles(ctx, j.Library.Module)

		// Set the jniLibs of this binary.
		// These will be added to `LOCAL_REQUIRED_MODULES`, and the kati packaging system will
		// install these alongside the java binary.
		ctx.VisitDirectDepsWithTag(jniInstallTag, func(jni android.Module) {
			// Use the BaseModuleName of the dependency (without any prebuilt_ prefix)
			bmn, _ := jni.(interface{ BaseModuleName() string })
			j.androidMkNamesOfJniLibs = append(j.androidMkNamesOfJniLibs, bmn.BaseModuleName()+":"+jni.Target().Arch.ArchType.Bitness())
		})
		// Check that native libraries are not listed in `required`. Prompt users to use `jni_libs` instead.
		ctx.VisitDirectDepsWithTag(android.RequiredDepTag, func(dep android.Module) {
			if _, hasSharedLibraryInfo := android.OtherModuleProvider(ctx, dep, cc.SharedLibraryInfoProvider); hasSharedLibraryInfo {
				ctx.ModuleErrorf("cc_library %s is no longer supported in `required` of java_binary modules. Please use jni_libs instead.", dep.Name())
			}
		})
	}
}

@@ -1888,11 +1904,9 @@ func (j *Binary) DepsMutator(ctx android.BottomUpMutatorContext) {
		j.deps(ctx)
	}
	// These dependencies ensure the installation rules will install the jar file when the
	// wrapper is installed, and the jni libraries on host when the wrapper is installed.
	if ctx.Arch().ArchType != android.Common && ctx.Os().Class == android.Host {
		ctx.AddVariationDependencies(nil, jniInstallTag, j.binaryProperties.Jni_libs...)
	}
	// wrapper is installed, and the jni libraries when the wrapper is installed.
	if ctx.Arch().ArchType != android.Common {
		ctx.AddVariationDependencies(nil, jniInstallTag, j.binaryProperties.Jni_libs...)
		ctx.AddVariationDependencies(
			[]blueprint.Variation{{Mutator: "arch", Variation: android.CommonArch.String()}},
			binaryInstallTag, ctx.ModuleName())
+2 −2
Original line number Diff line number Diff line
@@ -3102,7 +3102,7 @@ func assertTestOnlyAndTopLevel(t *testing.T, ctx *android.TestResult, expectedTe
	}
}

// Test that a dependency edge is created to the "first" variant of a native library listed in `required` of java_binary
// Test that a dependency edge is created to the matching variant of a native library listed in `jni_libs` of java_binary
func TestNativeRequiredDepOfJavaBinary(t *testing.T) {
	findDepsOfModule := func(ctx *android.TestContext, module android.Module, depName string) []blueprint.Module {
		var ret []blueprint.Module
@@ -3118,7 +3118,7 @@ func TestNativeRequiredDepOfJavaBinary(t *testing.T) {
java_binary {
	name: "myjavabin",
	main_class: "com.android.MyJava",
	required: ["mynativelib"],
	jni_libs: ["mynativelib"],
}
cc_library_shared {
	name: "mynativelib",