// 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_ANROID_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
*/
