Android.Gradle:如何从buildTypes中提取相同的代码?

时间:2017-09-30 17:21:57

标签: android gradle

我的app / build.gradle:

android {
    defaultConfig {
        applicationId "com.myproject"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 80
        versionName "1.1.80"
    }

    buildTypes {
        release {
            minifyEnabled false
            ext.betaDistributionReleaseNotes = defaultConfig.versionName + " " + name
            ext.betaDistributionEmailsFilePath="app/emails.txt"
        }
        debug {
            initWith(debug)
            ext.betaDistributionReleaseNotes = defaultConfig.versionName + " " + name
            ext.betaDistributionEmailsFilePath="app/emails.txt"
        }
        dev {
            initWith(debug)
            ext.betaDistributionReleaseNotes = defaultConfig.versionName + " " + name
            ext.betaDistributionEmailsFilePath="app/emails.txt"
        }
    }   
}

正如您所看到的,每个buildType都有相同的代码:

 ext.betaDistributionReleaseNotes = defaultConfig.versionName + " " + name
 ext.betaDistributionEmailsFilePath="app/emails.txt"

我想在一些 ONE 位置执行此代码(从所有buildTypes中提取)。 我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

试试这个

buildTypes {
    base {
        ext.betaDistributionReleaseNotes = defaultConfig.versionName + " " + name
        ext.betaDistributionEmailsFilePath="app/emails.txt"
    }
    release.initWith(buildTypes.base)
    release {
        minifyEnabled false
    }
    debug.initWith(buildTypes.base)
    debug {
    }
    dev_no_verification.initWith(buildTypes.base)
    dev_no_verification {
    }
}