【问题标题】:How to set the versionname in a buildType in Gradle如何在 Gradle 的 buildType 中设置版本名称
【发布时间】:2017-02-03 15:08:19
【问题描述】:

我可以在风味中设置版本代码和版本名称,但我喜欢在我的构建类型中设置它们。 这有可能吗?

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

【问题讨论】:

  • 您可能会发现stackoverflow.com/q/28691093/794088 很有帮助
  • 谢谢,但这并没有真正帮助我。我用 manifestPlaceholders = [versionCode:"2", versionName:"2"] 解决了它,并在 AndroidManifest.xml 中添加了占位符,如 android:versionCode="${versionCode}"。我知道它不是真正推荐的,但就我而言,它是迄今为止最好的解决方案。
  • 请回答

标签: android android-studio android-gradle-plugin android-build-type


【解决方案1】:

Here是一个完整的字段列表,可以在buildType中修改。如您所见,没有您需要的字段。你最好使用versionNameSuffix

这应该放在productFlavors:

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

【讨论】:

    【解决方案2】:

    我通过以下方式解决了它。可能不推荐,但在我的情况下它是一个很好的解决方案。

    我在我的 buildvariants 中使用 manifestPlaceholders 替换了 AndroidManifest.xml 中的值

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

    我的 AndroidManifest.xml 有以下内容:

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

    【讨论】:

      猜你喜欢
      • 2016-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-30
      • 1970-01-01
      相关资源
      最近更新 更多