简体   繁体   English

如何在 Gradle 中的 buildType 中设置版本名称

[英]How to set the versionname in a buildType in Gradle

I could set versioncode and versionname in flavors, but I like to set them in my build type.我可以在口味中设置 versioncode 和 versionname,但我喜欢在我的构建类型中设置它们。 Is this possible in any way ?这有可能吗?

defaultConfig {
    versionCode 1
    versionName "1"
}
buildTypes {
    specialversion {
        versionCode 2      // <--- Doesn't work.
        versionName "2"    // <--- Doesn't work.
    }
}

Here is a complete list of field, that can be modified in buildType . 是一个完整的字段列表,可以在buildType修改。 As you might see, there aren't fields, that you need.如您所见,没有您需要的字段。 You might at best use versionNameSuffix .您最多可以使用versionNameSuffix

This should be put inside productFlavors :这应该放在productFlavors

defaultConfig {
    productFlavors {
        specialversion {
            versionCode 2      // <--- Doesn't work.
            versionName "2"    // <--- Doesn't work.
        }
    }
}

I solved it the following way.我通过以下方式解决了它。 Probably not recommended, but in my case its a good solution.可能不推荐,但就我而言,这是一个很好的解决方案。

I replaced values in the AndroidManifest.xml with the use of manifestPlaceholders in my buildvariants我在构建变量中使用manifestPlaceholders替换了 AndroidManifest.xml 中的值

   buildTypes {
demo {
...
            manifestPlaceholders = [iconpath: "@drawable/icon", versionCode:"161", versionName:"2.37"]
        }
   }

and my AndroidManifest.xml has the following :我的 AndroidManifest.xml 有以下内容:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="xxx"
    android:versionCode="${versionCode}"
    android:versionName="${versionName}"
    android:installLocation="auto">

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

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