如何在gradle构建脚本中获取包名称或applicationId?

时间:2017-06-07 01:35:58

标签: android gradle build.gradle

我已从Ant脚本迁移到gradle构建脚本。但我不知道如何在gradle构建脚本中获取包名称或applicationId。

See image

See image

1 个答案:

答案 0 :(得分:1)

project.afterEvaluate {
    project.android.applicationVariants.all { variant ->
        def applicationId = [variant.mergedFlavor.applicationId, variant.buildType.applicationIdSuffix].findAll().join()
        ...
    }
}

或在foo.gradle中定义一个:

// foo.gradle
def appId = 'com.example.bar'
ext.appId = appId

// build.gradle
apply from: './foo.gradle'
...
defaultConfig {
   ...
   applicationId appId
}

// other.gradle you want
apply from: './foo.gradle'
...
task example {
    println(appId)
}