Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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:

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

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

Thanks a lot.

like image 811
NullPointerException Avatar asked Aug 21 '15 10:08

NullPointerException


2 Answers

It doesn't actually differ. But I'll tell you some info about that.

  1. 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.

  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.

  3. And if you have written them in your AppManifest.xml only, nothing will happen. It is already written...

So as a summary, build.gradle overrides your AppManifest.xml.

Hope that helps.

Source: 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.

like image 177
Hussein El Feky Avatar answered Nov 19 '22 21:11

Hussein El Feky


Here you can find the official doc.

Pay attention that Gradle overrides the info in the AndroidManifest.xml
I suggest you using always the gradle script to set these values.

If a property is not set through the DSL, some default value will be used. 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
like image 34
Gabriele Mariotti Avatar answered Nov 19 '22 19:11

Gabriele Mariotti