blob: ceb7791fafe96fc205733cf7ffe09d695c0398f8 [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
23 "github.com/google/blueprint"
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
55 // Java resources
56 Android_resource_dirs []string
57}
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
71 if !a.properties.No_standard_libraries {
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
91 a.ExtraSrcLists = append(a.ExtraSrcLists, aaptJavaFileList)
92
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,
105 "--product "+ctx.AConfig().ProductAaptCharacteristics())
106 }
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,
135 "--product "+ctx.AConfig().ProductAaptCharacteristics())
136 }
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 Cross635c3b02016-05-18 15:37:25 -0700153 ctx.InstallFileName(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")
185 resourceDirs := android.PathsWithOptionalDefaultForModuleSrc(ctx, a.appProperties.Android_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
233 ctx.VisitDirectDeps(func(module blueprint.Module) {
Colin Cross74d73e22017-08-02 11:05:49 -0700234 var depFiles android.Paths
Colin Cross30e076a2015-04-13 13:58:27 -0700235 if sdkDep, ok := module.(sdkDependency); ok {
Colin Cross74d73e22017-08-02 11:05:49 -0700236 depFiles = sdkDep.ClasspathFiles()
Colin Crossf506d872017-07-19 15:53:04 -0700237 } else if javaDep, ok := module.(Dependency); ok {
Colin Cross30e076a2015-04-13 13:58:27 -0700238 if ctx.OtherModuleName(module) == "framework-res" {
Colin Cross74d73e22017-08-02 11:05:49 -0700239 depFiles = android.Paths{javaDep.(*AndroidApp).exportPackage}
Colin Cross30e076a2015-04-13 13:58:27 -0700240 }
241 }
Colin Cross74d73e22017-08-02 11:05:49 -0700242
243 for _, dep := range depFiles {
244 aaptFlags = append(aaptFlags, "-I "+dep.String())
Colin Cross30e076a2015-04-13 13:58:27 -0700245 }
Colin Cross74d73e22017-08-02 11:05:49 -0700246 aaptDeps = append(aaptDeps, depFiles...)
Colin Cross30e076a2015-04-13 13:58:27 -0700247 })
248
Colin Cross540eff82017-06-22 17:01:52 -0700249 sdkVersion := a.deviceProperties.Sdk_version
Colin Cross30e076a2015-04-13 13:58:27 -0700250 if sdkVersion == "" {
251 sdkVersion = ctx.AConfig().PlatformSdkVersion()
252 }
253
254 aaptFlags = append(aaptFlags, "--min-sdk-version "+sdkVersion)
255 aaptFlags = append(aaptFlags, "--target-sdk-version "+sdkVersion)
256
257 if !hasVersionCode {
258 aaptFlags = append(aaptFlags, "--version-code "+ctx.AConfig().PlatformSdkVersion())
259 }
260
261 if !hasVersionName {
262 aaptFlags = append(aaptFlags,
263 "--version-name "+ctx.AConfig().PlatformVersion()+"-"+ctx.AConfig().BuildNumber())
264 }
265
266 // TODO: LOCAL_PACKAGE_OVERRIDES
267 // $(addprefix --rename-manifest-package , $(PRIVATE_MANIFEST_PACKAGE_NAME)) \
268
269 // TODO: LOCAL_INSTRUMENTATION_FOR
270 // $(addprefix --rename-instrumentation-target-package , $(PRIVATE_MANIFEST_INSTRUMENTATION_FOR))
271
272 return aaptFlags, aaptDeps, hasResources
273}
274
Colin Cross36242852017-06-23 15:06:31 -0700275func AndroidAppFactory() android.Module {
Colin Cross30e076a2015-04-13 13:58:27 -0700276 module := &AndroidApp{}
277
Colin Cross540eff82017-06-22 17:01:52 -0700278 module.deviceProperties.Dex = true
Colin Cross30e076a2015-04-13 13:58:27 -0700279
Colin Cross36242852017-06-23 15:06:31 -0700280 module.AddProperties(
Colin Cross540eff82017-06-22 17:01:52 -0700281 &module.Module.properties,
282 &module.Module.deviceProperties,
283 &module.appProperties)
Colin Cross36242852017-06-23 15:06:31 -0700284
285 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibCommon)
286 return module
Colin Cross30e076a2015-04-13 13:58:27 -0700287}