[FIXED] Android JNI GetMethodID for sharedPreferences.getString

Issue

I’m trying to get a string from sharedPreferences on Android from C++ code using JNI. I can successfully get the MethodID for getBoolean with this code:

jmethodID getBooleanMethodID = JEnv->GetMethodID(sharedPreferencesClass, "getBoolean", "(Ljava/lang/String;Z)Z");

The getBoolean function is defined in Java as getBoolean(String key, boolean defValue)

But I can’t figure out how to correctly format the GetMethodID arguments for getString.
The getString function is defined in Java as getString(String key, String defValue)

I’ve tried:

jmethodID getStringMethodID = JEnv->GetMethodID(sharedPreferencesClass, "getString", "(Ljava/lang/String;Ljava/lang/String)");

Can someone provide the correct way to format the argument param for getString?

Solution

The correct format turned out to be:

jmethodID getStringMethodID = JEnv->GetMethodID(sharedPreferencesClass, "getString", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");

There were a couple issues with my original attemps. First, the second String arg inside the brackets needed a semi-colon. Then I needed to add the return type, also a String, and be sure that it had a semi-colon at the end too.

Answered By – Chip Burwell

Answer Checked By – Timothy Miller (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published