简体   繁体   English

如何使用Gradle Android插件向Build Variant添加特定的包后缀(或packge名称)

[英]How to add a specific package suffix (or packge name) to a Build Variant using gradle Android plugin

Is it possible to set a specific package name (applicationId) to a Build Variant? 是否可以将特定的软件包名称(applicationId)设置为Build Variant? (flavor + build type) (风味+生成类型)

For instance, I want to add ".beta" to the package name of a variant that might be "flavor7Beta" but not the the other types of "flavor7" or other flavors of "Beta" type. 例如,我想在一个可能是“ flavor7Beta”的变体的程序包名称中添加“ .beta”,而不是其他类型的“ flavor7”或其他“ Beta”类型的风味。

I tried 我试过了

beta {
            ...
            applicationVariants.all { variant ->
                println "variant: ${variant.name}"
                if(variant.name == "flavor7Beta") {
                    variant.applicationId = "com.domain.myapp.beta"
                }
            }
        }

But I got 但是我得到了

Error:(172, 0) Cannot set the value of read-only property 'applicationId' on com.android.build.gradle.internal.api.ApplicationVariantImpl_Decorated@3cdafe1e. 错误:(172,0)无法在com.android.build.gradle.internal.api.ApplicationVariantImpl_Decorated@3cdafe1e上设置只读属性'applicationId'的值。

You should use reflection. 您应该使用反射。

android.variantFilter { variant -> 
    def config = variant.getDefaultConfig();
    def field = config.getClass().getDeclaredField('productFlavor');
    field.setAccessible(true);
    field.get(config).applicationId "com.example"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM