summaryrefslogtreecommitdiffstats
path: root/doc/src/platforms/android/Android-Feature-Delivery.qdoc
blob: a5cadcfa7cf87f1b978bbdc89d1238ab6f7ac5e7 (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
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only

/*!
    \page feature_delivery_example.html
    \ingroup android-platform-extra-topics
    \title Using Play Feature Delivery

    \section1 What is feature delivery?

    \l{https://developer.android.com/guide/playcore/feature-delivery}{Google's developer documentation}
    details this feature which essentially allows developers to structure their projects in a way
    that the Google Play store can split their app content into several downloadable packages. It
    also gives developers control over how the content is delivered to users. These split sofware
    and content packages are delivered to the Google Play store using Android App Bundles (AAB).

    \section1 Example project: FDMapLoader

    This app uses play feature delivery to serve images to a user when requested.
    The app can be easily modified to create an app that is over 200MB to test the app size
    limits and download using a large Feature Delivery module.

    \section2 Build Setup
    \list
        \li \b fdwintermapmodule: Feature Module project for Qt Creator
        \li \b fdmaploader: Main app project for Qt Creator
        \li \b fdmaploader-android-project: Android project for compiling Android App Bundle.
    \endlist

    \section2 Compile FDWintermapModule - Feature Module
    Load the \b fdwintermapmodule project to Qt Creator, configure it for your target platform, and
    build.

    \section2 Compile FDMapLoader - Main App
    Load the \b fdmaploader to Qt Creator, configure it for your target platform, and build.

    \section2 Modify Android Project
    \list
        \li Copy built binaries (.so) files from the feature module and main app build directories
        to corresponding library directories in an Android project.
        (\c app/src/main/jniLibs/[target ABI]/ and
        \c fdwintermapmodule/src/main/jniLibs/[target ABI]/ respectively)
        \li Copy other changed files, such as resource files and Java classes. (\c .../res/... and
        \c .../src/main/java/... folders)
        \li Build Android project either using Android Studio or from the command line using Gradle
        wrapper (\c ./gradlew bundle)
        \li For testing, you can create APKs from a bundle and install a local testing version to a
        device using \c bundletool. Bundletool is a tool provided by Google and we'll cover using it
        later in this documentation. For testing and releasing, you can upload the bundle to the
        Google Play Console.
    \endlist

    \section2 Add your own content
    This example is designed so that developer can easily add their own content to test the Feature
    Delivery. To exceed the Play Store maximum package size, map images (It does not need to be map
    images, but it fits the theme of the example.) can be added to the images folder in FDMapLoader
    and FDWintermapModule, the image names must alse be added to the \c images.qrc file.

    \section1 Create your own
    The following sections describe how to create your own project that uses play feature delivery.
    It is advised to use the example as a basis. In the project, Qt 6.7.2 was used.

    \section2 Feature Module
    Feature delivery handles C++ libraries like normal shared libraries that may or may not be
    available at runtime. Before calls to a library, the availability of one must be checked.

    \list
        \li Use Qt Creator to create a C++ shared library.
        \li Implement features and add resources.
        \li Build to create .so binaries.
    \endlist

    \section2 Main App (Qt)
    \list
        \li Use Qt Creator to create an app (Qt Quick project template was used here).
        \li Implement access to the Feature Delivery library. The central class in Google Play
        Feature Delivery Java library is \l{https://developer.android.com/reference/com/google/android/play/core/splitinstall/SplitInstallManager}{\c SplitInstallManager}.
        \li Android template files can be created by using the 'Create Templates' button on QtCreator
        \uicontrol{Projects -> Build&Run -> [target ABI] -> Build Steps -> Build Android APK}.
        Templates are created in the 'android' folder in the project.
        \li Add Java files to the folder \c .../android/src/java/[package...] and file paths to
        \c CMakeLists.txt:
        \code
        qt_add_executable...
        ...[path]/[java-filename.java]
        ...
        \endcode
        \li In the example, a Java class was created to handle the calls and callbacks. The
        Java class would then be accessed from Qt using JNI.
        \l{https://developer.android.com/guide/playcore/feature-delivery/on-demand#request}{The Android documentation}
        has a simple description of how to request a module.
        \li When adding Java files under the android folder in the project,
        \l QT_ANDROID_PACKAGE_SOURCE_DIR property must be added to the \c CMakeLists.txt:
        \code
        ...
        set_property(TARGET appFDMainApp APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
                     ${CMAKE_CURRENT_SOURCE_DIR}/android)
        ...
        \endcode
        \li Also, the main app \c build.gradle must have dependencies for the feature API:
        in the dependencies block, replace
        \code
        implementation 'androidx.core:core:1.13.1'
        \endcode
        with
        \code
        implementation("com.google.android.play:feature-delivery:2.1.0")
        \endcode
        \li Implement access to the library provided by the feature module. As the Feature Module
        may or may not be available to the main app, modules are not linked at build time and calls
        to the module must be resolved at runtime.
        Example:
        \code
        typedef void* (*LoadModuleRulesFunc)();
        LoadModuleRulesFunc loadModuleRules =
            (LoadModuleRulesFunc) mFDModuleLibrary.resolve("loadModuleRules");
        if (loadModuleRules) {
            void* result = loadModuleRules();
            QScopedPointer<QString> resultStr{static_cast<QString*>(result)};
        }
        \endcode
        \li Implement the user interface and other required parts of the main app.
    \endlist

    \section2 Feature Module (Qt)

    \list
        \li Use Qt Creator to create an app (Qt C++ Library project template was used).
        \li Implement features the module offers.
    \endlist

    \section2 Android Project (Android)

    Creating a project for building an Android app bundle for feature delivery is based on
    Android's documentation, mainly:
    \list
        \li \l {https://developer.android.com/guide/playcore/feature-delivery}{Android Feature Delivery Documentation}
        \li \l {https://developer.android.com/codelabs/on-demand-dynamic-delivery}{Android On Demand Modules Workshop}
    \endlist

    Create an Android project manually or using Android Studio (Using the 'No Activity' template).
    The project is modified so that it contains a top-level project and two sub-projects, \c app and
    \c feature-module. The Android Studio template creates the \c app sub-project and the
    \c feature-module can be added using \uicontrol{File -> New -> New Module} template.

    The template project requires several modifications:

    \list
        \li Add feature delivery plugin to the main level \c build.gradle:
        \code
        plugins {
            id 'com.android.application' version '8.5.2' apply false
            id 'com.android.dynamic-feature' version '8.5.2' apply false
            id 'com.android.library' version '8.5.2' apply false
        }
        \endcode
        \li Add feature module to the \c settings.gradle, change \c rootProject.name if needed:
        \code
        ...
        rootProject.name = "name-of-the-root-project"
        include(:app)
        include(:name-of-the-feature-module)
        \endcode
    \endlist

    \section3 app - Sub-project
    \list
        \li Android project requires Qt binaries from the Main App project:
            \list
                \li Copy native libraries in Qt build: \c [build directory]/android-build/libs/[target ABI]
                to \c app/src/main/jniLibs/[target ABI]
                \li Copy jars in \c [build directory]/android-build/libs/ to \c app/libs/
            \endlist
        \li From the Qt build, also contents of the \c res folder, \c AndroidManifest.xml, and
        \c local.properties are copied to respective places in the Android project.
        \li Add file \c feature_names.xml to the \c app/src/main/res/values folder containing
        a string for the feature module:
        \code
        <?xml version="1.0" encoding="utf-8"?>
        <resources>
            <string name="feature_module_name">name-of-the-feature-module-here</string>
        </resources>
        \endcode
        \li Add file \c keep.xml to \c app/src/main/res/raw folder containing:
        \code
        <?xml version="1.0" encoding="utf-8"?>
        <resources xmlns:tools="http://schemas.android.com/tools"
            tools:keep="@string/feature_module_winter_map"
            tools:discard="" />
        \endcode
    \endlist

    \section2 Modifications to app sub-project build files
    Build files copied to the Android project need some modifications.

    \section3 app - Sub-project

    \section4 build.gradle
    \list
        \li Remove \c buildScript and \c repositories blocks.
        \li Android block in main app's \c build.gradle requires some modifications:
            \list
                \li \c defaultConfig
                \li \c packagingOptions
                \li \c dynamicFeatures
                \li \c sourceSets
                \li \c aaptOptions
                \li \c dependencies
            \endlist
    \endlist

    \code
    android {
    ...
      defaultConfig {
      ...
        applicationId "your-project-name-here"
      ...
      }
      packagingOptions.jniLibs.useLegacyPackaging true

      dynamicFeatures = [":your-dynamic-feature-name-here"]

      sourceSets {
        main {
          manifest.srcFile 'src/main/AndroidManifest.xml'
          java.srcDirs = [qtAndroidDir + '/src', 'src', 'java']
          aidl.srcDirs = [qtAndroidDir + '/src', 'src', 'aidl']
          res.srcDirs = [qtAndroidDir + '/res', 'res']
          resources.srcDirs = ['resources']
          renderscript.srcDirs = ['src']
          assets.srcDirs = ['assets']
          jniLibs.srcDirs = ['src/main/jniLibs/']
        }
      }

      // Do not compress Qt binary resources file
      aaptOptions {
        noCompress 'rcc'
      }
    ...
    }

    dependencies {
    ...
      implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
      implementation 'com.google.android.play:feature-delivery:2.1.0'
      implementation libs.material
    ...
    }
    \endcode

    Also add signing configuration to android block:

    \code
    android {
    ...
      signingConfigs {
        release {
          store

          \code
    android {
    ...
      signingConfigs {
        release {
          storeFile file("/absolute/path/to/the/keystore.jks")
          storePassword "myStorePassword"
          keyAlias "myKeyAlias"
          keyPassword "myKeyPassword"
        }
      }
      buildTypes {
        release {
          signingConfig signingConfigs.release
          ...
        }
      }
    ...
    }
    \endcode

    \section4 gradle.properties
    Qt has added project variables to \c gradle.properties. Change the value of
   \c androidPackageName if needed.

    \section4 AndroidManifest.xml
    \list
        \li Remove \c package:
        \code
        ...
        <manifest
        ...
          android:package... <--remove
        ...
        >
        ...
        \endcode
        \li Change \c label and \c android.app.lib_name if needed:
        \code
        ...
        <application ...
          android:label=" ...
          <activity ... >
            <meta-data android:name="android.app.lib_name" android:value=" ...
            />
        ...
        \endcode
    \endlist

    \section3 feature-module - Sub-project
    App and feature modules are created as sub-projects for the top-level Android project.
    The folder and file structure is similar to the app sub-project.

    \list
        \li Feature-module binaries from the Qt build are copied to the
        \c [name-of-feature-module]/src/main/jniLibs/
        \li Like in the main app, \c src/main/res/ folder should have \c xml and \c values
        folders containing \c qtprovider_paths.xml and \c libs.xml respectively. Both files
        can be copied from the app project.
        \li If \c src/main/res/ folder contains drawable or mipmap folders and the feature does not
        require them, they can be removed.
        \li In the feature module \c src/main/res/values should not contain \c app_name field. In
        simple projects where strings.xml is not needed for other uses, it can be removed.
        \li \c libs.xml contains only the name of the feature module:
        \code
        ...
            <array name="load_local_libs">
                <item>name-of-the-feature-module-here</item>
            </array>

            <string name="static_init_classes"></string>
            <string name="use_local_qt_libs">0</string>
            <string name="bundle_local_qt_libs">0</string>
        ...
        \endcode
        \li \c AndroidManifest.xml is added to the \c src/main/ directory:
        \code
        <?xml version="1.0" encoding="utf-8"?>
        <manifest xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:dist="http://schemas.android.com/apk/distribution">

            <dist:module
                dist:instant="false"
                dist:title="@string/feature_module_title_string">
                <dist:delivery>
                    <dist:on-demand />
                </dist:delivery>
                <dist:fusing dist:include="false" />
            </dist:module>
            <!-- This feature module does contain code. -->
            <application android:hasCode="true"/>
        </manifest>
        \endcode
        \li Feature module \c build.gradle is quite similar to the one from the app project,
        with some changes. Here's an example from the example project:
        \code
        plugins {
            id 'com.android.dynamic-feature'
        }

        dependencies {
            implementation project(':app')
            implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
            implementation 'com.google.android.play:feature-delivery:2.1.0'
        }

        android {

            namespace = androidPackageName
            compileSdk = androidCompileSdkVersion
            ndkVersion androidNdkVersion

            // Extract native libraries from the APK
            packagingOptions.jniLibs.useLegacyPackaging true

            defaultConfig {
                resConfig "en"
                minSdkVersion qtMinSdkVersion
                targetSdkVersion qtTargetSdkVersion
            }

            sourceSets {
                main {
                    manifest.srcFile 'src/main/AndroidManifest.xml'
                    resources.srcDirs = ['resources']
                    renderscript.srcDirs = ['src']
                    assets.srcDirs = ['assets']
                    jniLibs.srcDirs = ['src/main/jniLibs/']
               }
            }

            tasks.withType(JavaCompile) {
                options.incremental = true
            }

            compileOptions {
                sourceCompatibility JavaVersion.VERSION_1_8
                targetCompatibility JavaVersion.VERSION_1_8
            }

            lintOptions {
                abortOnError false
            }

            // Do not compress Qt binary resources file
            aaptOptions {
                noCompress 'rcc'
            }
        }
        \endcode
        \li \c gradle.properties file can be copied over from the app sub-project,
        change the \c androidPackageName to the feature module package.
    \endlist

    \section2 Building and deployment
    AAB bundle can be built from the command line using gradle wrapper: \c ./gradlew bundle
    The produced AAB will be in \c build/outputs/bundle/release (or \c debug) folder. The AAB can
    then be copied to the Google Play Store and released for testing. Testing can also be done
    locally by using \c bundletool with \c --local-testing parameter.
    \l {https://developer.android.com/tools/bundletool}{Bundletool documentation}

    \section3 Bundletool commands used
    \list
        \li Generate APK:s from a bundle:
        \code
        bundletool build-apks --bundle=/path/to/bundle.aab --output=/path/to/apk/package.apks --local-testing
        \endcode
        \li Install app to the device:
        \code
        bundletool install-apks --apks=/path/to/apk/package.apks
        \endcode
    \endlist
*/