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

Commit 85390231 authored by Sasha Smundak's avatar Sasha Smundak Committed by Jaewoong Jung
Browse files

Implement android_app_set module

Bug: 152319766
Test: manual and builtin
Change-Id: Id0877476f9ae23311d92c0b59a9c568140ab4119
Merged-In: Id0877476f9ae23311d92c0b59a9c568140ab4119
parent 97d511a1
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -1053,3 +1053,11 @@ func (c *config) ProductHiddenAPIStubsTest() []string {
func (c *deviceConfig) TargetFSConfigGen() []string {
	return c.config.productVariables.TargetFSConfigGen
}

func (c *deviceConfig) DeviceArch() string {
	return String(c.config.productVariables.DeviceArch)
}

func (c *deviceConfig) DeviceSecondaryArch() string {
	return String(c.config.productVariables.DeviceSecondaryArch)
}
+17 −0
Original line number Diff line number Diff line
@@ -595,3 +595,20 @@ func androidMkWriteTestData(data android.Paths, ret *android.AndroidMkData) {
		})
	}
}

func (apkSet *AndroidAppSet) AndroidMk() android.AndroidMkData {
	return android.AndroidMkData{
		Class:      "APPS",
		OutputFile: android.OptionalPathForPath(apkSet.packedOutput),
		Include:    "$(BUILD_SYSTEM)/soong_android_app_set.mk",
		Extra: []android.AndroidMkExtraFunc{
			func(w io.Writer, outputFile android.Path) {
				if apkSet.Privileged() {
					fmt.Fprintln(w, "LOCAL_PRIVILEGED_MODULE := true")
				}
				fmt.Fprintln(w, "LOCAL_APK_SET_MASTER_FILE := ", apkSet.masterFile)
				fmt.Fprintln(w, "LOCAL_OVERRIDES_PACKAGES :=", strings.Join(apkSet.properties.Overrides, " "))
			},
		},
	}
}
+122 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package java
import (
	"path/filepath"
	"sort"
	"strconv"
	"strings"

	"github.com/google/blueprint"
@@ -35,6 +36,127 @@ func init() {
	android.RegisterModuleType("android_test_helper_app", AndroidTestHelperAppFactory)
	android.RegisterModuleType("android_app_certificate", AndroidAppCertificateFactory)
	android.RegisterModuleType("override_android_app", OverrideAndroidAppModuleFactory)
	android.RegisterModuleType("android_app_set", AndroidApkSetFactory)
}

type AndroidAppSetProperties struct {
	// APK Set path
	Set string

	// Specifies that this app should be installed to the priv-app directory,
	// where the system will grant it additional privileges not available to
	// normal apps.
	Privileged *bool

	// APKs in this set use prerelease SDK version
	Prerelease *bool

	// Names of modules to be overridden. Listed modules can only be other apps
	//	(in Make or Soong).
	Overrides []string
}

type AndroidAppSet struct {
	android.ModuleBase
	android.DefaultableModuleBase
	prebuilt android.Prebuilt

	properties   AndroidAppSetProperties
	packedOutput android.WritablePath
	masterFile   string
}

func (as *AndroidAppSet) Name() string {
	return as.prebuilt.Name(as.ModuleBase.Name())
}

func (as *AndroidAppSet) IsInstallable() bool {
	return true
}

func (as *AndroidAppSet) Prebuilt() *android.Prebuilt {
	return &as.prebuilt
}

func (as *AndroidAppSet) Privileged() bool {
	return Bool(as.properties.Privileged)
}

var targetCpuAbi = map[string]string{
	"arm":    "ARMEABI_V7A",
	"arm64":  "ARM64_V8A",
	"x86":    "X86",
	"x86_64": "X86_64",
}

func supportedAbis(ctx android.ModuleContext) []string {
	abiName := func(archVar string, deviceArch string) string {
		if abi, found := targetCpuAbi[deviceArch]; found {
			return abi
		}
		ctx.ModuleErrorf("Invalid %s: %s", archVar, deviceArch)
		return "BAD_ABI"
	}

	result := []string{abiName("TARGET_ARCH", ctx.DeviceConfig().DeviceArch())}
	if s := ctx.DeviceConfig().DeviceSecondaryArch(); s != "" {
		result = append(result, abiName("TARGET_2ND_ARCH", s))
	}
	return result
}

func (as *AndroidAppSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
	as.packedOutput = android.PathForModuleOut(ctx, "extracted.zip")
	// We are assuming here that the master file in the APK
	// set has `.apk` suffix. If it doesn't the build will fail.
	// APK sets containing APEX files are handled elsewhere.
	as.masterFile = ctx.ModuleName() + ".apk"
	screenDensities := "all"
	if dpis := ctx.Config().ProductAAPTPrebuiltDPI(); len(dpis) > 0 {
		screenDensities = strings.ToUpper(strings.Join(dpis, ","))
	}
	// TODO(asmundak): handle locales.
	// TODO(asmundak): do we support device features
	ctx.Build(pctx,
		android.BuildParams{
			Rule:        extractMatchingApks,
			Description: "Extract APKs from APK set",
			Output:      as.packedOutput,
			Inputs:      android.Paths{as.prebuilt.SingleSourcePath(ctx)},
			Args: map[string]string{
				"abis":              strings.Join(supportedAbis(ctx), ","),
				"allow-prereleased": strconv.FormatBool(proptools.Bool(as.properties.Prerelease)),
				"screen-densities":  screenDensities,
				"sdk-version":       ctx.Config().PlatformSdkVersion(),
				"stem":              ctx.ModuleName(),
			},
		})
	// TODO(asmundak): add this (it's wrong now, will cause copying extracted.zip)
	/*
		var installDir android.InstallPath
		if Bool(as.properties.Privileged) {
			installDir = android.PathForModuleInstall(ctx, "priv-app", as.BaseModuleName())
		} else if ctx.InstallInTestcases() {
			installDir = android.PathForModuleInstall(ctx, as.BaseModuleName(), ctx.DeviceConfig().DeviceArch())
		} else {
			installDir = android.PathForModuleInstall(ctx, "app", as.BaseModuleName())
		}
		ctx.InstallFile(installDir, as.masterFile", as.packedOutput)
	*/
}

// android_app_set extracts a set of APKs based on the target device
// configuration and installs this set as "split APKs".
// The set will always contain `base-master.apk` and every APK built
// to the target device. All density-specific APK will be included, too,
// unless PRODUCT_APPT_PREBUILT_DPI is defined (should contain comma-sepearated
// list of density names (LDPI, MDPI, HDPI, etc.)
func AndroidApkSetFactory() android.Module {
	module := &AndroidAppSet{}
	module.AddProperties(&module.properties)
	InitJavaModule(module, android.DeviceSupported)
	android.InitSingleSourcePrebuiltModule(module, &module.properties.Set)
	return module
}

// AndroidManifest.xml merging
+84 −0
Original line number Diff line number Diff line
@@ -134,6 +134,90 @@ func TestAppSplits(t *testing.T) {
	}
}

