blob: c2e9214b3a3fd5b8770ae6ce258541a97d452005 [file] [log] [blame]
Colin Cross30e076a2015-04-13 13:58:27 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package java
16
17// This file contains the module types for compiling Android apps.
18
19import (
Colin Cross30e076a2015-04-13 13:58:27 -070020 "path/filepath"
21 "strings"
22
Colin Cross76b5f0c2017-08-29 16:02:06 -070023 "github.com/google/blueprint/proptools"
Colin Cross30e076a2015-04-13 13:58:27 -070024
Colin Cross635c3b02016-05-18 15:37:25 -070025 "android/soong/android"
Colin Cross30e076a2015-04-13 13:58:27 -070026)
27
28// AAR prebuilts
29// AndroidManifest.xml merging
30// package splits
31
Colin Cross7d5136f2015-05-11 13:39:40 -070032type androidAppProperties struct {
33 // path to a certificate, or the name of a certificate in the default
34 // certificate directory, or blank to use the default product certificate
35 Certificate string
36
37 // paths to extra certificates to sign the apk with
38 Additional_certificates []string
39
40 // If set, create package-export.apk, which other packages can
41 // use to get PRODUCT-agnostic resource data like IDs and type definitions.
42 Export_package_resources bool
43
44 // flags passed to aapt when creating the apk
45 Aaptflags []string
46
47 // list of resource labels to generate individual resource packages
48 Package_splits []string
49
50 // list of directories relative to the Blueprints file containing assets.
51 // Defaults to "assets"
52 Asset_dirs []string
53
54 // list of directories relative to the Blueprints file containing
Colin Cross86a63ff2017-09-27 17:33:10 -070055 // Android resources
56 Resource_dirs []string
Colin Cross7d5136f2015-05-11 13:39:40 -070057}
58
Colin Cross30e076a2015-04-13 13:58:27 -070059type AndroidApp struct {
Colin Cross46c9b8b2017-06-22 16:51:17 -070060 Module
Colin Cross30e076a2015-04-13 13:58:27 -070061
Colin Cross7d5136f2015-05-11 13:39:40 -070062 appProperties androidAppProperties
Colin Cross30e076a2015-04-13 13:58:27 -070063
Colin Cross635c3b02016-05-18 15:37:25 -070064 aaptJavaFileList android.Path
65 exportPackage android.Path
Colin Cross30e076a2015-04-13 13:58:27 -070066}
67
Colin Cross46c9b8b2017-06-22 16:51:17 -070068func (a *AndroidApp) DepsMutator(ctx android.BottomUpMutatorContext) {
69 a.Module.deps(ctx)
Colin Cross30e076a2015-04-13 13:58:27 -070070
Colin Cross76b5f0c2017-08-29 16:02:06 -070071 if !proptools.Bool(a.properties.No_standard_libs) {
Colin Cross540eff82017-06-22 17:01:52 -070072 switch a.deviceProperties.Sdk_version { // TODO: Res_sdk_version?
Colin Cross30e076a2015-04-13 13:58:27 -070073 case "current", "system_current", "":
Colin Crossbe1da472017-07-07 15:59:46 -070074 ctx.AddDependency(ctx.Module(), frameworkResTag, "framework-res")
Colin Cross30e076a2015-04-13 13:58:27 -070075 default:
76 // We'll already have a dependency on an sdk prebuilt android.jar
77 }
78 }
Colin Cross30e076a2015-04-13 13:58:27 -070079}
80
Colin Cross46c9b8b2017-06-22 16:51:17 -070081func (a *AndroidApp) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross30e076a2015-04-13 13:58:27 -070082 aaptFlags, aaptDeps, hasResources := a.aaptFlags(ctx)
83
84 if hasResources {
85 // First generate R.java so we can build the .class files
86 aaptRJavaFlags := append([]string(nil), aaptFlags...)
87
88 publicResourcesFile, proguardOptionsFile, aaptJavaFileList :=
89 CreateResourceJavaFiles(ctx, aaptRJavaFlags, aaptDeps)
90 a.aaptJavaFileList = aaptJavaFileList
Colin Cross59149b62017-10-16 18:07:29 -070091 // TODO(ccross): export aapt generated java files as a src jar
Colin Cross30e076a2015-04-13 13:58:27 -070092
93 if a.appProperties.Export_package_resources {
94 aaptPackageFlags := append([]string(nil), aaptFlags...)
95 var hasProduct bool
96 for _, f := range aaptPackageFlags {
97 if strings.HasPrefix(f, "--product") {
98 hasProduct = true
99 break
100 }
101 }
102
103 if !hasProduct {
104 aaptPackageFlags = append(aaptPackageFlags,
Colin Crossface4e42017-10-30 17:32:15 -0700105 "--product "+ctx.AConfig().ProductAAPTCharacteristics())
Colin Cross30e076a2015-04-13 13:58:27 -0700106 }
107 a.exportPackage = CreateExportPackage(ctx, aaptPackageFlags, aaptDeps)
108 ctx.CheckbuildFile(a.exportPackage)
109 }
110 ctx.CheckbuildFile(publicResourcesFile)
111 ctx.CheckbuildFile(proguardOptionsFile)
112 ctx.CheckbuildFile(aaptJavaFileList)
113 }
114
Colin Cross46c9b8b2017-06-22 16:51:17 -0700115 // apps manifests are handled by aapt, don't let Module see them
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700116 a.properties.Manifest = nil
Colin Cross30e076a2015-04-13 13:58:27 -0700117
118 //if !ctx.ContainsProperty("proguard.enabled") {
119 // a.properties.Proguard.Enabled = true
120 //}
121
Colin Cross46c9b8b2017-06-22 16:51:17 -0700122 a.Module.compile(ctx)
Colin Cross30e076a2015-04-13 13:58:27 -0700123
124 aaptPackageFlags := append([]string(nil), aaptFlags...)
125 var hasProduct bool
126 for _, f := range aaptPackageFlags {
127 if strings.HasPrefix(f, "--product") {
128 hasProduct = true
129 break
130 }
131 }
132
133 if !hasProduct {
134 aaptPackageFlags = append(aaptPackageFlags,
Colin Crossface4e42017-10-30 17:32:15 -0700135 "--product "+ctx.AConfig().ProductAAPTCharacteristics())
Colin Cross30e076a2015-04-13 13:58:27 -0700136 }
137
138 certificate := a.appProperties.Certificate
139 if certificate == "" {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700140 certificate = ctx.AConfig().DefaultAppCertificate(ctx).String()
Colin Cross30e076a2015-04-13 13:58:27 -0700141 } else if dir, _ := filepath.Split(certificate); dir == "" {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700142 certificate = filepath.Join(ctx.AConfig().DefaultAppCertificateDir(ctx).String(), certificate)
Colin Cross30e076a2015-04-13 13:58:27 -0700143 } else {
Colin Cross635c3b02016-05-18 15:37:25 -0700144 certificate = filepath.Join(android.PathForSource(ctx).String(), certificate)
Colin Cross30e076a2015-04-13 13:58:27 -0700145 }
146
147 certificates := []string{certificate}
148 for _, c := range a.appProperties.Additional_certificates {
Colin Cross635c3b02016-05-18 15:37:25 -0700149 certificates = append(certificates, filepath.Join(android.PathForSource(ctx).String(), c))
Colin Cross30e076a2015-04-13 13:58:27 -0700150 }
151
152 a.outputFile = CreateAppPackage(ctx, aaptPackageFlags, a.outputFile, certificates)
Colin Cross5c517922017-08-31 12:29:17 -0700153 ctx.InstallFile(android.PathForModuleInstall(ctx, "app"), ctx.ModuleName()+".apk", a.outputFile)
Colin Cross30e076a2015-04-13 13:58:27 -0700154}
155
156var aaptIgnoreFilenames = []string{
157 ".svn",
158 ".git",
159 ".ds_store",
160 "*.scc",
161 ".*",
162 "CVS",
163 "thumbs.db",
164 "picasa.ini",
165 "*~",
166}
167
Colin Cross635c3b02016-05-18 15:37:25 -0700168func (a *AndroidApp) aaptFlags(ctx android.ModuleContext) ([]string, android.Paths, bool) {
Colin Cross30e076a2015-04-13 13:58:27 -0700169 aaptFlags := a.appProperties.Aaptflags
170 hasVersionCode := false
171 hasVersionName := false
172 for _, f := range aaptFlags {
173 if strings.HasPrefix(f, "--version-code") {
174 hasVersionCode = true
175 } else if strings.HasPrefix(f, "--version-name") {
176 hasVersionName = true
177 }
178 }
179
180 if true /* is not a test */ {
181 aaptFlags = append(aaptFlags, "-z")
182 }
183
Colin Cross635c3b02016-05-18 15:37:25 -0700184 assetDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.appProperties.Asset_dirs, "assets")
Colin Cross86a63ff2017-09-27 17:33:10 -0700185 resourceDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.appProperties.Resource_dirs, "res")
Colin Cross30e076a2015-04-13 13:58:27 -0700186
Colin Cross635c3b02016-05-18 15:37:25 -0700187 var overlayResourceDirs android.Paths
Colin Cross30e076a2015-04-13 13:58:27 -0700188 // For every resource directory, check if there is an overlay directory with the same path.
189 // If found, it will be prepended to the list of resource directories.
190 for _, overlayDir := range ctx.AConfig().ResourceOverlays() {
191 for _, resourceDir := range resourceDirs {
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700192 overlay := overlayDir.OverlayPath(ctx, resourceDir)
193 if overlay.Valid() {
194 overlayResourceDirs = append(overlayResourceDirs, overlay.Path())
Colin Cross30e076a2015-04-13 13:58:27 -0700195 }
196 }
197 }
198
199 if len(overlayResourceDirs) > 0 {
200 resourceDirs = append(overlayResourceDirs, resourceDirs...)
201 }
202
203 // aapt needs to rerun if any files are added or modified in the assets or resource directories,
204 // use glob to create a filelist.
Colin Cross635c3b02016-05-18 15:37:25 -0700205 var aaptDeps android.Paths
Colin Cross30e076a2015-04-13 13:58:27 -0700206 var hasResources bool
207 for _, d := range resourceDirs {
Colin Cross7f19f372016-11-01 11:10:25 -0700208 newDeps := ctx.Glob(filepath.Join(d.String(), "**/*"), aaptIgnoreFilenames)
Colin Cross30e076a2015-04-13 13:58:27 -0700209 aaptDeps = append(aaptDeps, newDeps...)
210 if len(newDeps) > 0 {
211 hasResources = true
212 }
213 }
214 for _, d := range assetDirs {
Colin Cross7f19f372016-11-01 11:10:25 -0700215 newDeps := ctx.Glob(filepath.Join(d.String(), "**/*"), aaptIgnoreFilenames)
Colin Cross30e076a2015-04-13 13:58:27 -0700216 aaptDeps = append(aaptDeps, newDeps...)
217 }
218
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700219 var manifestFile string
220 if a.properties.Manifest == nil {
Colin Cross30e076a2015-04-13 13:58:27 -0700221 manifestFile = "AndroidManifest.xml"
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700222 } else {
223 manifestFile = *a.properties.Manifest
Colin Cross30e076a2015-04-13 13:58:27 -0700224 }
225
Colin Cross635c3b02016-05-18 15:37:25 -0700226 manifestPath := android.PathForModuleSrc(ctx, manifestFile)
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700227 aaptDeps = append(aaptDeps, manifestPath)
Colin Cross30e076a2015-04-13 13:58:27 -0700228
Dan Willemsen34cc69e2015-09-23 15:26:20 -0700229 aaptFlags = append(aaptFlags, "-M "+manifestPath.String())
Colin Cross635c3b02016-05-18 15:37:25 -0700230 aaptFlags = append(aaptFlags, android.JoinWithPrefix(assetDirs.Strings(), "-A "))
231 aaptFlags = append(aaptFlags, android.JoinWithPrefix(resourceDirs.Strings(), "-S "))
Colin Cross30e076a2015-04-13 13:58:27 -0700232
Colin Crossd11fcda2017-10-23 17:59:01 -0700233 ctx.VisitDirectDeps(func(module android.Module) {
Colin Cross74d73e22017-08-02 11:05:49 -0700234 var depFiles android.Paths
Colin Crossfc3674a2017-09-18 17:41:52 -0700235 if javaDep, ok := module.(Dependency); ok {
Colin Cross30e076a2015-04-13 13:58:27 -0700236 if ctx.OtherModuleName(module) == "framework-res" {
Colin Cross74d73e22017-08-02 11:05:49 -0700237 depFiles = android.Paths{javaDep.(*AndroidApp).exportPackage}
Colin Cross30e076a2015-04-13 13:58:27 -0700238 }
239 }
Colin Cross74d73e22017-08-02 11:05:49 -0700240
241 for _, dep := range depFiles {
242 aaptFlags = append(aaptFlags, "-I "+dep.String())
Colin Cross30e076a2015-04-13 13:58:27 -0700243 }
Colin Cross74d73e22017-08-02 11:05:49 -0700244 aaptDeps = append(aaptDeps, depFiles...)
Colin Cross30e076a2015-04-13 13:58:27 -0700245 })
246
Colin Cross540eff82017-06-22 17:01:52 -0700247 sdkVersion := a.deviceProperties.Sdk_version
Colin Cross30e076a2015-04-13 13:58:27 -0700248 if sdkVersion == "" {
249 sdkVersion = ctx.AConfig().PlatformSdkVersion()
250 }
251
252 aaptFlags = append(aaptFlags, "--min-sdk-version "+sdkVersion)
253 aaptFlags = append(aaptFlags, "--target-sdk-version "+sdkVersion)
254
255 if !hasVersionCode {
256 aaptFlags = append(aaptFlags, "--version-code "+ctx.AConfig().PlatformSdkVersion())
257 }
258
259 if !hasVersionName {
260 aaptFlags = append(aaptFlags,
261 "--version-name "+ctx.AConfig().PlatformVersion()+"-"+ctx.AConfig().BuildNumber())
262 }
263
264 // TODO: LOCAL_PACKAGE_OVERRIDES
265 // $(addprefix --rename-manifest-package , $(PRIVATE_MANIFEST_PACKAGE_NAME)) \
266
267 // TODO: LOCAL_INSTRUMENTATION_FOR
268 // $(addprefix --rename-instrumentation-target-package , $(PRIVATE_MANIFEST_INSTRUMENTATION_FOR))
269
270 return aaptFlags, aaptDeps, hasResources
271}
272
Colin Cross36242852017-06-23 15:06:31 -0700273func AndroidAppFactory() android.Module {
Colin Cross30e076a2015-04-13 13:58:27 -0700274 module := &AndroidApp{}
275
Colin Cross36242852017-06-23 15:06:31 -0700276 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700277 &module.Module.properties,
278 &module.Module.deviceProperties,
279 &module.appProperties)
Colin Cross36242852017-06-23 15:06:31 -0700280
281 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
282 return module
Colin Cross30e076a2015-04-13 13:58:27 -0700283}