gradle动态修改applicationId

废话不多说,上需求:想通过渠道和buildType区分不同applicationId
WHY: 某些三方sdk(某华、某米)以applicationId为维度,一个applictionId不能申请多个appId,怎么区分buildType呢,问题来了

挣扎中
build.gradle里,通过task修改


        android.variantFilter { variant ->
            if (variant.buildType.name.equals('release')) {
            } else {
                variant.getFlavors().each() { flavor ->
                    if (flavor.name.equals('xiaomi')||flavor.name.equals('huawei')) {
                        variant.defaultConfig.applicationIdSuffix 'test'
                    }
                }
            }
            print('=====================')
        }
}
project.beforeEvaluate {
    android.applicationVariants.each { variant ->
        def changeTask = tasks.getByName('changeApplicationId2'){
            doFirst {
                println('doFirst111111====' + changeTask)
            }
        }
        if (changeTask != null) {
            println('doFirst1' + changeTask)
            changeTask.doLast { task ->
                println('doLast')
            }
        }
        println("applicationId=============" + variant.applicationId)
    }
}

然后就呵呵了

Error:(167, 0) No signature of method: com.android.build.gradle.internal.api.ReadOnlyProductFlavor.applicationIdSuffix() is applicable for argument types: (java.lang.String) values: [test]Possible solutions: getApplicationIdSuffix()

神马改不了

哎,暂定最终方案

 debug {
            applicationIdSuffix 'test'
        }

凡是非release就都改applicationId吧
罗里吧嗦说一大堆,真是希望善良又博学的你看完能在留言里告诉我下更好的方法,真是感激不尽~~~

你可能感兴趣的:(gradle动态修改applicationId)