aboutsummaryrefslogtreecommitdiffstats
path: root/share/qbs/modules/Android/sdk/sdk.qbs
blob: 31f3ed46520a5862fbf368e3ba6edb2d7292799f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
**
** This file is part of Qbs.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms and
** conditions see http://www.qt.io/terms-conditions. For further information
** use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

import qbs.Environment
import qbs.File
import qbs.FileInfo
import qbs.ModUtils
import qbs.Probes
import qbs.TextFile
import qbs.Utilities
import qbs.Xml
import "utils.js" as SdkUtils

Module {
    Probes.AndroidSdkProbe {
        id: sdkProbe
        environmentPaths: (sdkDir ? [sdkDir] : []).concat(base)
    }

    Probes.AndroidNdkProbe {
        id: ndkProbe
        sdkPath: sdkProbe.path
        environmentPaths: (ndkDir ? [ndkDir] : []).concat(base)
    }

    Probes.PathProbe {
        id: bundletoolProbe
        platformSearchPaths: [Android.sdk.sdkDir]
        names: ["bundletool-all"]
        nameSuffixes: ["-0.11.0.jar", "-0.12.0.jar", "-0.13.0.jar", "-0.13.3.jar", "-0.13.4.jar",
            "-0.14.0.jar", "-0.15.0.jar"]
    }

    property path sdkDir: sdkProbe.path
    property path ndkDir: ndkProbe.path
    property path ndkSamplesDir: ndkProbe.samplesDir
    property string buildToolsVersion: sdkProbe.buildToolsVersion
    property var buildToolsVersionParts: buildToolsVersion ? buildToolsVersion.split('.').map(function(item) { return parseInt(item, 10); }) : []
    property int buildToolsVersionMajor: buildToolsVersionParts[0]
    property int buildToolsVersionMinor: buildToolsVersionParts[1]
    property int buildToolsVersionPatch: buildToolsVersionParts[2]
    property string platform: sdkProbe.platform

    property path bundletoolFilePath: bundletoolProbe.filePath

    // Product-specific properties and files
    property string packageName: {
        var idx = product.name.indexOf(".")
        if (idx > 0 && idx < product.name.length)
            return product.name;
        return "com.example." + product.name;
    }
    property int versionCode
    property string versionName
    property string apkBaseName: packageName
    property bool automaticSources: true
    property bool legacyLayout: false
    property path sourceSetDir: legacyLayout
                                ? product.sourceDirectory
                                : FileInfo.joinPaths(product.sourceDirectory, "src/main")
    property path resourcesDir: FileInfo.joinPaths(sourceSetDir, "res")
    property path assetsDir: FileInfo.joinPaths(sourceSetDir, "assets")
    property path sourcesDir: FileInfo.joinPaths(sourceSetDir, legacyLayout ? "src" : "java")
    property string manifestFile: defaultManifestFile
    readonly property string defaultManifestFile: FileInfo.joinPaths(sourceSetDir,
                                                                   "AndroidManifest.xml")

    property bool _enableRules: !product.multiplexConfigurationId && !!packageName

    property bool _archInName: false
    property bool _bundledInAssets: true

    Group {
        name: "java sources"
        condition: Android.sdk.automaticSources
        prefix: FileInfo.resolvePath(product.sourceDirectory, Android.sdk.sourcesDir + '/')
        files: "**/*.java"
    }

    Group {
        name: "android resources"
        condition: Android.sdk.automaticSources
        fileTags: ["android.resources"]
        prefix: FileInfo.resolvePath(product.sourceDirectory, Android.sdk.resourcesDir + '/')
        files: "**/*"
    }

    Group {
        name: "android assets"
        condition: Android.sdk.automaticSources
        fileTags: ["android.assets"]
        prefix: FileInfo.resolvePath(product.sourceDirectory, Android.sdk.assetsDir + '/')
        files: "**/*"
    }

    Group {
        name: "manifest"
        condition: Android.sdk.automaticSources
        fileTags: ["android.manifest"]
        files: Android.sdk.manifestFile
               && Android.sdk.manifestFile !== Android.sdk.defaultManifestFile
               ? [Android.sdk.manifestFile]
               : (File.exists(Android.sdk.defaultManifestFile)
                  ? [Android.sdk.defaultManifestFile] : [])
    }


    // Internal properties.
    property int platformVersion: {
        if (platform) {
            var match = platform.match(/^android-([0-9]+)$/);
            if (match !== null) {
                return parseInt(match[1], 10);
            }
        }
    }

    property string platformJavaVersion: {
        if (platformVersion >= 21)
            return "1.7";
        return "1.5";
    }

    property path buildToolsDir: FileInfo.joinPaths(sdkDir, "build-tools", buildToolsVersion)
    property string aaptName: "aapt"
    PropertyOptions {
        name: "aaptName"
        allowedValues: ["aapt", "aapt2"]
    }
    property path aaptFilePath: FileInfo.joinPaths(buildToolsDir, aaptName)
    readonly property bool _enableAapt2: aaptName === "aapt2"
    property string packageType: "apk"
    PropertyOptions {
        name: "packageType"
        allowedValues: ["apk", "aab"]
    }
    readonly property bool _generateAab: packageType == "aab"

    property path apksignerFilePath: FileInfo.joinPaths(buildToolsDir, "apksigner")
    property path aidlFilePath: FileInfo.joinPaths(buildToolsDir, "aidl")
    property path dxFilePath: FileInfo.joinPaths(buildToolsDir, "dx")
    property path zipalignFilePath: FileInfo.joinPaths(buildToolsDir, "zipalign")
    property path androidJarFilePath: FileInfo.joinPaths(sdkDir, "platforms", platform,
                                                         "android.jar")
    property path frameworkAidlFilePath: FileInfo.joinPaths(sdkDir, "platforms", platform,
                                                            "framework.aidl")
    property path generatedJavaFilesBaseDir: FileInfo.joinPaths(product.buildDirectory, "gen")
    property path generatedJavaFilesDir: FileInfo.joinPaths(generatedJavaFilesBaseDir,
                                         (packageName || "").split('.').join('/'))
    property path compiledResourcesDir: FileInfo.joinPaths(product.buildDirectory,
                                                           "compiled_resources")
    property string packageContentsDir: FileInfo.joinPaths(product.buildDirectory, packageType)
    property string debugKeyStorePath: FileInfo.joinPaths(
                                           Environment.getEnv(qbs.hostOS.contains("windows")
                                                              ? "USERPROFILE" : "HOME"),
                                           ".android", "debug.keystore")
    property bool useApksigner: buildToolsVersion && Utilities.versionCompare(
                                    buildToolsVersion, "24.0.3") >= 0
    property stringList aidlSearchPaths

    Depends { name: "java"; condition: _enableRules }
    Properties {
        condition: _enableRules
        java.languageVersion: platformJavaVersion
        java.runtimeVersion: platformJavaVersion
        java.bootClassPaths: androidJarFilePath
    }

    validate: {
        if (!sdkDir) {
            throw ModUtils.ModuleError("Could not find an Android SDK at any of the following "
                                       + "locations:\n\t" + sdkProbe.candidatePaths.join("\n\t")
                                       + "\nInstall the Android SDK to one of the above locations, "
                                       + "or set the Android.sdk.sdkDir property or "
                                       + "ANDROID_HOME environment variable to a valid "
                                       + "Android SDK location.");
        }
        if (!bundletoolFilePath && _generateAab) {
            throw ModUtils.ModuleError("Could not find Android bundletool at the following "
                                       + "location:\n\t" + Android.sdk.sdkDir
                                       + "\nInstall the Android bundletool to the above location, "
                                       + "or set the Android.sdk.bundletoolFilePath property.\n"
                                       + "Android bundletool can be downloaded from "
                                       + "https://github.com/google/bundletool");
        }
    }

    FileTagger {
        patterns: ["AndroidManifest.xml"]
        fileTags: ["android.manifest"]
    }

    FileTagger {
        patterns: ["*.aidl"]
        fileTags: ["android.aidl"]
    }

    FileTagger {
        patterns: ["*.keystore"]
        fileTags: ["android.keystore"]
    }

    // Typically there is a debug keystore in ~/.android/debug.keystore which gets created
    // by the native build tools the first time a build is done. However, we don't want to create it
    // ourselves, because writing to a location outside the qbs build directory is both polluting
    // and has the potential for race conditions. So we'll instruct the user what to do.
    Group {
        name: "Android debug keystore"
        files: {
            if (!File.exists(Android.sdk.debugKeyStorePath)) {
                throw ModUtils.ModuleError("Could not find an Android debug keystore at " +
                      Android.sdk.debugKeyStorePath + ". " +
                      "If you are developing for Android on this machine for the first time and " +
                      "have never built an application using the native Gradle / Android Studio " +
                      "tooling, this is normal. You must create the debug keystore now using the " +
                      "following command, in order to continue:\n\n" +
                      SdkUtils.createDebugKeyStoreCommandString(java.keytoolFilePath,
                                                                Android.sdk.debugKeyStorePath) +
                      "\n\n" +
                      "See the following URL for more information: " +
                      "https://developer.android.com/studio/publish/app-signing.html#debug-mode");
            }
            return [Android.sdk.debugKeyStorePath];
        }
        fileTags: ["android.keystore"]
    }

    Parameter {
        property bool embedJar: true
    }

    Rule {
        condition: _enableRules
        inputs: ["android.aidl"]
        Artifact {
            filePath: FileInfo.joinPaths(Utilities.getHash(input.filePath),
                                         input.completeBaseName + ".java")
            fileTags: ["java.java"]
        }

        prepare: {
            var aidl = product.Android.sdk.aidlFilePath;
            var args = ["-p" + product.Android.sdk.frameworkAidlFilePath];
            var aidlSearchPaths = input.Android.sdk.aidlSearchPaths;
            for (var i = 0; i < (aidlSearchPaths ? aidlSearchPaths.length : 0); ++i)
                args.push("-I" + aidlSearchPaths[i]);
            args.push(input.filePath, output.filePath);
            cmd = new Command(aidl, args);
            cmd.description = "Processing " + input.fileName;
            return [cmd];
        }
    }

    property bool customManifestProcessing: false
    Group {
        condition: !Android.sdk.customManifestProcessing
        fileTagsFilter: "android.manifest_processed"
        fileTags: "android.manifest_final"
    }
    Rule {
        condition: _enableRules
        inputs: "android.manifest"
        Artifact {
            filePath: FileInfo.joinPaths("processed_manifest", input.fileName)
            fileTags: "android.manifest_processed"
        }
        prepare: {
            var cmd = new JavaScriptCommand();
            cmd.description = "Ensuring correct package name in Android manifest file";
            cmd.sourceCode = function() {
                var manifestData = new Xml.DomDocument();
                manifestData.load(input.filePath);
                var rootElem = manifestData.documentElement();
                if (!rootElem || !rootElem.isElement() || rootElem.tagName() != "manifest")
                    throw "No manifest tag found in '" + input.filePath + "'.";

                // Quick sanity check. Don't try to be fancy; let's not risk rejecting valid names.
                var packageName = product.Android.sdk.packageName;
                if (!packageName.match(/^[^.]+(?:\.[^.]+)+$/)) {
                    throw "Package name '" + packageName + "' is not valid. Please set "
                            + "Android.sdk.packageName to a name following the "
                            + "'com.mycompany.myproduct' pattern."
                }
                rootElem.setAttribute("package", packageName);
                if (product.Android.sdk.versionCode !== undefined)
                    rootElem.setAttribute("android:versionCode", product.Android.sdk.versionCode);
                if (product.Android.sdk.versionName !== undefined)
                    rootElem.setAttribute("android:versionName", product.Android.sdk.versionName);

                if (product.Android.sdk._bundledInAssets) {
                    // Remove <meta-data android:name="android.app.bundled_in_assets_resource_id"
                    // android:resource="@array/bundled_in_assets"/>
                    // custom AndroidManifest.xml because assets are in rcc files for qt >= 5.14
                    var appElem = rootElem.firstChild("application");
                    if (!appElem || !appElem.isElement() || appElem.tagName() != "application")
                         throw "No application tag found in '" + input.filePath + "'.";
                    var activityElem = appElem.firstChild("activity");
                    if (!activityElem || !activityElem.isElement() ||
                        activityElem.tagName() != "activity")
                         throw "No activity tag found in '" + input.filePath + "'.";
                    var metaDataElem = activityElem.firstChild("meta-data");
                    while (metaDataElem && metaDataElem.isElement()) {
                        if (SdkUtils.elementHasBundledAttributes(metaDataElem)) {
                            var elemToRemove = metaDataElem;
                            metaDataElem = metaDataElem.nextSibling("meta-data");
                            activityElem.removeChild(elemToRemove);
                        } else {
                            metaDataElem = metaDataElem.nextSibling("meta-data");
                        }
                    }
                }

                manifestData.save(output.filePath, 4);
            }
            return cmd;
        }
    }

    Rule {
        condition: _enableRules && !_enableAapt2
        multiplex: true
        inputs: ["android.resources", "android.assets", "android.manifest_final"]

        outputFileTags: ["java.java"]
        outputArtifacts: {
            var artifacts = [];
            var resources = inputs["android.resources"];
            if (resources && resources.length) {
                artifacts.push({
                    filePath: FileInfo.joinPaths(product.Android.sdk.generatedJavaFilesDir,
                                                 "R.java"),
                    fileTags: ["java.java"]
                });
            }

            return artifacts;
        }

        prepare: SdkUtils.prepareAaptGenerate.apply(SdkUtils, arguments)
    }

    Rule {
        condition: _enableRules && _enableAapt2
        inputs: ["android.resources"]
        outputArtifacts: {
            var outputs = [];
            var resources = inputs["android.resources"];
            for (var i = 0; i < resources.length; ++i) {
                var filePath = resources[i].filePath;
                var resourceFileName = SdkUtils.generateAapt2ResourceFileName(filePath);
                var compiledName = FileInfo.joinPaths(product.Android.sdk.compiledResourcesDir,
                                                      resourceFileName);
                outputs.push({filePath: compiledName, fileTags: "android.resources_compiled"});
            }
            return outputs;
        }
        outputFileTags: ["android.resources_compiled"]

        prepare: SdkUtils.prepareAapt2CompileResource.apply(SdkUtils, arguments)
    }

    Rule {
        condition: _enableRules && _enableAapt2
        multiplex: true
        inputs: ["android.resources_compiled", "android.assets", "android.manifest_final"]
        outputFileTags: ["java.java", "android.apk_resources"]
        outputArtifacts: {
            var artifacts = [];
            artifacts.push({
                filePath: product.Android.sdk.apkBaseName + (product.Android.sdk._generateAab ?
                              ".apk_aab" : ".apk_apk"),
                fileTags: ["android.apk_resources"]
            });
            var resources = inputs["android.resources_compiled"];
            if (resources && resources.length) {
                artifacts.push({
                    filePath: FileInfo.joinPaths(product.Android.sdk.generatedJavaFilesDir,
                                                 "R.java"),
                    fileTags: ["java.java"]
                });
            }

            return artifacts;
        }
        prepare: SdkUtils.prepareAapt2Link.apply(SdkUtils, arguments)
    }

    Rule {
        condition: _enableRules
        multiplex: true

        Artifact {
            filePath: FileInfo.joinPaths(product.Android.sdk.generatedJavaFilesDir,
                                         "BuildConfig.java")
            fileTags: ["java.java"]
        }

        prepare: {
            var cmd = new JavaScriptCommand();
            cmd.description = "Generating BuildConfig.java";
            cmd.sourceCode = function() {
                var debugValue = product.qbs.buildVariant === "debug" ? "true" : "false";
                var ofile = new TextFile(output.filePath, TextFile.WriteOnly);
                ofile.writeLine("package " + product.Android.sdk.packageName +  ";")
                ofile.writeLine("public final class BuildConfig {");
                ofile.writeLine("    public final static boolean DEBUG = " + debugValue + ";");
                ofile.writeLine("}");
                ofile.close();
            };
            return [cmd];
        }
    }

    Rule {
        condition: _enableRules
        multiplex: true
        inputs: ["java.class"]
        inputsFromDependencies: ["java.jar"]
        Artifact {
            filePath: product.Android.sdk._generateAab ?
                          FileInfo.joinPaths(product.Android.sdk.packageContentsDir, "dex",
                                             "classes.dex") :
                          FileInfo.joinPaths(product.Android.sdk.packageContentsDir, "classes.dex")
            fileTags: ["android.dex"]
        }
        prepare: SdkUtils.prepareDex.apply(SdkUtils, arguments)
    }

    Rule {
        condition: _enableRules
        property stringList inputTags: "android.nativelibrary"
        inputsFromDependencies: inputTags
        inputs: product.aggregate ? [] : inputTags
        Artifact {
            filePath: FileInfo.joinPaths(product.Android.sdk.packageContentsDir, "lib",
                                         input.Android.ndk.abi, input.fileName)
            fileTags: "android.nativelibrary_deployed"
        }
        prepare: {
            var cmd = new JavaScriptCommand();
            cmd.description = "copying " + input.fileName + " for packaging";
            cmd.sourceCode = function() { File.copy(input.filePath, output.filePath); };
            return cmd;
        }
    }

    Rule {
        condition: _enableRules
        multiplex: true
        property stringList inputTags: "android.stl"
        inputsFromDependencies: inputTags
        inputs: product.aggregate ? [] : inputTags
        outputFileTags: "android.stl_deployed"
        outputArtifacts: {
            var deploymentData = SdkUtils.stlDeploymentData(product, inputs, "stl");
            var outputs = [];
            for (i = 0; i < deploymentData.outputFilePaths.length; ++i) {
                outputs.push({filePath: deploymentData.outputFilePaths[i],
                              fileTags: "android.stl_deployed"});
            }
            return outputs;
        }
        prepare: {
            var cmds = [];
            var deploymentData = SdkUtils.stlDeploymentData(product, inputs);
            for (var i = 0; i < deploymentData.uniqueInputs.length; ++i) {
                var input = deploymentData.uniqueInputs[i];
                var stripArgs = ["--strip-all", "-o", deploymentData.outputFilePaths[i],
                                 input.filePath];
                var cmd = new Command(input.cpp.stripPath, stripArgs);
                cmd.description = "deploying " + input.fileName;
                cmds.push(cmd);
            }
            return cmds;
        }
    }

    Rule {
        condition: _enableRules && !_enableAapt2 && !_generateAab
        multiplex: true
        inputs: [
            "android.resources", "android.assets", "android.manifest_final",
            "android.dex", "android.stl_deployed",
            "android.nativelibrary_deployed", "android.keystore"
        ]
        Artifact {
            filePath: product.Android.sdk.apkBaseName + ".apk"
            fileTags: "android.package"
        }
        prepare: SdkUtils.prepareAaptPackage.apply(SdkUtils, arguments)
    }

    Rule {
        condition: _enableRules && _enableAapt2 && !_generateAab
        multiplex: true
        inputs: [
            "android.apk_resources", "android.manifest_final",
            "android.dex", "android.stl_deployed",
            "android.nativelibrary_deployed", "android.keystore"
        ]
        Artifact {
            filePath: product.Android.sdk.apkBaseName + ".apk"
            fileTags: "android.package"
        }
        prepare: SdkUtils.prepareApkPackage.apply(SdkUtils, arguments)
    }

    Rule {
        condition: _enableRules && _enableAapt2 && _generateAab
        multiplex: true
        inputs: [
            "android.apk_resources", "android.manifest_final",
            "android.dex", "android.stl_deployed",
            "android.nativelibrary_deployed"
        ]
        Artifact {
            filePath: product.Android.sdk.apkBaseName + ".aab"
            fileTags: "android.package"
        }
        prepare: SdkUtils.prepareBundletoolPackage.apply(SdkUtils, arguments)
    }
}