1

I'm trying to solve a similar problem to NDK support with different Product Flavour but in a .c file instead of a .cpp file.

Does anyone know how of if it's possible to get the current build flavor from the JNIEnv parameter in C like in the accepted C++ solution in the linked thread? And if not what is the most efficient way to have a function in C return a different string on each flavor.

My first instinct was to declare a different jni folder in each flavor in gradle by giving each an ExternalNativeBuild NDKBuild path, but it fails to build as gradle attempts to find a method path() in Android.mk opposed to setting the jni folder for the flavor.

Janvi Vyas
  • 732
  • 5
  • 16
Ryan McCaffrey
  • 201
  • 2
  • 15
  • 1
    Seems to me like [Alex's answer](https://stackoverflow.com/a/47361259/1524450) to the question you linked to applies perfectly well in your case as well. Just use `cFlags` instead of `cppFlags`, and `const char*` instead of `std::string`. Or [this answer of mine](https://stackoverflow.com/questions/46771588/read-build-arguments-from-ndk-code/46774983#46774983) should also work. – Michael May 08 '18 at 14:52
  • Thanks for the help, but I have little to no experience with these C files, so I have a bit of a followup question. To use Alex's answer I add something like `#ifdef FLAVORONE const char* secret = "my_app_secret" #endif` to the top of my keys.c file? Then how would I reference that defined variable to return it in my a function like `JNIEXPORT jstring JNICALL Java_com_mycompany_id_utilities_UtilFuncs_getSecret(JNIEnv *env, jobject instance) { return (*env)-> NewStringUTF(env, secret); }` – Ryan McCaffrey May 08 '18 at 15:24
  • Just like in the code you posted in your comment, i.e. pass the `const char*` to `NewStringUTF` and return the result. – Michael May 08 '18 at 15:55
  • I get "Can't resolve variable 'secret'" when I try to use the code in my post. Am I declaring it in the wrong place or not writing out the NewStringUTF call right? – Ryan McCaffrey May 08 '18 at 16:01
  • Sounds like you either set the define incorrectly, or didn't declare a string for every possible define. But it's really impossible to say without having seen the code and build script. – Michael May 08 '18 at 18:00
  • I added a post with all my code in it [here](https://stackoverflow.com/questions/50240190/android-ndk-cflags-not-defined-for-product-flavors) I'd be very thankful if you could help me figure this out. – Ryan McCaffrey May 08 '18 at 18:36

0 Answers0