Qt Android with external app



  • Hi,

    After few days of googling and reading post, I'm still bit stuck here, i'm hoping someone can point me to the right place. I have wrote a very simple app using Android studio where the app has a button and use a 3rd party SDK to do the barcode scanning. When I try on the app which i create on the Android Studio, it works perfectly fine but when i try to use the QAndroidJniObject in my Qt app it doesn't work. Here is the java code,

    package com.test.timebarcodescan;
    
            import android.support.v7.app.AppCompatActivity;
            import android.os.Bundle;
            import android.view.View;
            import android.widget.Button;
            import android.widget.TextView;
            import com.sdk.support.openapi.Scanner;
    
    public class MainActivity extends AppCompatActivity {
    
        TextView tv;
        int count = 0;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            tv = (TextView) findViewById(R.id.textView);
            tv.setText("");
        }
    
        public void onButtonOKClick(View view){
            count += 1;
            String scanResult = scanBarcode();
            tv.setText(scanResult);
        }
    
        static String scanBarcode(){
            Scanner scanner=Scanner.getInstance();
            String result = "";
            if(scanner == null){
                result = "";
            } else{
                try {
                    result = scanner.scan();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return result;
        }
    }
    

    I don't know how to link this app with my Qt App, properly with this app or the java code. My Qt app can either to call this app as Indent and get the result after pressing the "Scan" button or use JNI to call the "scanBarcode()" function using QAndroidJniObject. But I have no idea how to it. I read the example http://doc.qt.io/qt-5/qtandroidextras-notification-example.html, but the app is using qml which i just use the normal MainWindow app type.

    In my Qt app, my on_scanBarcode_clicked() doesn't work when i use the QAndroidJniObject

    void MainWindow::on_scanBarcode_clicked()
    {
        qDebug() <<"Scan button clicked.";
    
        QAndroidJniObject testObject = QAndroidJniObject::callStaticObjectMethod<jstring>("com/test/timebarcodescan/MainActivity", "scanBarcode");
    
        if(testObject.isValid()){
            qDebug() <<"testObject is valid...";
        } else{
            qDebug() <<"testObject is invalid..";
        }
        
    #endif
    }
    
    
    ****** My dummy Qt application .pro file********
    #-------------------------------------------------
    #
    # Project created by QtCreator 2016-09-16T12:26:49
    #
    #-------------------------------------------------
    
    QT       += core gui
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    android {
        QT += androidextras
    }
    
    TARGET = untitled
    TEMPLATE = app
    
    
    SOURCES += main.cpp\
            mainwindow.cpp
    
    HEADERS  += mainwindow.h
    
    FORMS    += mainwindow.ui
    
    CONFIG += mobility
    MOBILITY = 
    
    DISTFILES += \
        ../../../AndriodApp/TimeBarcodeScan/app/src/main/java/com/test/timebarcodescan/AndroidManifest.xml \
        ../../../AndriodApp/TimeBarcodeScan/app/src/main/java/com/test/timebarcodescan/gradle/wrapper/gradle-wrapper.jar \
        ../../../AndriodApp/TimeBarcodeScan/app/src/main/java/com/test/timebarcodescan/gradlew \
        ../../../AndriodApp/TimeBarcodeScan/app/src/main/java/com/test/timebarcodescan/res/values/libs.xml \
        ../../../AndriodApp/TimeBarcodeScan/app/src/main/java/com/test/timebarcodescan/build.gradle \
        ../../../AndriodApp/TimeBarcodeScan/app/src/main/java/com/test/timebarcodescan/gradle/wrapper/gradle-wrapper.properties \
        ../../../AndriodApp/TimeBarcodeScan/app/src/main/java/com/test/timebarcodescan/gradlew.bat \
        ../../../AndriodApp/TimeBarcodeScan/app/src/main/java/com/test/timebarcodescan/MainActivity.java
    
    ANDROID_PACKAGE_SOURCE_DIR = $$PWD/../../../AndriodApp/TimeBarcodeScan/app/src/main/java/com/test/timebarcodescan
    
    
    

    I'm desperate to get some advice/suggestion.

    Or how do i create java code for my qt instead from the Android Studio? i read few post from the google, all the java files seems create from the qt. But when i create it, it's different from the examples.

    Many thanks.



  • I'm not sure about this, but I think there is a special setting
    JAVASOURCES +=
    $$PWD/path/to/your/java/Class.java
    that you can use to declare your java sources in the .pro configuration.

    Also, you can add some output to your java-method (e.g. a plain System.out.println) to see if your Java scanBarCode-method get's called from the C++ on_scanBarcode_clicked() handler over JNI. Maybe this helps to locate the problem?

    Best,
    GT


  • Moderators

    @liewjls
    You can try to explicitly specify the JNI signature:
    QAndroidJniObject::callStaticObjectMethod<jstring>("com/test/timebarcodescan/MainActivity", "scanBarcode", "()Ljava/lang/String;")
    But i expect the same behavior.

    Is the MainActivity class the activity you are using in your Qt app?
    You should use the QtActivity instead (see examples, or generate the templates in QtCreator).
    What i can think of is, that the QtActivity initializes some stuff (like the classloader) used by the QtAndroidJni* classes.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.