Search code examples
androidc++qtjava-native-interface

NoSuchMethodError with JNI on Qt during reading Shared Preferences in Android


I want to use the Shared Preferences in Android to save persistently the User-ID, also if the User deinstalls the app. For this I'm using JNI/Qt5.12 but during reading the ID I get an error.

https://developer.android.com/training/data-storage/shared-preferences.html

Reading:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = getResources().getInteger(R.integer.saved_high_score_default_key);
int highScore = sharedPref.getInt(getString(R.string.saved_high_score_key), defaultValue);

JNI:

jint mp = QAndroidJniObject::getStaticField<jint>("android/content/Context", "MODE_PRIVATE");
QAndroidJniObject name = QAndroidJniObject::fromString("test");
QAndroidJniObject activity = QtAndroid::androidActivity();
QAndroidJniObject sharedPref = activity.callObjectMethod("getPreferences", "(I)Landroid/content/SharedPreferences;", mp);
jint defId = 2;

// this line throws
jint id = sharedPref.callMethod<jint>("getInt","(Ljava/lang/String;I)I;", name.object<jstring>(), defId);

Stacktrace:

W/System.err(10285): java.lang.NoSuchMethodError: no non-static method "Landroid/app/SharedPreferencesImpl;.getInt(Ljava/lang/String;I)I;"
W/System.err(10285): at org.qtproject.qt5.android.QtNative.startQtApplication(Native Method)
W/System.err(10285): at org.qtproject.qt5.android.QtNative$6.run(QtNative.java:365)
W/System.err(10285): at org.qtproject.qt5.android.QtThread$1.run(QtThread.java:61)
W/System.err(10285): at java.lang.Thread.run(Thread.java:818)

I also don't understand the sematic issue: warning: instantiation of function QAndroidJniObject::callMethod required here, but no defenition is available

Does someone know whats going wrong here? Thxs...


Solution

  • The reason this fails is a rather small thing: an extra semicolon in "(Ljava/lang/String;I)I;" - it should be "(Ljava/lang/String;I)I" instead (without the semicolon at the end).

    Regarding the warnings: You can ignore them, it simply the code model not beeing able to completly understand the definitions of those methods.