// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only

/*!
\page porting-to-android.html
\title Porting to Android
\brief Provides instructions to port your existing Qt application to Android.
\ingroup androidplatform
\previouspage android-building.html
\nextpage deployment-android.html

Most Qt applications should be portable to Android with ease unless they
depend on a specific hardware or software feature not supported by Android.

The porting approach differs depending on whether the existing application is
QML or Widget based, your intended build tool, and if you are porting from a
mobile or desktop application.

QML-based applications built with CMake are considered the best approach for
mobile applications.

\section1 Porting from Qt desktop applications

Most of your existing Qt code should work, but you must make some decisions
around your UI scalability and layout for different orientations and screen sizes.
Consider a \l{https://www.qt.io/blog/responsive-layouts-in-qt}{responsive layouts}
approach to fully take advantage of Qt Qml Applications.

There are considerations that are not directly impacted by using Qt, such
as app store guidelines, theme guidelines, and others that may impact the way you
develop your application. These won't be discussed here.

\section1 Adding resources

Most applications need resources. Here, we discuss how that impacts porting your
application to Android.

\section2 The Qt resource system

\include porting-notes.qdocinc using resources

\section2 Adding resources using the asset approach

The asset approach is the best option for better
interoperability with the Android APIs. You can access all resources in the
directory using the "assets:" prefix. Unlike qrc, this approach is
Android-specific, not a cross-platform solution.

\section3 The asset approach with CMake

If you are new to using CMake or CMake with Qt for the first time, see
\l{Build with CMake}. Here, the focus is on the steps applicable for adding
resources.

You will likely want to modify a few things that are controlled by the Android
Manifest file. See \l{Qt Android Manifest File Configuration} for more information
on that. So you likely have already set \l QT_ANDROID_PACKAGE_SOURCE_DIR like
so:

\badcode
set_property(TARGET target_name PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
             "${CMAKE_CURRENT_SOURCE_DIR}/android")
\endcode

Once that is done, you can access your assets in your application as detailed in
\l{Assets File System}.

\section3 The asset approach with qmake

To use the "assets:" approach, for example, add the following lines into the
\c {.pro} file:

\badcode
android {
    assets.files = images/happy.png
    assets.path = /assets/images/
    INSTALLS += assets
}
\endcode

Then, you can access that image asset from C++ as follows:

\code
QImage image("assets:/images/happy.png");
\endcode

The following step-by-step instructions guide you to port an existing Qt Quick
application to Android using the qrc approach:

\list 1
    \li Open your project in Qt Creator and select an Android kit. For more
    information, see \l{Qt Creator: Configuring Projects}{Configuring Projects in Qt Creator}.
    \li Identify all the resources used by your application and add them to one
        or more qrc files. Qt Creator updates your qmake project file with the
        "RESOURCES" variable listing the qrc files you added.
    \li To load or refer to the resources in the qrc file from your C++ code,
        use the "qrc:" scheme followed by the absolute URL. For example, to
        load the \c{main.qml} file from \c{resources.qrc}, you can use the
        following C++ code:

        \code
        QQuickView viewer;
        viewer.setSource(QUrl("qrc:/qml/main.qml"));
        viewer.show();
        \endcode

        \note QML documents can refer to the contents in qrc files using the
        relative path to the document. Such references do not require the
        "\c{qrc:}" or "\c{:/}" prefix.
        \li Deploy your application to a device or AVD. For more information, see
        \l {Qt Creator: Deploying Applications to Android Devices}.
\endlist

\note Qt Quick Controls will use the \l{Material Style}{Material Style} if the
target device is running Android 3.0 (API 11) or later. The application should
function normally on devices with Android versions earlier than v3.0 but
without a native style for controls.
*/
