QAndroidJniObject::callStaticMethod got SIGSEGV
-
My code is like this:
void MainClass::installApk(QString apkFile) { QAndroidJniObject apkPath = QAndroidJniObject::fromString(apkFile); QAndroidJniObject activity = QtAndroid::androidActivity(); QAndroidJniObject::callStaticMethod<void>("per.pqy.Extra", "installApk", "(Lorg/qtproject/qt5/android/bindings/QtActivity;Ljava/lang/String;)V", activity.object<jobject>(), apkPath.object<jstring>()); }
package per.pqy; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.app.Activity; import java.io.File; import org.qtproject.qt5.android.bindings.QtActivity; public class Extra { public Extra(){} public static void installApk(QtActivity activity, String apkFile) { Intent intent = new Intent(Intent.ACTION_VIEW); final Uri apkuri = Uri.fromFile(new File(apkFile)); intent.setDataAndType(apkuri, "application/vnd.android.package-archive"); activity.startActivity(intent); } }
When the function "void MainClass::installApk(QString apkFile)" is executed ,Qt app crashs with SIGSEGV. How to solve? Thanks.
-
I haven't touched the JNI/Android and Qt yet though hopefully I will this month. But I remember BogDan Vatra saying anything that a call you make from C++ to Java will be on the Qt thread, thus you will need to move your code to the Android UI Thread, meaning all you probably have to do is refactor your code and wrap it around a Runnable and throw it at
runOnUiThread
. Point is, it will crash because some Android APIs needs to be called from the Android thread.call a Java method from C/C++ Qt thread. The Java method will be executed in Qt thread, so we we need a way to access Android APIs in Android UI thread.
our Java method uses Activity.runOnUiThread to post a runnable on Android UI thread. This runnable will be executed by the Android event loop on Android UI thread.
the runnable accesses the Android APIs from Android UI thread.Take with a grain of salt. I have yet to touch the JNI, just recalling the blog post made since I have to work on it eventually.