放置 versionCode/versionName 的更好位置? build.gradle 对比 AndroidManifest.xml

Better place to put versionCode/versionName? build.gradle vs AndroidManifest.xml

现在您可以将 versionCodeversionName 放入 build.gradleAndroidManifest.xml 文件中。其中哪一个是放置这些属性的最佳位置,为什么它比另一个选项更好?

我在 google 官方文档中找不到任何关于此的信息:

  1. https://developer.android.com/tools/building/configuring-gradle.html

  2. http://developer.android.com/guide/topics/manifest/manifest-element.html

非常感谢。

实际上并没有什么不同。但我会告诉你一些相关信息。

  1. 如果您在 AppManifest.xml 和 build.gradle 中都写了 versionCode 和 versionName,则 build.gradle 中的将替换 [=] 中的36=] 当你编译你的应用程序时,第 2 点出现了。

  2. 如果你没有写在你的AppManifest.xml中,当你编译你的应用程序并构建它时,你写在你的build.gradle中的versionCode和versionName将自动被写入在你的 AppManifest.xml.

  3. 如果您只将它们写在 AppManifest.xml 中,则什么也不会发生。已经写好了...

总而言之,build.gradle覆盖你的AppManifest.xml。

希望对您有所帮助。

来源:https://developer.android.com/studio/publish/versioning

Note: If your app defines the app version directly in the element, the version values in the Gradle build file will override the settings in the manifest. Additionally, defining these settings in the Gradle build files allows you to specify different values for different versions of your app. For greater flexibility and to avoid potential overwriting when the manifest is merged, you should remove these attributes from the element and define your version settings in the Gradle build files instead.

在这里你可以找到官方doc

注意 Gradle 覆盖 AndroidManifest.xml
中的信息 我建议您始终使用 gradle 脚本来设置这些值。

如果未通过 DSL 设置 属性,将使用一些默认值。这是如何处理的 table。

 Property Name   Default value in DSL object     Default value
 versionCode     -1                              value from manifest if present
 versionName     null                            value from manifest if present

使用 Gradle,我不记得在什么情况下我必须使用 AndroidManifest.xml 的版本。

我的第一个想法是只删除 AndroidManifest.xml 中的版本,并使用来自 build.gradle 的版本。