Important: Please read the Qt Code of Conduct -

Android Services: I can start a service using the same .so but i can't use a separate .so for the service code

  • Hi,
    I want to create an android service with its code in a separate .so file.

    I used this reference to start an android service with its code inside the same .so. successfully!

    I followed this reference to make separate .so files but failed:

    Here's my code:

    Manifest: (only relevant lines)

    <manifest package="org.qtproject.example" xmlns:android="" android:versionName="1.0" android:versionCode="1" android:installLocation="auto">
        <application android:hardwareAccelerated="true" android:name="" android:label="-- %%INSERT_APP_NAME%% --">
            <activity android:configChanges="orientation|uiMode|screenLayout|screenSize|smallestScreenSize|layoutDirection|locale|fontScale|keyboard|keyboardHidden|navigation" android:name="org.qtproject.example.MyCustomAppActivity" android:label="-- %%INSERT_APP_NAME%% --" android:screenOrientation="unspecified" android:launchMode="singleTop">
                    <action android:name="android.intent.action.MAIN"/>
                    <category android:name="android.intent.category.LAUNCHER"/>
                <meta-data android:name="" android:value="qtandroidservices_example"/>
        <service android:process=":qt" android:enabled="true" android:name="org.qtproject.example.MyCustomAppService">
    <meta-data android:name="" android:value="server"/>
                 <!-- Background running -->
                 <meta-data android:name="" android:value="true"/>
                 <!-- Background running -->


    TEMPLATE = app
        QT += qml quick webview multimedia
        QT += qml quick webview multimedia androidextras
    CONFIG += c++11
    SOURCES += \
    RESOURCES += qml.qrc
    # Default rules for deployment.
    DISTFILES += \
        android/AndroidManifest.xml \
        android/gradle/wrapper/gradle-wrapper.jar \
        android/gradlew \
        android/res/values/libs.xml \
        android/build.gradle \
        android/gradle/wrapper/ \
        android/gradlew.bat \
        android/ \
    android {
        ANDROID_JAVA_SOURCES.path = /src/org/qtproject/example
        ANDROID_JAVA_SOURCES.files = $$files($$PWD/source/java/*.java)

    package org.qtproject.example;
    import org.qtproject.example.R;
    import org.qtproject.example.MyCustomAppService;
    import android.content.Intent;
    import android.util.Log;
    import android.os.Bundle;
    public class MyCustomAppActivity extends QtActivity {
        public void onCreate(Bundle bundle) {
            Log.i("Activity", "Starting service!");
            Intent serviceIntent = new Intent(this, org.qtproject.example.MyCustomAppService.class);
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);


    package org.qtproject.example;
    import android.os.IBinder;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    public class MyCustomAppService extends QtService {
       /** Called when the service is being created. */
       public void onCreate() {
          Log.i("Service", "Service created!");
       /** The service is starting, due to a call to startService() */
       public int onStartCommand(Intent intent, int flags, int startId) {
         Log.i("Service", "Service created!");
           int ret = super.onStartCommand(intent, flags, startId);
          return ret;
          //return mStartMode;


    TEMPLATE = lib
    TARGET = server
    CONFIG += dll
    QT += core
    SOURCES += \


    #include <QDebug>
    #ifdef Q_OS_ANDROID
    #include <QAndroidJniObject>
    #include <QtAndroid>
    int main(int argc, char *argv[])
        QGuiApplication app(argc, argv);
        qWarning("I am serving");
        return app.exec();

  • @Mena
    Hello sir
    i am struggling with the same problem here too.
    1_can i have you code with the same .so file?
    2_any progress since last time you posted this?

    When i use this example for test i get the error :
    "E Qt : Can't create main activity"
    but the service starts successfully with no UI because of the error.
    i also use Qt5.9.2

  • Did you succeed with the service? I also had a lot of problems with it but I finally managed to make a working service. There are still a lot of things I need to sort out:

    1. How to correctly shut down the service
    2. How to find out in the main app that the service stopped and vice-versa.
    3. How to reconnect to a running service when the main app is started.

    But the basics do work. I started by modifying the samples you mentioned. Do they work for you? That is the starting point. After each change I always tried if it still works. I found it extremely difficult to debug because currently QtCreator doesn't print any debug messages from a service. I use adb logcat to read debug messages from my service.

    You can find my code here. The version with service (0.6) is not ready yet and there is no release APK. There are still many bugs and problems I need to sort out (like those mentioned above).

    Also I'm not a C++ or Java professional. This is my hobby and there might be many faults and incorrect constructions. But it seems to work somehow. :-)

Log in to reply