aboutsummaryrefslogtreecommitdiffstats
path: root/java/app.go
diff options
context:
space:
mode:
Diffstat (limited to 'java/app.go')
-rw-r--r--java/app.go389
1 files changed, 341 insertions, 48 deletions
diff --git a/java/app.go b/java/app.go
index ad672ead..fdea4350 100644
--- a/java/app.go
+++ b/java/app.go
@@ -17,24 +17,29 @@ package java
// This file contains the module types for compiling Android apps.
import (
+ "github.com/google/blueprint"
+ "github.com/google/blueprint/proptools"
"path/filepath"
+ "reflect"
"sort"
"strings"
- "github.com/google/blueprint"
- "github.com/google/blueprint/proptools"
-
"android/soong/android"
"android/soong/cc"
"android/soong/tradefed"
)
+var supportedDpis = []string{"ldpi", "mdpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi"}
+
func init() {
android.RegisterModuleType("android_app", AndroidAppFactory)
android.RegisterModuleType("android_test", AndroidTestFactory)
android.RegisterModuleType("android_test_helper_app", AndroidTestHelperAppFactory)
android.RegisterModuleType("android_app_certificate", AndroidAppCertificateFactory)
android.RegisterModuleType("override_android_app", OverrideAndroidAppModuleFactory)
+ android.RegisterModuleType("android_app_import", AndroidAppImportFactory)
+
+ initAndroidAppImportVariantGroupTypes()
}
// AndroidManifest.xml merging
@@ -114,6 +119,10 @@ type AndroidApp struct {
// the install APK name is normally the same as the module name, but can be overridden with PRODUCT_PACKAGE_NAME_OVERRIDES.
installApkName string
+ installDir android.OutputPath
+
+ onDeviceDir string
+
additionalAaptFlags []string
}
@@ -188,11 +197,9 @@ func (a *AndroidApp) shouldUncompressDex(ctx android.ModuleContext) bool {
return true
}
- // Uncompress dex in APKs of privileged apps, and modules used by privileged apps
- // (even for unbundled builds, they may be preinstalled as prebuilts).
- if ctx.Config().UncompressPrivAppDex() &&
- (Bool(a.appProperties.Privileged) ||
- inList(ctx.ModuleName(), ctx.Config().ModulesLoadedByPrivilegedModules())) {
+ // Uncompress dex in APKs of privileged apps (even for unbundled builds, they may
+ // be preinstalled as prebuilts).
+ if ctx.Config().UncompressPrivAppDex() && Bool(a.appProperties.Privileged) {
return true
}
@@ -200,12 +207,7 @@ func (a *AndroidApp) shouldUncompressDex(ctx android.ModuleContext) bool {
return false
}
- // Uncompress if the dex files is preopted on /system.
- if !a.dexpreopter.dexpreoptDisabled(ctx) && (ctx.Host() || !odexOnSystemOther(ctx, a.dexpreopter.installPath)) {
- return true
- }
-
- return false
+ return shouldUncompressDex(ctx, &a.dexpreopter)
}
func (a *AndroidApp) aaptBuildActions(ctx android.ModuleContext) {
@@ -286,7 +288,7 @@ func (a *AndroidApp) dexBuildActions(ctx android.ModuleContext) android.Path {
a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
a.deviceProperties.UncompressDex = a.dexpreopter.uncompressedDex
- if ctx.ModuleName() != "framework-res" {
+ if ctx.ModuleName() != "framework-res" && ctx.ModuleName() != "org.lineageos.platform-res" {
a.Module.compile(ctx, a.aaptSrcJar)
}
@@ -308,40 +310,41 @@ func (a *AndroidApp) jniBuildActions(jniLibs []jniLib, ctx android.ModuleContext
return jniJarFile
}
-func (a *AndroidApp) certificateBuildActions(certificateDeps []Certificate, ctx android.ModuleContext) []Certificate {
- cert := a.getCertString(ctx)
- certModule := android.SrcIsModule(cert)
- if certModule != "" {
- a.certificate = certificateDeps[0]
- certificateDeps = certificateDeps[1:]
- } else if cert != "" {
- defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
- a.certificate = Certificate{
- defaultDir.Join(ctx, cert+".x509.pem"),
- defaultDir.Join(ctx, cert+".pk8"),
+// Reads and prepends a main cert from the default cert dir if it hasn't been set already, i.e. it
+// isn't a cert module reference. Also checks and enforces system cert restriction if applicable.
+func processMainCert(m android.ModuleBase, certPropValue string, certificates []Certificate, ctx android.ModuleContext) []Certificate {
+ if android.SrcIsModule(certPropValue) == "" {
+ var mainCert Certificate
+ if certPropValue != "" {
+ defaultDir := ctx.Config().DefaultAppCertificateDir(ctx)
+ mainCert = Certificate{
+ defaultDir.Join(ctx, certPropValue+".x509.pem"),
+ defaultDir.Join(ctx, certPropValue+".pk8"),
+ }
+ } else {
+ pem, key := ctx.Config().DefaultAppCertificate(ctx)
+ mainCert = Certificate{pem, key}
}
- } else {
- pem, key := ctx.Config().DefaultAppCertificate(ctx)
- a.certificate = Certificate{pem, key}
+ certificates = append([]Certificate{mainCert}, certificates...)
}
- if !a.Module.Platform() {
- certPath := a.certificate.Pem.String()
+ if !m.Platform() {
+ certPath := certificates[0].Pem.String()
systemCertPath := ctx.Config().DefaultAppCertificateDir(ctx).String()
if strings.HasPrefix(certPath, systemCertPath) {
enforceSystemCert := ctx.Config().EnforceSystemCertificate()
whitelist := ctx.Config().EnforceSystemCertificateWhitelist()
- if enforceSystemCert && !inList(a.Module.Name(), whitelist) {
+ if enforceSystemCert && !inList(m.Name(), whitelist) {
ctx.PropertyErrorf("certificate", "The module in product partition cannot be signed with certificate in system.")
}
}
}
- return append([]Certificate{a.certificate}, certificateDeps...)
+ return certificates
}
-func (a *AndroidApp) noticeBuildActions(ctx android.ModuleContext, installDir android.OutputPath) android.OptionalPath {
+func (a *AndroidApp) noticeBuildActions(ctx android.ModuleContext) android.OptionalPath {
if !Bool(a.appProperties.Embed_notices) && !ctx.Config().IsEnvTrue("ALWAYS_EMBED_NOTICES") {
return android.OptionalPath{}
}
@@ -388,7 +391,7 @@ func (a *AndroidApp) noticeBuildActions(ctx android.ModuleContext, installDir an
sort.Slice(noticePaths, func(i, j int) bool {
return noticePaths[i].String() < noticePaths[j].String()
})
- noticeFile := android.BuildNoticeOutput(ctx, installDir, a.installApkName+".apk", noticePaths)
+ noticeFile := android.BuildNoticeOutput(ctx, a.installDir, a.installApkName+".apk", noticePaths)
return android.OptionalPathForPath(noticeFile)
}
@@ -397,17 +400,22 @@ func (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) {
// Check if the install APK name needs to be overridden.
a.installApkName = ctx.DeviceConfig().OverridePackageNameFor(a.Name())
- var installDir android.OutputPath
if ctx.ModuleName() == "framework-res" {
// framework-res.apk is installed as system/framework/framework-res.apk
- installDir = android.PathForModuleInstall(ctx, "framework")
+ a.installDir = android.PathForModuleInstall(ctx, "framework")
+ } else if ctx.ModuleName() == "org.lineageos.platform-res" {
+ // org.lineageos.platform-res.apk needs to be in system/framework
+ a.installDir = android.PathForModuleInstall(ctx, "framework")
} else if Bool(a.appProperties.Privileged) {
- installDir = android.PathForModuleInstall(ctx, "priv-app", a.installApkName)
+ a.installDir = android.PathForModuleInstall(ctx, "priv-app", a.installApkName)
+ } else if ctx.InstallInTestcases() {
+ a.installDir = android.PathForModuleInstall(ctx, a.installApkName)
} else {
- installDir = android.PathForModuleInstall(ctx, "app", a.installApkName)
+ a.installDir = android.PathForModuleInstall(ctx, "app", a.installApkName)
}
+ a.onDeviceDir = android.InstallPathToOnDevicePath(ctx, a.installDir)
- a.aapt.noticeFile = a.noticeBuildActions(ctx, installDir)
+ a.aapt.noticeFile = a.noticeBuildActions(ctx)
// Process all building blocks, from AAPT to certificates.
a.aaptBuildActions(ctx)
@@ -416,25 +424,26 @@ func (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) {
dexJarFile := a.dexBuildActions(ctx)
- jniLibs, certificateDeps := a.collectAppDeps(ctx)
+ jniLibs, certificateDeps := collectAppDeps(ctx)
jniJarFile := a.jniBuildActions(jniLibs, ctx)
if ctx.Failed() {
return
}
- certificates := a.certificateBuildActions(certificateDeps, ctx)
+ certificates := processMainCert(a.ModuleBase, a.getCertString(ctx), certificateDeps, ctx)
+ a.certificate = certificates[0]
// Build a final signed app package.
// TODO(jungjw): Consider changing this to installApkName.
packageFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".apk")
- CreateAppPackage(ctx, packageFile, a.exportPackage, jniJarFile, dexJarFile, certificates)
+ CreateAndSignAppPackage(ctx, packageFile, a.exportPackage, jniJarFile, dexJarFile, certificates)
a.outputFile = packageFile
for _, split := range a.aapt.splits {
// Sign the split APKs
packageFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"_"+split.suffix+".apk")
- CreateAppPackage(ctx, packageFile, split.path, nil, nil, certificates)
+ CreateAndSignAppPackage(ctx, packageFile, split.path, nil, nil, certificates)
a.extraOutputFiles = append(a.extraOutputFiles, packageFile)
}
@@ -444,13 +453,13 @@ func (a *AndroidApp) generateAndroidBuildActions(ctx android.ModuleContext) {
a.bundleFile = bundleFile
// Install the app package.
- ctx.InstallFile(installDir, a.installApkName+".apk", a.outputFile)
+ ctx.InstallFile(a.installDir, a.installApkName+".apk", a.outputFile)
for _, split := range a.aapt.splits {
- ctx.InstallFile(installDir, a.installApkName+"_"+split.suffix+".apk", split.path)
+ ctx.InstallFile(a.installDir, a.installApkName+"_"+split.suffix+".apk", split.path)
}
}
-func (a *AndroidApp) collectAppDeps(ctx android.ModuleContext) ([]jniLib, []Certificate) {
+func collectAppDeps(ctx android.ModuleContext) ([]jniLib, []Certificate) {
var jniLibs []jniLib
var certificates []Certificate
@@ -472,7 +481,6 @@ func (a *AndroidApp) collectAppDeps(ctx android.ModuleContext) ([]jniLib, []Cert
}
} else {
ctx.ModuleErrorf("jni_libs dependency %q must be a cc library", otherName)
-
}
} else if tag == certificateTag {
if dep, ok := module.(*AndroidAppCertificate); ok {
@@ -539,6 +547,10 @@ type AndroidTest struct {
data android.Paths
}
+func (a *AndroidTest) InstallInTestcases() bool {
+ return true
+}
+
func (a *AndroidTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
// Check if the instrumentation target package is overridden before generating build actions.
if a.appTestProperties.Instrumentation_for != nil {
@@ -680,3 +692,284 @@ func OverrideAndroidAppModuleFactory() android.Module {
android.InitOverrideModule(m)
return m
}
+
+type AndroidAppImport struct {
+ android.ModuleBase
+ android.DefaultableModuleBase
+ prebuilt android.Prebuilt
+
+ properties AndroidAppImportProperties
+ dpiVariants interface{}
+ archVariants interface{}
+
+ outputFile android.Path
+ certificate *Certificate
+
+ dexpreopter
+
+ installPath android.OutputPath
+}
+
+type AndroidAppImportProperties struct {
+ // A prebuilt apk to import
+ Apk *string
+
+ // The name of a certificate in the default certificate directory or an android_app_certificate
+ // module name in the form ":module". Should be empty if presigned or default_dev_cert is set.
+ Certificate *string
+
+ // Set this flag to true if the prebuilt apk is already signed. The certificate property must not
+ // be set for presigned modules.
+ Presigned *bool
+
+ // Sign with the default system dev certificate. Must be used judiciously. Most imported apps
+ // need to either specify a specific certificate or be presigned.
+ Default_dev_cert *bool
+
+ // 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
+
+ // Names of modules to be overridden. Listed modules can only be other binaries
+ // (in Make or Soong).
+ // This does not completely prevent installation of the overridden binaries, but if both
+ // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
+ // from PRODUCT_PACKAGES.
+ Overrides []string
+
+ // Optional name for the installed app. If unspecified, it is derived from the module name.
+ Filename *string
+}
+
+// Updates properties with variant-specific values.
+func (a *AndroidAppImport) processVariants(ctx android.LoadHookContext) {
+ config := ctx.Config()
+
+ dpiProps := reflect.ValueOf(a.dpiVariants).Elem().FieldByName("Dpi_variants")
+ // Try DPI variant matches in the reverse-priority order so that the highest priority match
+ // overwrites everything else.
+ // TODO(jungjw): Can we optimize this by making it priority order?
+ for i := len(config.ProductAAPTPrebuiltDPI()) - 1; i >= 0; i-- {
+ MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPrebuiltDPI()[i])
+ }
+ if config.ProductAAPTPreferredConfig() != "" {
+ MergePropertiesFromVariant(ctx, &a.properties, dpiProps, config.ProductAAPTPreferredConfig())
+ }
+
+ archProps := reflect.ValueOf(a.archVariants).Elem().FieldByName("Arch")
+ archType := ctx.Config().Targets[android.Android][0].Arch.ArchType
+ MergePropertiesFromVariant(ctx, &a.properties, archProps, archType.Name)
+}
+
+func MergePropertiesFromVariant(ctx android.BaseModuleContext,
+ dst interface{}, variantGroup reflect.Value, variant string) {
+ src := variantGroup.FieldByName(proptools.FieldNameForProperty(variant))
+ if !src.IsValid() {
+ return
+ }
+
+ err := proptools.ExtendMatchingProperties([]interface{}{dst}, src.Interface(), nil, proptools.OrderAppend)
+ if err != nil {
+ if propertyErr, ok := err.(*proptools.ExtendPropertyError); ok {
+ ctx.PropertyErrorf(propertyErr.Property, "%s", propertyErr.Err.Error())
+ } else {
+ panic(err)
+ }
+ }
+}
+
+func (a *AndroidAppImport) DepsMutator(ctx android.BottomUpMutatorContext) {
+ cert := android.SrcIsModule(String(a.properties.Certificate))
+ if cert != "" {
+ ctx.AddDependency(ctx.Module(), certificateTag, cert)
+ }
+}
+
+func (a *AndroidAppImport) uncompressEmbeddedJniLibs(
+ ctx android.ModuleContext, inputPath android.Path, outputPath android.OutputPath) {
+ rule := android.NewRuleBuilder()
+ rule.Command().
+ Textf(`if (zipinfo %s 'lib/*.so' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then`, inputPath).
+ Tool(ctx.Config().HostToolPath(ctx, "zip2zip")).
+ FlagWithInput("-i ", inputPath).
+ FlagWithOutput("-o ", outputPath).
+ FlagWithArg("-0 ", "'lib/**/*.so'").
+ Textf(`; else cp -f %s %s; fi`, inputPath, outputPath)
+ rule.Build(pctx, ctx, "uncompress-embedded-jni-libs", "Uncompress embedded JIN libs")
+}
+
+// Returns whether this module should have the dex file stored uncompressed in the APK.
+func (a *AndroidAppImport) shouldUncompressDex(ctx android.ModuleContext) bool {
+ if ctx.Config().UnbundledBuild() {
+ return false
+ }
+
+ // Uncompress dex in APKs of privileged apps
+ if ctx.Config().UncompressPrivAppDex() && Bool(a.properties.Privileged) {
+ return true
+ }
+
+ return shouldUncompressDex(ctx, &a.dexpreopter)
+}
+
+func (a *AndroidAppImport) uncompressDex(
+ ctx android.ModuleContext, inputPath android.Path, outputPath android.OutputPath) {
+ rule := android.NewRuleBuilder()
+ rule.Command().
+ Textf(`if (zipinfo %s '*.dex' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then`, inputPath).
+ Tool(ctx.Config().HostToolPath(ctx, "zip2zip")).
+ FlagWithInput("-i ", inputPath).
+ FlagWithOutput("-o ", outputPath).
+ FlagWithArg("-0 ", "'classes*.dex'").
+ Textf(`; else cp -f %s %s; fi`, inputPath, outputPath)
+ rule.Build(pctx, ctx, "uncompress-dex", "Uncompress dex files")
+}
+
+func (a *AndroidAppImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
+ numCertPropsSet := 0
+ if String(a.properties.Certificate) != "" {
+ numCertPropsSet++
+ }
+ if Bool(a.properties.Presigned) {
+ numCertPropsSet++
+ }
+ if Bool(a.properties.Default_dev_cert) {
+ numCertPropsSet++
+ }
+ if numCertPropsSet != 1 {
+ ctx.ModuleErrorf("One and only one of certficate, presigned, and default_dev_cert properties must be set")
+ }
+
+ _, certificates := collectAppDeps(ctx)
+
+ // TODO: LOCAL_EXTRACT_APK/LOCAL_EXTRACT_DPI_APK
+ // TODO: LOCAL_PACKAGE_SPLITS
+
+ srcApk := a.prebuilt.SingleSourcePath(ctx)
+
+ // TODO: Install or embed JNI libraries
+
+ // Uncompress JNI libraries in the apk
+ jnisUncompressed := android.PathForModuleOut(ctx, "jnis-uncompressed", ctx.ModuleName()+".apk")
+ a.uncompressEmbeddedJniLibs(ctx, srcApk, jnisUncompressed.OutputPath)
+
+ installDir := android.PathForModuleInstall(ctx, "app", a.BaseModuleName())
+ a.dexpreopter.installPath = installDir.Join(ctx, a.BaseModuleName()+".apk")
+ a.dexpreopter.isInstallable = true
+ a.dexpreopter.isPresignedPrebuilt = Bool(a.properties.Presigned)
+ a.dexpreopter.uncompressedDex = a.shouldUncompressDex(ctx)
+ dexOutput := a.dexpreopter.dexpreopt(ctx, jnisUncompressed)
+ if a.dexpreopter.uncompressedDex {
+ dexUncompressed := android.PathForModuleOut(ctx, "dex-uncompressed", ctx.ModuleName()+".apk")
+ a.uncompressDex(ctx, dexOutput, dexUncompressed.OutputPath)
+ dexOutput = dexUncompressed
+ }
+
+ // Sign or align the package
+ // TODO: Handle EXTERNAL
+ if !Bool(a.properties.Presigned) {
+ // If the certificate property is empty at this point, default_dev_cert must be set to true.
+ // Which makes processMainCert's behavior for the empty cert string WAI.
+ certificates = processMainCert(a.ModuleBase, String(a.properties.Certificate), certificates, ctx)
+ if len(certificates) != 1 {
+ ctx.ModuleErrorf("Unexpected number of certificates were extracted: %q", certificates)
+ }
+ a.certificate = &certificates[0]
+ signed := android.PathForModuleOut(ctx, "signed", ctx.ModuleName()+".apk")
+ SignAppPackage(ctx, signed, dexOutput, certificates)
+ a.outputFile = signed
+ } else {
+ alignedApk := android.PathForModuleOut(ctx, "zip-aligned", ctx.ModuleName()+".apk")
+ TransformZipAlign(ctx, alignedApk, dexOutput)
+ a.outputFile = alignedApk
+ }
+
+ // TODO: Optionally compress the output apk.
+
+ a.installPath = ctx.InstallFile(installDir,
+ proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+".apk"), a.outputFile)
+
+ // TODO: androidmk converter jni libs
+}
+
+func (a *AndroidAppImport) Prebuilt() *android.Prebuilt {
+ return &a.prebuilt
+}
+
+func (a *AndroidAppImport) Name() string {
+ return a.prebuilt.Name(a.ModuleBase.Name())
+}
+
+var dpiVariantGroupType reflect.Type
+var archVariantGroupType reflect.Type
+
+func initAndroidAppImportVariantGroupTypes() {
+ dpiVariantGroupType = createVariantGroupType(supportedDpis, "Dpi_variants")
+
+ archNames := make([]string, len(android.ArchTypeList()))
+ for i, archType := range android.ArchTypeList() {
+ archNames[i] = archType.Name
+ }
+ archVariantGroupType = createVariantGroupType(archNames, "Arch")
+}
+
+// Populates all variant struct properties at creation time.
+func (a *AndroidAppImport) populateAllVariantStructs() {
+ a.dpiVariants = reflect.New(dpiVariantGroupType).Interface()
+ a.AddProperties(a.dpiVariants)
+
+ a.archVariants = reflect.New(archVariantGroupType).Interface()
+ a.AddProperties(a.archVariants)
+}
+
+func createVariantGroupType(variants []string, variantGroupName string) reflect.Type {
+ props := reflect.TypeOf((*AndroidAppImportProperties)(nil))
+
+ variantFields := make([]reflect.StructField, len(variants))
+ for i, variant := range variants {
+ variantFields[i] = reflect.StructField{
+ Name: proptools.FieldNameForProperty(variant),
+ Type: props,
+ }
+ }
+
+ variantGroupStruct := reflect.StructOf(variantFields)
+ return reflect.StructOf([]reflect.StructField{
+ {
+ Name: variantGroupName,
+ Type: variantGroupStruct,
+ },
+ })
+}
+
+// android_app_import imports a prebuilt apk with additional processing specified in the module.
+// DPI-specific apk source files can be specified using dpi_variants. Example:
+//
+// android_app_import {
+// name: "example_import",
+// apk: "prebuilts/example.apk",
+// dpi_variants: {
+// mdpi: {
+// apk: "prebuilts/example_mdpi.apk",
+// },
+// xhdpi: {
+// apk: "prebuilts/example_xhdpi.apk",
+// },
+// },
+// certificate: "PRESIGNED",
+// }
+func AndroidAppImportFactory() android.Module {
+ module := &AndroidAppImport{}
+ module.AddProperties(&module.properties)
+ module.AddProperties(&module.dexpreoptProperties)
+ module.populateAllVariantStructs()
+ android.AddLoadHook(module, func(ctx android.LoadHookContext) {
+ module.processVariants(ctx)
+ })
+
+ InitJavaModule(module, android.DeviceSupported)
+ android.InitSingleSourcePrebuiltModule(module, &module.properties, "Apk")
+
+ return module
+}