0

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.

Ryan McCaffrey
  • 201
  • 2
  • 15

2 Answers2

1

I think that you have to replace #elseif by #elif, the preprocessor stops at #ifdef if it is flavor 1 or jump to #else if other because #elseif is not defined

E.Abdel
  • 1,992
  • 1
  • 13
  • 24
1

You use ndkBuild (via Android.mk), not cmake. Hence, in flavored section, you need

externalNativeBuild.ndkBuild
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • I changed the code to: `productFlavors { flavorone{ externalNativeBuild. ndkBuild { cFlags '-DFLAVORONE' } signingConfig signingConfigs.flavoronerelease } flavortwo{ applicationId "com.mycompany. ndkBuild" versionCode 2 versionName "1.0.1" externalNativeBuild.cmake { cFlags '-DFLAVORTWO' } signingConfig signingConfigs.flavortworelease }` And the flag variables are still not defined. – Ryan McCaffrey May 09 '18 at 17:02
  • I see that some `cmake` is still there. It's hard to understand in format of comments. Please try to use [this technique](https://stackoverflow.com/a/43442227/192373) to see what flags are actually passed to **ndk-build**. – Alex Cohn May 09 '18 at 18:11
  • Oops, missed some cmakes in the genericized code, but it was correct in my actual code. Looking at that link now. Thanks. – Ryan McCaffrey May 09 '18 at 19:08