2018-05-25 01:11:20 +02:00
|
|
|
// Copyright 2018 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package java
|
|
|
|
|
|
|
|
import (
|
2019-02-06 06:55:21 +01:00
|
|
|
"fmt"
|
2021-12-08 18:00:38 +01:00
|
|
|
"strconv"
|
2018-07-24 23:51:30 +02:00
|
|
|
"strings"
|
2018-05-25 01:11:20 +02:00
|
|
|
|
|
|
|
"github.com/google/blueprint"
|
|
|
|
|
|
|
|
"android/soong/android"
|
2020-08-14 18:32:16 +02:00
|
|
|
"android/soong/dexpreopt"
|
2018-05-25 01:11:20 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var manifestFixerRule = pctx.AndroidStaticRule("manifestFixer",
|
|
|
|
blueprint.RuleParams{
|
2018-09-10 22:35:13 +02:00
|
|
|
Command: `${config.ManifestFixerCmd} ` +
|
|
|
|
`$args $in $out`,
|
2018-05-25 01:11:20 +02:00
|
|
|
CommandDeps: []string{"${config.ManifestFixerCmd}"},
|
|
|
|
},
|
2022-01-24 18:44:05 +01:00
|
|
|
"args")
|
2018-05-25 01:11:20 +02:00
|
|
|
|
|
|
|
var manifestMergerRule = pctx.AndroidStaticRule("manifestMerger",
|
|
|
|
blueprint.RuleParams{
|
2019-04-20 01:25:38 +02:00
|
|
|
Command: `${config.ManifestMergerCmd} $args --main $in $libs --out $out`,
|
2019-02-27 07:14:04 +01:00
|
|
|
CommandDeps: []string{"${config.ManifestMergerCmd}"},
|
2018-05-25 01:11:20 +02:00
|
|
|
},
|
2019-04-20 01:25:38 +02:00
|
|
|
"args", "libs")
|
2018-05-25 01:11:20 +02:00
|
|
|
|
2021-12-08 18:00:38 +01:00
|
|
|
// targetSdkVersion for manifest_fixer
|
|
|
|
// When TARGET_BUILD_APPS is not empty, this method returns 10000 for modules targeting an unreleased SDK
|
|
|
|
// This enables release builds (that run with TARGET_BUILD_APPS=[val...]) to target APIs that have not yet been finalized as part of an SDK
|
2022-06-10 13:24:05 +02:00
|
|
|
func targetSdkVersionForManifestFixer(ctx android.ModuleContext, params ManifestFixerParams) string {
|
|
|
|
targetSdkVersionSpec := params.SdkContext.TargetSdkVersion(ctx)
|
|
|
|
|
|
|
|
// Check if we want to return 10000
|
2022-07-25 02:34:18 +02:00
|
|
|
// TODO(b/240294501): Determine the rules for handling test apexes
|
2022-06-10 13:24:05 +02:00
|
|
|
if shouldReturnFinalOrFutureInt(ctx, targetSdkVersionSpec, params.EnforceDefaultTargetSdkVersion) {
|
2021-12-08 18:00:38 +01:00
|
|
|
return strconv.Itoa(android.FutureApiLevel.FinalOrFutureInt())
|
|
|
|
}
|
|
|
|
targetSdkVersion, err := targetSdkVersionSpec.EffectiveVersionString(ctx)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ModuleErrorf("invalid targetSdkVersion: %s", err)
|
|
|
|
}
|
|
|
|
return targetSdkVersion
|
|
|
|
}
|
|
|
|
|
2022-06-10 13:24:05 +02:00
|
|
|
// Return true for modules targeting "current" if either
|
|
|
|
// 1. The module is built in unbundled mode (TARGET_BUILD_APPS not empty)
|
|
|
|
// 2. The module is run as part of MTS, and should be testable on stable branches
|
|
|
|
// Do not return 10000 if we are enforcing default targetSdkVersion and sdk has been finalised
|
|
|
|
func shouldReturnFinalOrFutureInt(ctx android.ModuleContext, targetSdkVersionSpec android.SdkSpec, enforceDefaultTargetSdkVersion bool) bool {
|
|
|
|
if enforceDefaultTargetSdkVersion && ctx.Config().PlatformSdkFinal() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return targetSdkVersionSpec.ApiLevel.IsPreview() && (ctx.Config().UnbundledBuildApps() || includedInMts(ctx.Module()))
|
|
|
|
}
|
|
|
|
|
2022-07-25 02:34:18 +02:00
|
|
|
// Helper function that casts android.Module to java.androidTestApp
|
|
|
|
// If this type conversion is possible, it queries whether the test app is included in an MTS suite
|
|
|
|
func includedInMts(module android.Module) bool {
|
|
|
|
if test, ok := module.(androidTestApp); ok {
|
|
|
|
return test.includedInTestSuite("mts")
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-01-24 18:44:05 +01:00
|
|
|
type ManifestFixerParams struct {
|
2022-06-10 13:24:05 +02:00
|
|
|
SdkContext android.SdkContext
|
|
|
|
ClassLoaderContexts dexpreopt.ClassLoaderContextMap
|
|
|
|
IsLibrary bool
|
|
|
|
DefaultManifestVersion string
|
|
|
|
UseEmbeddedNativeLibs bool
|
|
|
|
UsesNonSdkApis bool
|
|
|
|
UseEmbeddedDex bool
|
|
|
|
HasNoCode bool
|
|
|
|
TestOnly bool
|
|
|
|
LoggingParent string
|
|
|
|
EnforceDefaultTargetSdkVersion bool
|
2022-01-24 18:44:05 +01:00
|
|
|
}
|
2018-07-24 23:51:30 +02:00
|
|
|
|
2022-01-24 18:44:05 +01:00
|
|
|
// Uses manifest_fixer.py to inject minSdkVersion, etc. into an AndroidManifest.xml
|
2022-02-10 14:28:35 +01:00
|
|
|
func ManifestFixer(ctx android.ModuleContext, manifest android.Path,
|
|
|
|
params ManifestFixerParams) android.Path {
|
2018-07-24 23:51:30 +02:00
|
|
|
var args []string
|
2022-01-24 18:44:05 +01:00
|
|
|
|
|
|
|
if params.IsLibrary {
|
2018-07-24 23:51:30 +02:00
|
|
|
args = append(args, "--library")
|
2022-01-24 18:44:05 +01:00
|
|
|
} else if params.SdkContext != nil {
|
2022-02-10 14:28:35 +01:00
|
|
|
minSdkVersion, err := params.SdkContext.MinSdkVersion(ctx).EffectiveVersion(ctx)
|
2019-02-06 06:55:21 +01:00
|
|
|
if err != nil {
|
2022-02-10 14:28:35 +01:00
|
|
|
ctx.ModuleErrorf("invalid minSdkVersion: %s", err)
|
2019-02-06 06:55:21 +01:00
|
|
|
}
|
2021-03-31 11:17:53 +02:00
|
|
|
if minSdkVersion.FinalOrFutureInt() >= 23 {
|
2022-01-24 18:44:05 +01:00
|
|
|
args = append(args, fmt.Sprintf("--extract-native-libs=%v", !params.UseEmbeddedNativeLibs))
|
|
|
|
} else if params.UseEmbeddedNativeLibs {
|
2022-04-22 15:53:16 +02:00
|
|
|
ctx.ModuleErrorf("module attempted to store uncompressed native libraries, but minSdkVersion=%s doesn't support it",
|
|
|
|
minSdkVersion.String())
|
2019-02-06 06:55:21 +01:00
|
|
|
}
|
2018-07-24 23:51:30 +02:00
|
|
|
}
|
2019-02-07 22:07:08 +01:00
|
|
|
|
2022-01-24 18:44:05 +01:00
|
|
|
if params.UsesNonSdkApis {
|
2019-02-18 19:24:16 +01:00
|
|
|
args = append(args, "--uses-non-sdk-api")
|
|
|
|
}
|
|
|
|
|
2022-01-24 18:44:05 +01:00
|
|
|
if params.UseEmbeddedDex {
|
2019-05-15 02:03:24 +02:00
|
|
|
args = append(args, "--use-embedded-dex")
|
2019-02-07 22:07:08 +01:00
|
|
|
}
|
2018-05-25 01:11:20 +02:00
|
|
|
|
2022-01-24 18:44:05 +01:00
|
|
|
if params.ClassLoaderContexts != nil {
|
2022-05-04 13:00:02 +02:00
|
|
|
// Libraries propagated via `uses_libs`/`optional_uses_libs` are also added (they may be
|
|
|
|
// propagated from dependencies).
|
|
|
|
requiredUsesLibs, optionalUsesLibs := params.ClassLoaderContexts.UsesLibs()
|
2022-01-24 18:44:05 +01:00
|
|
|
|
|
|
|
for _, usesLib := range requiredUsesLibs {
|
|
|
|
args = append(args, "--uses-library", usesLib)
|
|
|
|
}
|
|
|
|
for _, usesLib := range optionalUsesLibs {
|
|
|
|
args = append(args, "--optional-uses-library", usesLib)
|
|
|
|
}
|
2019-05-22 19:46:27 +02:00
|
|
|
}
|
|
|
|
|
2022-01-24 18:44:05 +01:00
|
|
|
if params.HasNoCode {
|
2019-05-31 00:51:14 +02:00
|
|
|
args = append(args, "--has-no-code")
|
|
|
|
}
|
|
|
|
|
2022-01-24 18:44:05 +01:00
|
|
|
if params.TestOnly {
|
|
|
|
args = append(args, "--test-only")
|
2020-02-12 02:27:19 +01:00
|
|
|
}
|
2021-12-08 18:00:38 +01:00
|
|
|
|
2022-01-24 18:44:05 +01:00
|
|
|
if params.LoggingParent != "" {
|
|
|
|
args = append(args, "--logging-parent", params.LoggingParent)
|
Abstract sdk_version string using sdkSpec type
The value format that sdk_version (and min_sdk_version, etc.) can have
has consistently evolved and is quite complicated. Furthermore, with the
Mainline module effort, we are expected to have more sdk_versions like
'module-app-current', 'module-lib-current', etc.
The goal of this change is to abstract the various sdk versions, which
are currently represented in string and is parsed in various places,
into a type called sdkSpec, so that adding new sdk veresions becomes
easier than before.
The sdk_version string is now parsed in only one place 'SdkSpecFrom', in
which it is converted into the sdkSpec struct. The struct type provides
several methods that again converts sdkSpec into context-specific
information such as the effective version number, etc.
Bug: 146757305
Bug: 147879031
Test: m
Change-Id: I252f3706544f00ea71c61c23460f07561dd28ab0
2020-01-20 18:03:43 +01:00
|
|
|
}
|
2022-01-24 18:44:05 +01:00
|
|
|
var deps android.Paths
|
|
|
|
var argsMapper = make(map[string]string)
|
Abstract sdk_version string using sdkSpec type
The value format that sdk_version (and min_sdk_version, etc.) can have
has consistently evolved and is quite complicated. Furthermore, with the
Mainline module effort, we are expected to have more sdk_versions like
'module-app-current', 'module-lib-current', etc.
The goal of this change is to abstract the various sdk versions, which
are currently represented in string and is parsed in various places,
into a type called sdkSpec, so that adding new sdk veresions becomes
easier than before.
The sdk_version string is now parsed in only one place 'SdkSpecFrom', in
which it is converted into the sdkSpec struct. The struct type provides
several methods that again converts sdkSpec into context-specific
information such as the effective version number, etc.
Bug: 146757305
Bug: 147879031
Test: m
Change-Id: I252f3706544f00ea71c61c23460f07561dd28ab0
2020-01-20 18:03:43 +01:00
|
|
|
|
2022-01-24 18:44:05 +01:00
|
|
|
if params.SdkContext != nil {
|
2022-06-10 13:24:05 +02:00
|
|
|
targetSdkVersion := targetSdkVersionForManifestFixer(ctx, params)
|
2019-04-18 23:27:12 +02:00
|
|
|
|
2022-02-10 14:28:35 +01:00
|
|
|
if UseApiFingerprint(ctx) && ctx.ModuleName() != "framework-res" {
|
|
|
|
targetSdkVersion = ctx.Config().PlatformSdkCodename() + fmt.Sprintf(".$$(cat %s)", ApiFingerprintPath(ctx).String())
|
|
|
|
deps = append(deps, ApiFingerprintPath(ctx))
|
2022-01-24 18:44:05 +01:00
|
|
|
}
|
|
|
|
|
2023-02-01 00:13:45 +01:00
|
|
|
args = append(args, "--targetSdkVersion ", targetSdkVersion)
|
|
|
|
|
2022-02-10 14:28:35 +01:00
|
|
|
minSdkVersion, err := params.SdkContext.MinSdkVersion(ctx).EffectiveVersionString(ctx)
|
2022-01-24 18:44:05 +01:00
|
|
|
if err != nil {
|
2022-02-10 14:28:35 +01:00
|
|
|
ctx.ModuleErrorf("invalid minSdkVersion: %s", err)
|
2022-01-24 18:44:05 +01:00
|
|
|
}
|
|
|
|
|
2022-05-17 22:21:50 +02:00
|
|
|
replaceMaxSdkVersionPlaceholder, err := params.SdkContext.ReplaceMaxSdkVersionPlaceholder(ctx).EffectiveVersion(ctx)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ModuleErrorf("invalid ReplaceMaxSdkVersionPlaceholder: %s", err)
|
|
|
|
}
|
|
|
|
|
2022-02-10 14:28:35 +01:00
|
|
|
if UseApiFingerprint(ctx) && ctx.ModuleName() != "framework-res" {
|
|
|
|
minSdkVersion = ctx.Config().PlatformSdkCodename() + fmt.Sprintf(".$$(cat %s)", ApiFingerprintPath(ctx).String())
|
|
|
|
deps = append(deps, ApiFingerprintPath(ctx))
|
2022-01-24 18:44:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2022-02-10 14:28:35 +01:00
|
|
|
ctx.ModuleErrorf("invalid minSdkVersion: %s", err)
|
2022-01-24 18:44:05 +01:00
|
|
|
}
|
|
|
|
args = append(args, "--minSdkVersion ", minSdkVersion)
|
2022-05-17 22:21:50 +02:00
|
|
|
args = append(args, "--replaceMaxSdkVersionPlaceholder ", strconv.Itoa(replaceMaxSdkVersionPlaceholder.FinalOrFutureInt()))
|
2022-01-24 18:44:05 +01:00
|
|
|
args = append(args, "--raise-min-sdk-version")
|
Abstract sdk_version string using sdkSpec type
The value format that sdk_version (and min_sdk_version, etc.) can have
has consistently evolved and is quite complicated. Furthermore, with the
Mainline module effort, we are expected to have more sdk_versions like
'module-app-current', 'module-lib-current', etc.
The goal of this change is to abstract the various sdk versions, which
are currently represented in string and is parsed in various places,
into a type called sdkSpec, so that adding new sdk veresions becomes
easier than before.
The sdk_version string is now parsed in only one place 'SdkSpecFrom', in
which it is converted into the sdkSpec struct. The struct type provides
several methods that again converts sdkSpec into context-specific
information such as the effective version number, etc.
Bug: 146757305
Bug: 147879031
Test: m
Change-Id: I252f3706544f00ea71c61c23460f07561dd28ab0
2020-01-20 18:03:43 +01:00
|
|
|
}
|
2022-07-27 15:59:18 +02:00
|
|
|
if params.DefaultManifestVersion != "" {
|
|
|
|
args = append(args, "--override-placeholder-version", params.DefaultManifestVersion)
|
|
|
|
}
|
2022-01-24 18:44:05 +01:00
|
|
|
|
2022-02-10 14:28:35 +01:00
|
|
|
fixedManifest := android.PathForModuleOut(ctx, "manifest_fixer", "AndroidManifest.xml")
|
2022-01-24 18:44:05 +01:00
|
|
|
argsMapper["args"] = strings.Join(args, " ")
|
|
|
|
|
2022-02-10 14:28:35 +01:00
|
|
|
ctx.Build(pctx, android.BuildParams{
|
2019-04-20 01:22:57 +02:00
|
|
|
Rule: manifestFixerRule,
|
|
|
|
Description: "fix manifest",
|
2022-02-10 14:28:35 +01:00
|
|
|
Input: manifest,
|
2019-04-20 01:22:57 +02:00
|
|
|
Implicits: deps,
|
|
|
|
Output: fixedManifest,
|
2022-01-24 18:44:05 +01:00
|
|
|
Args: argsMapper,
|
2018-05-25 01:11:20 +02:00
|
|
|
})
|
|
|
|
|
2020-01-30 05:07:03 +01:00
|
|
|
return fixedManifest.WithoutRel()
|
2019-04-20 01:22:57 +02:00
|
|
|
}
|
|
|
|
|
2019-04-20 01:25:38 +02:00
|
|
|
func manifestMerger(ctx android.ModuleContext, manifest android.Path, staticLibManifests android.Paths,
|
|
|
|
isLibrary bool) android.Path {
|
|
|
|
|
|
|
|
var args string
|
|
|
|
if !isLibrary {
|
|
|
|
// Follow Gradle's behavior, only pass --remove-tools-declarations when merging app manifests.
|
|
|
|
args = "--remove-tools-declarations"
|
|
|
|
}
|
|
|
|
|
2019-04-20 01:22:57 +02:00
|
|
|
mergedManifest := android.PathForModuleOut(ctx, "manifest_merger", "AndroidManifest.xml")
|
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: manifestMergerRule,
|
|
|
|
Description: "merge manifest",
|
|
|
|
Input: manifest,
|
|
|
|
Implicits: staticLibManifests,
|
|
|
|
Output: mergedManifest,
|
|
|
|
Args: map[string]string{
|
|
|
|
"libs": android.JoinWithPrefix(staticLibManifests.Strings(), "--libs "),
|
2019-04-20 01:25:38 +02:00
|
|
|
"args": args,
|
2019-04-20 01:22:57 +02:00
|
|
|
},
|
|
|
|
})
|
2018-05-25 01:11:20 +02:00
|
|
|
|
2020-01-30 05:07:03 +01:00
|
|
|
return mergedManifest.WithoutRel()
|
2018-05-25 01:11:20 +02:00
|
|
|
}
|