[英]Better place to put versionCode/versionName? build.gradle vs AndroidManifest.xml
Now you can put versionCode
and versionName
in both build.gradle
and AndroidManifest.xml
files.现在您可以将
versionCode
和versionName
放在build.gradle
和AndroidManifest.xml
文件中。 Which of them is the best place to put these attributes, and why is it better than the other option?其中哪个是放置这些属性的最佳位置,为什么它比其他选项更好?
I can't find any info about this in google official documentation:我在谷歌官方文档中找不到任何关于此的信息:
https://developer.android.com/tools/building/configuring-gradle.html https://developer.android.com/tools/building/configuring-gradle.html
http://developer.android.com/guide/topics/manifest/manifest-element.html http://developer.android.com/guide/topics/manifest/manifest-element.html
Thanks a lot.非常感谢。
It doesn't actually differ.其实并没有什么不同。 But I'll tell you some info about that.
但我会告诉你一些有关的信息。
If you have written your versionCode and versionName in both your AppManifest.xml and build.gradle, the ones written in build.gradle will replace the ones in your AppManifest.xml when you compile your app, and here comes point 2.如果您在 AppManifest.xml 和 build.gradle 中都编写了 versionCode 和 versionName,那么在编译应用程序时,build.gradle 中编写的将替换 AppManifest.xml 中的那些,这就是第 2 点。
If you haven't written them in your AppManifest.xml, when you compile your app and build it, your versionCode and versionName written in your build.gradle will automatically be written in your AppManifest.xml.如果你没有在你的 AppManifest.xml 中写入它们,当你编译你的应用并构建它时,你在 build.gradle 中写入的 versionCode 和 versionName 将自动写入你的 AppManifest.xml。
And if you have written them in your AppManifest.xml only, nothing will happen.如果您只在 AppManifest.xml 中编写了它们,则不会发生任何事情。 It is already written...
已经写好了...
So as a summary, build.gradle overrides your AppManifest.xml.因此,总而言之,build.gradle会覆盖您的 AppManifest.xml。
Hope that helps.希望有帮助。
Source: https://developer.android.com/studio/publish/versioning来源: 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.
注意:如果您的应用直接在元素中定义应用版本,则 Gradle 构建文件中的版本值将覆盖清单中的设置。 Additionally, defining these settings in the Gradle build files allows you to specify different values for different versions of your app.
此外,在 Gradle 构建文件中定义这些设置允许您为应用程序的不同版本指定不同的值。 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.
为了获得更大的灵活性并避免在合并清单时潜在的覆盖,您应该从元素中删除这些属性并改为在 Gradle 构建文件中定义您的版本设置。
Here you can find the official doc .在这里你可以找到官方文档。
Pay attention that Gradle overrides the info in the AndroidManifest.xml
注意 Gradle会覆盖
AndroidManifest.xml
的信息
I suggest you using always the gradle script to set these values.我建议您始终使用 gradle 脚本来设置这些值。
If a property is not set through the DSL, some default value will be used.如果属性不是通过 DSL 设置的,则将使用一些默认值。 Here's a table of how this is processed.
这是如何处理的表格。
Property Name Default value in DSL object Default value
versionCode -1 value from manifest if present
versionName null value from manifest if present
Using Gradle, I can't recall any situation where I had to use the version from the AndroidManifest.xml
.使用 Gradle,我想不起来我必须使用
AndroidManifest.xml
的版本的任何情况。
My first thought would be to just remove the version from AndroidManifest.xml
, and use the one coming from build.gradle
.我的第一个想法是从
AndroidManifest.xml
删除版本,并使用来自build.gradle
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.