/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Creator documentation.
**
** 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 https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Free Documentation License Usage
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of
** this file. Please review the following information to ensure
** the GNU Free Documentation License version 1.3 requirements
** will be met: https://www.gnu.org/licenses/fdl-1.3.html.
**
****************************************************************************/

    /*!
    \page creator-developing-android.html
    \previouspage creator-connecting-mobile.html
    \if defined(qtdesignstudio)
    \nextpage creator-developing-generic-linux.html
    \else
    \nextpage creator-developing-baremetal.html
    \endif

    \title Connecting Android Devices

    You can connect Android devices to the development PC using USB cables
    to build, run, debug, and analyze applications from \QC. Devices with
    Android version 4.1 (API level 16) or later are supported.

    To develop for Android, you must have a tool chain for building applications
    for Android devices installed on the development PC. \QC can automatically
    dowload and install the tool chain and create a suitable build and run
    \l{glossary-buildandrun-kit}{kit} that contains the tool chain and the Qt
    version for Android for the device's architecture.

    Starting from Qt 5.14.0, the Qt for Android package contains all the
    architectures (ABIs) installed as one.

    \note You can build a 64-bit version of Qt for Android yourself. However,
    for such a Qt version, the minimum required Android version on devices
    is 5.0 (API level 21). For more information, see \l{Qt for Android}.

    To enable helpful code editing features for Java, such as code completion,
    highlighting, function tooltips, and navigating in code, specify settings
    for a \l{Specifying Java Language Server Settings}{Java language server}.

    The Android Debug Bridge (adb) command line tool is integrated to \QC to
    enable you to deploy applications to connected Android devices, to run
    them, and to read their logs. It includes a client and server that run on
    the development host and a daemon that runs on the emulator or device.

    \section1 Requirements

    To use \QC to develop Qt applications for Android, you need
    \l{Qt for Android} 5.2, or later, and the tool chain that \QC
    can automatically download, install, and configure for you.
    For more information, see \l{Installing the Prerequisites}.

    \section1 Specifying Android Device Settings

    \QC offers to automatically install all the necessary packages and tools and
    to set up your development environment by creating debuggers, tool chains,
    and kits. You can use \QC to:

    \list
        \li Download and extract the Android SDK Command-line Tools.
        \li Install or update the essential packages such as NDKs,
            build tools, and platform tools.
    \endlist

    To set up the development environment for Android:

    \list 1
        \li Select \uicontrol Tools > \uicontrol Options > \uicontrol Devices >
            \uicontrol Android.
            \image qtcreator-options-android-main.png "Android options"
        \li In the \uicontrol {JDK location} field, set the path to the JDK.
            \QC checks the JDK installation and reports errors.

            By default, \QC tries to find a supported \l{AdoptOpenJDK} or
            \l{OpenJDK} installation. If none is found, you must set the path
            manually. If you don't have a supported JDK installed, select
            \inlineimage online.png
            to open the JDK download web page in the default browser.

            \note We recommended using a 64-bit JDK, because the 32-bit one
            might cause issues with \c cmdline-tools, and some packages might
            not be listed.
        \li In the \uicontrol {Android SDK location} field, set the path to the
            folder where you want the \l{Android SDK Command-line Tools} to be
            installed.
        \li Select \uicontrol {Set Up SDK} to automatically download and extract
            the Android SDK Command-line Tools to the selected path.

            The SDK Manager checks whether the tool chain is installed.
            If packages are missing or updates are needed, the SDK Manager
            offers to add or remove those packages. Before taking action, it
            prompts you to accept the changes it is about to make. In addition,
            it prompts you to accept Google licenses, as necessary.
        \li The installed NDK versions are listed below the SDK path.
            The locked items were installed by the SDK Manager,
            and can only be modified from the \uicontrol {SDK Manager} tab.
            For more information, see \l{Managing Android NDK Packages}.
        \li In the \uicontrol {Android OpenSSL} group, set the path to the
            prebuilt OpenSSL libraries.

            For Qt applications that require OpenSSL support, \QC allows to
            quickly add the \l {Android OpenSSL support} to your project.
            For more information, see \l{Adding External Libraries}.
        \li Select \inlineimage online.png
            to download the OpenSSL repository to the selected path. If the
            automatic download fails, the download web page opens for manual
            download.
        \li Select the \uicontrol {Automatically create kits for Android tool chains}
            check box to allow \QC to create the kits for you. \QC displays a
            warning if it cannot find a suitable Qt version.
    \endlist

    \section2 Manual Setup

    \note We recommend that you use the latest Android SDK Command-Line Tools.
    Using Android SDK Tools version 25.2.5 or earlier is not supported, because
    they cannot be fully integrated with \QC.

    However, if the automatic setup does not meet your needs, you can download
    and install Android SDK Command-line Tools, and then install or update the
    NDKs, tools and packages needed for development. For more information, see
    \l{Getting Started with Qt for Android}.

    \section2 Viewing Android Tool Chain Settings

    The Android SDK Command-Line Tools download URL, the essential
    packages list, and the appropriate NDK for each Qt version are defined in a JSON
    configuration file. The file is located under the user's \QC
    resource folder:

    \badcode
    # Linux and macOS
    ~/.config/QtProject/qtcreator/android/sdk_definitions.json

    # Windows
    C:\Users\Username\AppData\Local\QtProject\qtcreator\android\sdk_definitions.json
    \endcode

    For example, the SDK configuration file defines the NDK version 19.2.5345600
    to be used for Qt 5.12.0 to 5.12.5 and Qt 5.13.0 to 5.13.1 versions:

    \badcode
    "specific_qt_versions": [
        {
            "versions": ["5.12.[0-5]", "5.13.[0-1]"],
            "sdk_essential_packages": ["build-tools;28.0.2", "ndk;19.2.5345600"],
            "ndk_path": "ndk/19.2.5345600"
        }
    ]
    \endcode

    You can view the latest version of the configuration file that is up-to-date
    with the Android SDK and NDK changes, \l{sdk_definitions.json}, in Git.

    \section2 Managing Android NDK Packages

    To view the installed \l{Android NDK} versions, select \uicontrol Tools >
    \uicontrol Options > \uicontrol Devices > \uicontrol Android.

    \image qtcreator-options-android-sdk-tools.png "Android NDK and SDK checks"

    The locked versions were installed by the SDK Manager, and can only
    be modified from the SDK Manager tab. For more information, see
    \l{Managing Android SDK Packages}.

    To manually download NDKs, select \inlineimage online.png
    .

    To add custom NDK paths manually to the global list of NDKs, select
    \uicontrol Add. This creates custom tool chains and debuggers associated
    to that NDK. However, you have to manually create a kit that uses the
    custom NDK. For more information, see \l{Adding Kits}.

    \section2 Managing Android SDK Packages

    Since Android SDK Tools version 25.3.0, only a command-line tool,
    \l {sdkmanager}, is provided by Android for SDK package management.
    To make SDK management easier, \QC provides an SDK Manager for
    installing, updating, and removing SDK packages. You can still use
    sdkmanager for advanced SDK management.

    To view the installed Android SDK packages, select \uicontrol Tools >
    \uicontrol Options > \uicontrol Devices > \uicontrol Android >
    \uicontrol {SDK Manager}.

    \image qtcreator-android-sdk-manager.png "Android SDK Manager"

    To filter the packages, select \uicontrol Available, \uicontrol Installed,
    or \uicontrol All in \uicontrol {Show Packages}.

    To update the installed Android SDK packages, select
    \uicontrol {Update Installed}. Select the packages to update, and then
    select \uicontrol Apply.

    To specify advanced sdkmanager settings, select
    \uicontrol {Advanced Options} and enter arguments in the
    \uicontrol {SDK Manager arguments} field. The available arguments are listed
    and described in \uicontrol {Available arguments}.

    \section1 Managing Android Virtual Devices (AVD)

    A list of AVDs is shown under \uicontrol Tools > \uicontrol Options >
    \uicontrol Devices > \uicontrol Android > \uicontrol {AVD Manager}. This
    works only if the \uicontrol {Android SDK location} is set correctly in
    \uicontrol {Android Settings}.

    The value in \uicontrol {System/data partition size} is used to set the
    emulator's system partition size upon execution with \uicontrol {Start}.

    \image qtcreator-android-avd-manager.png "Android NDK and SDK checks"

    \section2 Creating a New AVD

    To create new virtual devices:

    \list 1
        \li Select \uicontrol Tools > \uicontrol Options > \uicontrol Devices >
            \uicontrol Android.
        \li In the \uicontrol {SDK Manager} tab, select \uicontrol Install for
            an Android version, and then select \uicontrol Apply to install a
            system image. Alternatively, you can install the system image from
            Android Studio.
        \li In the \uicontrol {AVD Manager} tab, select \uicontrol {Add} to
            open the \uicontrol {Create New AVD} dialog.
            \image qtcreator-android-create-avd.png "Android NDK and SDK checks"
        \li Set the name, type, skin, architecture, API level, and SD card size
            of the device.
        \li Select \uicontrol OK to create the AVD.
    \endlist

    For more advanced options for creating a new AVD, use the command-line tool
    \l{avdmanager}, or use Android Studio's native AVD Manager's UI.

    \section1 Selecting Android Devices

    When you deploy an application to an Android device with Android
    version 4.2 (API 16) or later, the \uicontrol {Select Android Device} dialog
    opens. It lists the devices that are connected to the development PC, as
    well as AVDs, and their status. You can select devices or AVDs from the
    \uicontrol {Compatible Devices} list for deployment.

    If \QC determines that a device is not ready for deployment, it places the
    device in the \uicontrol {Incompatible Devices} list, with information about the
    issue. For example, you might need to authorize the connection on the
    device. After you authorize the connection on the device, select
    \uicontrol {Refresh Device List}. The device is moved to the
    \uicontrol {Compatible Devices} list and you can select it for deployment.

    Other reasons for showing a device in \uicontrol {Incompatible Devices} are
    that the Android version on the device is too old or that the tool chain
    used for building does not match the Android architecture on the device
    (for example, you cannot deploy an ARM build on an x86 device).

    \image qtcreator-android-select-devices.png "Select Android Devices dialog"

    To \l{Creating a New AVD}{create a new AVD}, select \uicontrol {Create Android Virtual Device}.

    To set a device as the default device for a particular Android architecture
    for the current project, select the \uicontrol {Always use this device for architecture}
    check box. The \uicontrol {Select Android Devices} dialog will not appear
    until you switch to another project or restart \QC.

    \section1 Using the Android Emulator

    To run your application on the Android Emulator, you must have an Android
    virtual device (AVD). For more information about creating a new one, see
    \l{Creating a New AVD}. If you run an application without a device connected to the
    development PC and without an AVD specified, \QC asks you to add an AVD.

    \note The Android Emulator has a bug that prevents it from starting on some
    systems. If the Android Emulator does not start, you can try starting it
    manually by running the following commands:

    \badcode
    cd <ANDROID_SDK>/emulator
    ./emulator -avd <AVD_NAME>
    \endcode

    For more information, see \l{Start the emulator from the command line}.

    \section1 Debugging on Android Devices

    Debugging is enabled in different ways on different Android devices.
    Look for \uicontrol {USB Debugging} under \uicontrol {Developer Options}. On
    some devices \uicontrol {Developer Options} is hidden and becomes visible
    only when you tap the \uicontrol {Build number} field in \uicontrol Settings
    > \uicontrol About several times. For more information, see
    \l {Configure on-device developer options}.

    Select a \l{glossary-build-config}{debug build configuration} to build
    the application for debugging.

    \note \QC cannot debug applications on Android devices if Android Studio is
    running. If the following message is displayed in the \uicontrol Output
    pane, close Android Studio and try again:

    \badcode
    Ignoring second debugger -accepting and dropping.
    \endcode
*/
