30

我想将test每个风味设置不同的变量作为定义传递给 NDK。但由于某种原因,他总是传递最后一种味道的价值。

这是 build.gradle:

apply plugin: 'com.android.library'

def test

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"
    defaultPublishConfig "flavorARelease"
    publishNonDefault true

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 17

        ndk {
            moduleName "test"
            ldLibs "log"
        }
    }

    productFlavors {    
        flavorA {
            test = 1
        }

        flavorB {
            test = 2
        }    
    }

    buildTypes {    
        debug {
            ndk {
                if (cFlags == null) { cFlags = "" }
                cFlags = cFlags + " -DLOGGING=1 -DTEST="+test+" "
            }
            minifyEnabled false
        }
        release {
            ndk {
                if (cFlags == null) { cFlags = "" }
                cFlags = cFlags + " -DLOGGING=0 -DTEST="+test+" "
            }
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
}

这是生成的 Android.mk 中的 CFLAG 行

构建/中间体/ndk/flavorA/debug/Android.mk:

LOCAL_CFLAGS :=  -DLOGGING=1 -DTEST=2

我期待-DTEST=1这里

构建/中间体/ndk/flavorB/debug/Android.mk:

LOCAL_CFLAGS :=  -DLOGGING=1 -DTEST=2

那么我的错误在哪里?或者我怎样才能实现我的目标?还请考虑真正的问题更复杂,如果可能的话,我想在“buildTypes”部分中进行这些定义。

4

2 回答 2

25

您可以使用buildConfigField

productFlavors {
    demo {
        buildConfigField "int", "FOO", "1"
        buildConfigField "String", "FOO_STRING", "\"foo1\""
    }
    full {
        buildConfigField "int", "FOO", "2"
        buildConfigField "String", "FOO_STRING", "\"foo2\""
    }
}
于 2017-09-19T07:16:05.947 回答
17

我找到了解决方案:

首先而不是def test为所有 productFlavors 指定一个新字段

productFlavors.all {
    ext.dTest = null
}

然后在每个风味中设置这个字段(代码不变)

productFlavors {    
    flavorA {
        dTest = 1
    }

    flavorB {
        dTest = 2
    }    
}

最后,您可以在 buildTypes 中执行此操作

buildTypes {    
    all {
         productFlavors {
            all {
                ndk {
                    if (cFlags == null) { cFlags = "" }
                    cFlags = cFlags + " -DTEST="+dTest+" "
                }
            }
        }
    }
    debug {           
        minifyEnabled false
        ndk {
            if (cFlags == null) { cFlags = "" }
            cFlags = cFlags + " -DLOGGING=1 "
        }
    }
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

        ndk {
            if (cFlags == null) { cFlags = "" }
            cFlags = cFlags + " -DLOGGING=0 "
        }
    }
}

这里是完整的文件:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"
    defaultPublishConfig "flavorARelease"
    publishNonDefault true

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 17

        ndk {
            moduleName "dTest"
            ldLibs "log"
        }
    }

    productFlavors.all {
        ext.dTest = null
    }

    productFlavors {    
        flavorA {
            dTest = 1
        }

        flavorB {
            dTest = 2
        }    
    }


    buildTypes {    
        all {
            productFlavors {
                all {
                    ndk {
                        if (cFlags == null) { cFlags = "" }
                        cFlags = cFlags + " -DTEST="+dTest+" "
                    }
                }
            }
        }
        debug {           
            minifyEnabled false
            ndk {
                if (cFlags == null) { cFlags = "" }
                cFlags = cFlags + " -DLOGGING=1 "
            }
        }
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

            ndk {
                if (cFlags == null) { cFlags = "" }
                cFlags = cFlags + " -DLOGGING=0 "
            }
        }
    }

}
于 2016-03-16T16:31:41.537 回答