Want to generate random string of fix length from JNI function for that i have used below function
static const char alphanum[] =
"abcdefghijklmnopqrstuvwxyz";
jstring Utils::getRandomString(JNIEnv *env, const int len) {
char s[len];
for (int i = 0; i < len; ++i) {
int p = rand() % (sizeof(alphanum) - 1);
s[i] = alphanum[p];
}
s[len] = 0;
__android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "getRandomString %s", s);
jstring temp= env->NewStringUTF(s);
return temp;
}
but application get crash for line jstring temp= env->NewStringUTF(s);
only for ABI version armeabi-v7a
for the solution i have tried this solution
jstring Utils::getRandomString(JNIEnv *env, const int len) {
char s[len];
for (int i = 0; i < len; ++i) {
int p = rand() % (sizeof(alphanum) - 1);
s[i] = alphanum[p];
}
s[len] = 0;
__android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "getRandomString 2 %s", s);
jbyteArray array = env->NewByteArray(len);
env->SetByteArrayRegion(array, 0, len, (const jbyte *) s);
jstring strEncode = env->NewStringUTF("UTF-8");
jclass cls = env->FindClass("java/lang/String");
jmethodID ctor = env->GetMethodID(cls, "<init>", "([BLjava/lang/String;)V");
jstring object = (jstring) env->NewObject(cls, ctor, array, strEncode);
__android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "getRandomString 3 %s",
env->GetStringUTFChars(object, 0));
// jstring temp= env->NewStringUTF(s);
return object;
}
but it working fine for ABI version armeabi-v7a
when we execute same code on ABI x86
it returning unexpected output like PKdhtXMmr18n2L9K�ؾ�����-DL
please provide some solution that return generated string. env->NewStringUTF()
working for ABI version x86
it get crash on armeabi-v7a
.
thanks in advance