AndroidServices

From Qt Wiki
Jump to navigation Jump to search

This page contains information about Android Services using Qt.

Add an AndroidManifest.xml

If you have not already, you will need to copy the android template files into your project. This can be done in Qt Creator with Projects (left bar) -> Build & Run -> Android / Build -> Build Android APK -> Create Templates. For more information, see https://doc.qt.io/qt-5/deployment-android.html .

Add the service to your manifest

Qt 5.14.x versions

<service android:process=":qt" android:name=".MyCustomService" android:exported="true">
  <!-- android:process=":qt" is needed to force the service to run on a separate process than the Activity -->

  <!-- Application arguments. Uncomment the following line to use the same main function. -->
  <!-- meta-data android:name="android.app.arguments" android:value="-service"/ -->
  <!-- Application arguments -->

  <!-- If you are using the same application (.so file) for activity and also for service, then you
       need to use *android.app.arguments* to pass some arguments to your service in order to know which
       one is which.
    -->

  <!-- Application to launch -->
  <meta-data android:name="android.app.lib_name" android:value="-- %%INSERT_APP_LIB_NAME%% --"/>
  <!-- Application to launch -->

  <!-- Deploy Qt libs as part of package -->
  <meta-data android:name="android.app.bundle_local_qt_libs" android:value="-- %%BUNDLE_LOCAL_QT_LIBS%% --"/>
  <!-- Deploy Qt libs as part of package -->

  <!-- Run with local libs -->
  <meta-data android:name="android.app.use_local_qt_libs" android:value="-- %%USE_LOCAL_QT_LIBS%% --"/>
  <meta-data android:name="android.app.libs_prefix" android:value="/data/local/tmp/qt/"/>
  <meta-data android:name="android.app.load_local_libs_resource_id" android:resource="@array/load_local_libs"/>
  <meta-data android:name="android.app.load_local_jars" android:value="-- %%INSERT_LOCAL_JARS%% --"/>
  <meta-data android:name="android.app.static_init_classes" android:value="-- %%INSERT_INIT_CLASSES%% --"/>
  <!-- Run with local libs -->

  <!--  Messages maps -->
  <meta-data android:value="@string/fatal_error_msg" android:name="android.app.fatal_error_msg"/>
  <meta-data android:value="@string/unsupported_android_version" android:name="android.app.unsupported_android_version"/>
  <!--  Messages maps -->

  <!-- Background running -->
  <meta-data android:name="android.app.background_running" android:value="true"/>
  <!-- Background running -->
</service>

Compare to older versions, some modifications have to be done:

  • Following lines must be removed
    <meta-data android:name="android.app.bundled_in_lib_resource_id" android:resource="@array/bundled_in_lib"/>
    <meta-data android:name="android.app.bundled_in_assets_resource_id" android:resource="@array/bundled_in_assets"/>
    
  • The following line ...
    <meta-data android:name="android.app.load_local_libs" android:value="-- %%INSERT_LOCAL_LIBS%% --"/>
    
    should be replaced by this one
    <meta-data android:name="android.app.load_local_libs_resource_id" android:resource="@array/load_local_libs"/>
    
  • Eventually, one has to add this line:
    <meta-data android:value="@string/unsupported_android_version" android:name="android.app.unsupported_android_version"/>
    

older versions

Every service must be explicitly added to your manifest file, next you have an example:

<manifest package=...>
    <application android:....>
        <activity android:....>
         .....
        </activity>

        <service android:process=":qt" android:name="org.qtproject.qt5.android.bindings.QtService">
        <!-- android:process=":qt" is needed to force the service to run on a separate process than the Activity -->

            <!-- Application arguments -->
            <!-- meta-data android:name="android.app.arguments" android:value="-service"/ -->
            <!-- Application arguments -->

            <!-- If you are using the same application (.so file) for activity and also for service, then you
                 need to use *android.app.arguments* to pass some arguments to your service in order to know which
                 one is which.
            -->

            <!-- Application to launch -->
            <meta-data android:name="android.app.lib_name" android:value="-- %%INSERT_APP_LIB_NAME%% --"/>
            <!-- Application to launch -->

            <!-- Ministro -->
            <meta-data android:name="android.app.qt_sources_resource_id" android:resource="@array/qt_sources"/>
            <meta-data android:name="android.app.repository" android:value="default"/>
            <meta-data android:name="android.app.qt_libs_resource_id" android:resource="@array/qt_libs"/>
            <meta-data android:name="android.app.bundled_libs_resource_id" android:resource="@array/bundled_libs"/>
            <!-- Ministro -->

            <!-- Deploy Qt libs as part of package -->
            <meta-data android:name="android.app.bundle_local_qt_libs" android:value="-- %%BUNDLE_LOCAL_QT_LIBS%% --"/>
            <meta-data android:name="android.app.bundled_in_lib_resource_id" android:resource="@array/bundled_in_lib"/>
            <meta-data android:name="android.app.bundled_in_assets_resource_id" android:resource="@array/bundled_in_assets"/>
            <!-- Deploy Qt libs as part of package -->

            <!-- Run with local libs -->
            <meta-data android:name="android.app.use_local_qt_libs" android:value="-- %%USE_LOCAL_QT_LIBS%% --"/>
            <meta-data android:name="android.app.libs_prefix" android:value="/data/local/tmp/qt/"/>
            <meta-data android:name="android.app.load_local_libs" android:value="-- %%INSERT_LOCAL_LIBS%% --"/>
            <meta-data android:name="android.app.load_local_jars" android:value="-- %%INSERT_LOCAL_JARS%% --"/>
            <meta-data android:name="android.app.static_init_classes" android:value="-- %%INSERT_INIT_CLASSES%% --"/>
            <!-- Run with local libs -->

            <!--  Messages maps -->
            <meta-data android:value="@string/ministro_not_found_msg" android:name="android.app.ministro_not_found_msg"/>
            <meta-data android:value="@string/ministro_needed_msg" android:name="android.app.ministro_needed_msg"/>
            <meta-data android:value="@string/fatal_error_msg" android:name="android.app.fatal_error_msg"/>
            <!--  Messages maps -->


            <!-- Background running -->
            <meta-data android:name="android.app.background_running" android:value="true"/>
            <!-- Background running -->
        </service>
    </application>
    ....
</manifest>