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

Commit bb638eba authored by Paul Duffin's avatar Paul Duffin
Browse files

Output min_sdk_version to snapshot

The min_sdk_version was added to the java_import in Tiramisu. This
change will propagate the min_sdk_version set on a java_library to the
java_import snapshot. If possible the min_sdk_version will be resolved
into a numerical version to ensure consistent behavior across build
releases.

Bug: 260560424
Test: m nothing
      BUILD_NUMBER=fixed packages/modules/common/build/mainline_modules_sdks.sh
      # Ran above before and after this change and made sure that only
      # Tiramisu, UpsideDownCake and latest were changed and those changes
      # were the addition of a min_sdk_version property.
      #
      # Ran the following command in tm-mainline-prod:
      #     lunch cf_x86_64_phone-userdebug
      #     m ART_MODULE_BUILD_FROM_SOURCE=false nothing`.
      #
      # It failed with:
      #     module "prebuilt_okhttp-norepackage" variant "android_common": should support min_sdk_version(33) for "AdServicesApk": min_sdk_version is not specified.
      #
      # Unpacked the Tiramisu art snapshot generated with this change into
      # tm-mainline-prod and reran the previous command and it succeeded.
Change-Id: I9d9d730845554fc175d17f38c038e4e3c7d39e07
parent 9fd9129b
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -764,6 +764,9 @@ type librarySdkMemberProperties struct {
	// The list of permitted packages that need to be passed to the prebuilts as they are used to
	// create the updatable-bcp-packages.txt file.
	PermittedPackages []string

	// The value of the min_sdk_version property, translated into a number where possible.
	MinSdkVersion *string `supported_build_releases:"Tiramisu+"`
}

func (p *librarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
@@ -774,6 +777,13 @@ func (p *librarySdkMemberProperties) PopulateFromVariant(ctx android.SdkMemberCo
	p.AidlIncludeDirs = j.AidlIncludeDirs()

	p.PermittedPackages = j.PermittedPackagesForUpdatableBootJars()

	// If the min_sdk_version was set then add the canonical representation of the API level to the
	// snapshot.
	if j.deviceProperties.Min_sdk_version != nil {
		canonical := android.ReplaceFinalizedCodenames(ctx.SdkModuleContext().Config(), j.minSdkVersion.ApiLevel.String())
		p.MinSdkVersion = proptools.StringPtr(canonical)
	}
}

func (p *librarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
@@ -792,6 +802,10 @@ func (p *librarySdkMemberProperties) AddToPropertySet(ctx android.SdkMemberConte
		propertySet.AddProperty("jars", []string{snapshotRelativeJavaLibPath})
	}

	if p.MinSdkVersion != nil {
		propertySet.AddProperty("min_sdk_version", *p.MinSdkVersion)
	}

	if len(p.PermittedPackages) > 0 {
		propertySet.AddProperty("permitted_packages", p.PermittedPackages)
	}
+2 −0
Original line number Diff line number Diff line
@@ -358,6 +358,7 @@ java_import {
    visibility: ["//visibility:public"],
    apex_available: ["myapex"],
    jars: ["java_boot_libs/snapshot/jars/are/invalid/mybootlib.jar"],
    min_sdk_version: "2",
    permitted_packages: ["mybootlib"],
}

@@ -877,6 +878,7 @@ java_import {
    visibility: ["//visibility:public"],
    apex_available: ["myapex"],
    jars: ["java_boot_libs/snapshot/jars/are/invalid/mybootlib.jar"],
    min_sdk_version: "1",
    permitted_packages: ["mybootlib"],
}

+67 −0
Original line number Diff line number Diff line
@@ -352,6 +352,73 @@ java_import {
	})
}

func TestSnapshotWithJavaLibrary_MinSdkVersion(t *testing.T) {
	runTest := func(t *testing.T, targetBuildRelease, minSdkVersion, expectedMinSdkVersion string) {
		result := android.GroupFixturePreparers(
			prepareForSdkTestWithJava,
			android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
				variables.Platform_version_active_codenames = []string{"S", "Tiramisu", "Unfinalized"}
			}),
			android.FixtureMergeEnv(map[string]string{
				"SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE": targetBuildRelease,
			}),
		).RunTestWithBp(t, fmt.Sprintf(`
		sdk {
			name: "mysdk",
			java_header_libs: ["mylib"],
		}

		java_library {
			name: "mylib",
			srcs: ["Test.java"],
			system_modules: "none",
			sdk_version: "none",
			compile_dex: true,
			min_sdk_version: "%s",
		}
	`, minSdkVersion))

		expectedMinSdkVersionLine := ""
		if expectedMinSdkVersion != "" {
			expectedMinSdkVersionLine = fmt.Sprintf("    min_sdk_version: %q,\n", expectedMinSdkVersion)
		}

		CheckSnapshot(t, result, "mysdk", "",
			checkAndroidBpContents(fmt.Sprintf(`
// This is auto-generated. DO NOT EDIT.

java_import {
    name: "mylib",
    prefer: false,
    visibility: ["//visibility:public"],
    apex_available: ["//apex_available:platform"],
    jars: ["java/mylib.jar"],
%s}
`, expectedMinSdkVersionLine)),
		)
	}

	t.Run("min_sdk_version=S in S", func(t *testing.T) {
		// min_sdk_version was not added to java_import until Tiramisu.
		runTest(t, "S", "S", "")
	})

	t.Run("min_sdk_version=S in Tiramisu", func(t *testing.T) {
		// The canonical form of S is 31.
		runTest(t, "Tiramisu", "S", "31")
	})

	t.Run("min_sdk_version=24 in Tiramisu", func(t *testing.T) {
		// A numerical min_sdk_version is already in canonical form.
		runTest(t, "Tiramisu", "24", "24")
	})

	t.Run("min_sdk_version=Unfinalized in latest", func(t *testing.T) {
		// An unfinalized min_sdk_version has no numeric value yet.
		runTest(t, "", "Unfinalized", "Unfinalized")
	})
}

func TestSnapshotWithJavaSystemserverLibrary(t *testing.T) {
	result := android.GroupFixturePreparers(
		prepareForSdkTestWithJava,
+2 −0
Original line number Diff line number Diff line
@@ -120,6 +120,7 @@ java_import {
    visibility: ["//visibility:public"],
    apex_available: ["myapex"],
    jars: ["java_systemserver_libs/snapshot/jars/are/invalid/mylib.jar"],
    min_sdk_version: "2",
    permitted_packages: ["mylib"],
}

@@ -181,6 +182,7 @@ java_import {
    visibility: ["//visibility:public"],
    apex_available: ["myapex"],
    jars: ["java_systemserver_libs/snapshot/jars/are/invalid/mylib.jar"],
    min_sdk_version: "2",
    permitted_packages: ["mylib"],
}