web-dev-qa-db-ja.com

Gradleフレーバーに応じて変数を設定する方法

フレーバーごとに異なる設定をした変数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行です

build/intermediates/ndk/flavorA/debug/Android.mk:

LOCAL_CFLAGS :=  -DLOGGING=1 -DTEST=2

期待していた-DTEST=1 ここに

build/intermediates/ndk/flavorB/debug/Android.mk:

LOCAL_CFLAGS :=  -DLOGGING=1 -DTEST=2

では、私の間違いはどこにあるのでしょうか?または、どうすれば目標を達成できますか?また、実際の問題はより複雑であり、可能であれば「buildTypes」セグメントでこれらの定義を作成したいと考えています。

21
Torge

私は解決策を見つけました:

最初に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 "
            }
        }
    }

}
14
Torge

buildConfigFieldを使用できます

productFlavors {
    demo {
        buildConfigField "int", "FOO", "1"
        buildConfigField "String", "FOO_STRING", "\"foo1\""
    }
    full {
        buildConfigField "int", "FOO", "2"
        buildConfigField "String", "FOO_STRING", "\"foo2\""
    }
}
15
Phan Van Linh