简体   繁体   中英

NDK: How to use .c constants in java class

I am trying to define constant in .C file and use that constant in .Class file in my class. How can I do that? I am totally new for NDK. I defined constant here.

 const jstring BASE_URL ="http://www.google.com";'

currently i am using that constant by calling an method

jstring
Java_com_example_hellojni_HelloJni_variableData(JNIEnv *env) {
return (*env)->NewStringUTF(env, BASE_URL);
}

How can i Use it directly in my java code

Your method works. I can give you another way from my experience:

Create a custom java object with a String field and a method that returns a String.

Class Container{
  private String baseUrl;
  public String getBaseUrl(){
    return baseUrl;
  }
}

Create a Java native method and pass Container as an argument

On the side of C/C++, you can modify Container's fields here are jni funtions' reference that could help you http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/functions.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM