In my gradle file I have
externalNativeBuild {
ndkBuild {
path 'src/main/jni/Android.mk'
}
}
productFlavors {
flavorone{
externalNativeBuild.cmake {
cFlags '-DFLAVORONE'
}
signingConfig signingConfigs.flavoronerelease
}
flavortwo{
applicationId "com.mycompany.flavortwo"
versionCode 2
versionName "1.0.1"
externalNativeBuild.cmake {
cFlags '-DFLAVORTWO'
}
signingConfig signingConfigs.flavortworelease
}
flavorthree{
applicationId "com.mycompany.flavorthree"
versionCode 7
versionName "1.0.6"
externalNativeBuild.cmake {
cFlags '-DFLAVORTHREE'
}
signingConfig signingConfigs.flavorthreerelease
}
}
To add defined variables to my c file so I can identify the flavor. in my c file I have:
const char* secret;
#ifdef FLAVORONE
const char* secret = "flavor_1_secret";
#elif FLAVORTWO
const char* secret = "flavor_2_secret";
#elif FLAVORTHREE
const char* secret = "flavor_3_secret";
#else
const char* secret = "flavor_1_secret";
#endif
JNIEXPORT jstring JNICALL
Java_com_mycompany_app_MainActivity_getSecret(JNIEnv *env, jobject instance) {
return (*env)-> NewStringUTF(env, secret);
}
The problem is when I compile and run my code in flavor 2 or 3 getSecret() returns "flavor_1_secret". I suspect I have done something wrong defining the variables with cflags, but am very new to working with the NDK and I am having trouble figuring out what I've done wrong. I also tried:
const char* secret;
#ifdef FLAVORTWO
const char* secret = "flavor_2_secret";
#else
const char* secret = "flavor_1_secret";
#endif
JNIEXPORT jstring JNICALL
Java_com_mycompany_app_MainActivity_getSecret(JNIEnv *env, jobject instance) {
return (*env)-> NewStringUTF(env, secret);
}
and compiled it as flavor2. I still got the flavor 1 secret return. So it seems to always fall through the else and the flag variables are not defined.
See Question&Answers more detail:os