func TestAndroidAppSet(t *testing.T) {
	config := testConfig(nil)
	config.TestProductVariables.DeviceArch = proptools.StringPtr("arm64")
	ctx := testAppContext(config, `
	android_app_set {
		name: "foo",
		set: "prebuilts/apks/app.apks",
		prerelease: true,
	}`, nil)
	run(t, ctx, config)
	module := ctx.ModuleForTests("foo", "android_common")
	const packedSplitApks = "extracted.zip"
	params := module.Output(packedSplitApks)
	if params.Rule == nil {
		t.Errorf("expected output %s is missing", packedSplitApks)
	}
	if s := params.Args["allow-prereleased"]; s != "true" {
		t.Errorf("wrong allow-prereleased value: '%s', expected 'true'", s)
	}
}

func TestAndroidAppSet_Variants(t *testing.T) {
	bp := `
		android_app_set {
			name: "foo",
			set: "prebuilts/apks/app.apks",
		}`
	testCases := []struct {
		name                string
		deviceArch          *string
		deviceSecondaryArch *string
		aaptPrebuiltDPI     []string
		sdkVersion          int
		expected            map[string]string
	}{
		{
			name:            "One",
			deviceArch:      proptools.StringPtr("x86"),
			aaptPrebuiltDPI: []string{"ldpi", "xxhdpi"},
			sdkVersion:      29,
			expected: map[string]string{
				"abis":              "X86",
				"allow-prereleased": "false",
				"screen-densities":  "LDPI,XXHDPI",
				"sdk-version":       "29",
				"stem":              "foo",
			},
		},
		{
			name:                "Two",
			deviceArch:          proptools.StringPtr("x86_64"),
			deviceSecondaryArch: proptools.StringPtr("x86"),
			aaptPrebuiltDPI:     nil,
			sdkVersion:          30,
			expected: map[string]string{
				"abis":              "X86_64,X86",
				"allow-prereleased": "false",
				"screen-densities":  "all",
				"sdk-version":       "30",
				"stem":              "foo",
			},
		},
	}

	for _, test := range testCases {
		config := testConfig(nil)
		config.TestProductVariables.AAPTPrebuiltDPI = test.aaptPrebuiltDPI
		config.TestProductVariables.Platform_sdk_version = &test.sdkVersion
		config.TestProductVariables.DeviceArch = test.deviceArch
		config.TestProductVariables.DeviceSecondaryArch = test.deviceSecondaryArch
		ctx := testAppContext(config, bp, nil)
		run(t, ctx, config)
		module := ctx.ModuleForTests("foo", "android_common")
		const packedSplitApks = "extracted.zip"
		params := module.Output(packedSplitApks)
		for k, v := range test.expected {
			if actual := params.Args[k]; actual != v {
				t.Errorf("%s: bad build arg value for '%s': '%s', expected '%s'",
					test.name, k, actual, v)
			}
		}
	}
}

func TestResourceDirs(t *testing.T) {
	testCases := []struct {
		name      string
+12 −0
Original line number Diff line number Diff line
@@ -61,6 +61,18 @@ var (
		"javacFlags", "bootClasspath", "classpath", "processorpath", "processor", "srcJars", "srcJarDir",
		"outDir", "annoDir", "javaVersion")

	extractMatchingApks = pctx.StaticRule(
		"extractMatchingApks",
		blueprint.RuleParams{
			Command: `rm -rf "$out" && ` +
				`${config.ExtractApksCmd} -o "${out}" -allow-prereleased=${allow-prereleased} ` +
				`-sdk-version=${sdk-version} -abis=${abis} ` +
				`--screen-densities=${screen-densities} --stem=${stem} ` +
				`${in}`,
			CommandDeps: []string{"${config.ExtractApksCmd}"},
		},
		"abis", "allow-prereleased", "screen-densities", "sdk-version", "stem")

	turbine = pctx.AndroidStaticRule("turbine",
		blueprint.RuleParams{
			Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
Loading