如何根据gradle的口味设置变量 [英] How to set variable according to gradle flavors

查看:158
本文介绍了如何根据gradle的口味设置变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to pass a variable test that I set differently per flavor as a define to the NDK. But for some reason he always passes the value of the last flavor.

这是build.gradle:

Here is the 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行

And here are the CFLAG lines from the generated Android.mk

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

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

LOCAL_CFLAGS :=  -DLOGGING=1 -DTEST=2

我希望在这里-DTEST=1

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

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

LOCAL_CFLAGS :=  -DLOGGING=1 -DTEST=2

那我的错误在哪里?或者我该如何实现自己的目标?还请考虑真正的问题更加复杂,如果可能的话,我想在"buildTypes"部分中进行定义.

So where is my mistake? Or how can I achieve my goal? Please also consider that the real problem is more complex and I want to make those defines in the "buildTypes" segment if possible.

推荐答案

我找到了解决方法:

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

First instead of def test specify a new field for all productFlavors

productFlavors.all {
    ext.dTest = null
}

然后在每种口味中设置此字段(代码不变)

Then this field is set in each flavor (code unchanged)

productFlavors {    
    flavorA {
        dTest = 1
    }

    flavorB {
        dTest = 2
    }    
}

最后,您可以在buildTypes中完成此操作

And finally you can do this in the 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 "
        }
    }
}

此处是完整文件:

Here the full file:

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 "
            }
        }
    }

}

这篇关于如何根据gradle的口味设置变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