简体   繁体   English

NDK:如何在 java 类中使用 .c 常量

[英]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.我试图在 .C 文件中定义常量并在我的类中的 .Class 文件中使用该常量。 How can I do that?我怎样才能做到这一点? I am totally new for NDK.我对 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我如何直接在我的 java 代码中使用它

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.创建一个带有 String 字段和返回 String 的方法的自定义 java 对象。

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

Create a Java native method and pass Container as an argument创建 Java 本地方法并将 Container 作为参数传递

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在 C/C++ 方面,您可以在这里修改容器的字段是 jni 函数的参考,可以帮助您http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/functions.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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