I am trying to use NDK for my android application. I am using the map to store some key values as follow:
#include <jni.h>
#include <string>
#include <iostream>
#include <unordered_map>
class UrlHash {
std::unordered_map<std::string, std::string> urlHash;
public:
std::string getUrl(std::string urlKey) {
return urlHash[debugUrlKey];
}
UrlHash() {
urlHash["dummy"] = "dumyValue";
urlHash["dump_url"] = "https://dump.server.com";
urlHash["dump_url2"] = "https://dump2.server.com";
}
};
extern "C"
JNIEXPORT jstring JNICALL
Java_my_com_mynativecapp_UrlProvider_getUrl(JNIEnv *env, jobject thiz, jstring urlName) {
UrlHash _debugHashtableInstance;
const char *NativeUrlName = env->GetStringUTFChars(urlName, 0);
env->ReleaseStringUTFChars(urlName, NativeUrlName);
return env->NewStringUTF(_debugHashtableInstance.getDebugUrl(NativeUrlName).c_str());
}
retrieving these values in the following way:
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.TextView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<Button>(R.id.sample_text).setOnClickListener {
doExe()
}
fun doExe(){
var dummy = getUrl("dummy")
var dump_url = getUrl("dump_url")
var dump_url2 = getUrl("dump_url2")
}
external fun getUrl(urlName: String): String
companion object {
init {
System.loadLibrary("native-lib")
}
}
}
The above code works perfectly fine on other android versions except Android 5.1 and Android 6. On Android versions 5.1 and 6 sometimes the above values are obtained as empty values. This behaviour is not consistent. Sometimes it returns correct values and sometimes it returns empty values.
Code works perfectly fine on other Android versions like Android 7,8,10.
env->ReleaseStringUTFChars(urlName, NativeUrlName);
says we no longer needNativeUrlName
, but you use it on the next line, so perhaps the string is getting deallocated before you use it.