1

I have an Android NDK native project, and I would like to use the same constants in both the C++ and Java code.

From this question I understand that I can declare Android resources in Gradle files as

android {
    buildTypes {
        debug{ resValue "string", "app_name", "My App Name Debug" }
        release { resValue "string", "app_name", "My App Name" }
    }
}

which I can then access in my Java code with @string/app_name or R.string.app_name

Is there a mechanism for accessing these declarations in my native C++ code, which is compiled as part of my Gradle build process?

Community
  • 1
  • 1
WillC
  • 957
  • 16
  • 19

1 Answers1

2

I do not think you can access the declarations directly from c++. I can think of two other ways - either use a Java method to return the declarations you want (a method for each or an array, it is up to you), or pass the declarations to the compiler using LOCAL_CPPFLAGS with -D for each declaration, for example -Dapp_name="My App Name Debug" (you can set it directly to any variable that is available during the gradle build. You may have to change the way you build your JNI code for that.

yakobom
  • 2,681
  • 1
  • 25
  • 33