Processing of jstring input into Android Studio NDK



  • Hello.

    In the annex to the Android (Android Studio), the use of NDK has become necessary. However, the following difficulties have emerged.

    In the file with++ (pad jni) there is a function

       #define sd  0x0F
    

    jint
    Java_com_bignerdranch_android_newmyndkproj_MainActivity_intFromJNI(JNIEnv* env, jobject thiz, jstring str)
    {
    //Здесь нужно взять строку str сравнить с другой строкой с помощью if
    // и вернуть дефайн sd если истина.

    return ;
    }

    That's what happened to the body of this function and there's a problem. We need to compare the entry line. str with another line line, and if the truth is back, sd♪ If you translate all this into java, the code should be like:

          int sd = 0x0F;
    if (str.equals("reg785")){
    return sd;
    }
    else{
    return 0;
    }

    How to process the type jstring So that it can be compared to a pre-known string variable?

    Java activity that uses intFromJNI is as follows:

    static {
    System.loadLibrary("my-jni");
    }

    public native String intFromJNI();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    int testInt = intFromJNI(String str);
    
    
    
    
    TextView mainText = (TextView) findViewById(R.id.textView);
    

    mainText.setText( Integer.toString(intFromJNI("reg785")) );

    }

    Can you tell me how to write the bodywork? intFromJNI so that the entry line can be compared str type jstring With a pre-established line like "reg785"?

    Thank you so much for the answers.




  • Reply to my question (most thanks @alexolut):

    When I took on the body, intFromJNI code

     const char *nativeString = env->GetStringUTFChars(javaString, JNI_FALSE);
    

    I made a mistake: "request for member 'getstringutfchars' in something not a structure or union."

    After some searches, he found a solution to the problem. https://stackoverflow.com/questions/4181934/jni-converting-jstring-to-char/23126682#23126682 ♪

    The line shall look as follows:

       const char *res = (*env)->GetStringUTFChars(env, str, JNI_FALSE);
    

    As a result, the body of the function shall be:

     #define sd  0x0F
    

    jint
    Java_com_bignerdranch_android_newmyndkproj_MainActivity_intFromJNI(JNIEnv* env, jobject thiz, jstring str)
    {
    const char *res = (*env)->GetStringUTFChars(env, str, JNI_FALSE);;

    if(strcmp(res, "reg785") == 0){
       return sd;
    }
    else{
       return 0;
    }
    

    }




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2