Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
290 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...