107

I just discovered something weird about Android studio: it has some configuration options in the build.gradle file that override what is specified in the AndroidManifest.xml file.

For instance, I had the following lines in build.gradle:

android {
    compileSdkVersion 18
    buildToolsVersion "18.1.1"

    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 10
    }
...
}

which was overriding the corresponding tag in AndroidManifest.xml:

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="8"/>

I don't really like to have the same settings spread in two different files, so I am wondering if I can safely remove it either from build.gradle or AndroidManifest.xml and where it makes more sense to keep it.

mariosangiorgio
  • 5,230
  • 4
  • 30
  • 45

2 Answers2

113

Gradle overrides the manifest values, and I prefer to update the build.gradle file rather than the manifest. Probably this is the right way using Gradle. Gradle supports product flavours which can be controlled via an IDE and those product flavors can change many things in our Manifest like package name, version code, version name, target SDK and many other. Then by one click in Android Studio you can change many properties and generate another apk.

You can leave the manifest as it is and do all configuration in build.gradle. You can safely remove

<uses-sdk></uses-sdk>

from manifest as well as version codes.

coltox
  • 312
  • 3
  • 8
mar3kk
  • 1,856
  • 2
  • 16
  • 21
  • 3
    For anybody wondering, if you use apktool to unpack an APK built like this, you won't actually see the minSdkVersion in the AndroidManifest. I don't know where it goes, but it does do the correct thing (I confirmed by uploading to Google Play)! – Dan J Jan 10 '14 at 02:57
  • 6
    Strange. It seems that Google decides the minSDK by what API our app calls. Eventhough my app/build.gradle specifies minSDK to be Android 2.2, in Google Play it says minSDK = Android 1.6. And yes, the decompiled AndroidManifest.xml contains no minSDK information. I think this is a problem, since it "forces" us to also support Android 1.6 devices. – sancho21 May 17 '14 at 11:10
  • 1
    The sidebar here: http://developer.android.com/guide/topics/manifest/uses-sdk-element.html suggests that Play still uses the manifest somehow. Maybe it's reinserted behind the scenes by gradle prior to APK build? – jordanpg Sep 16 '14 at 16:10
  • 4
    Thats how it works. Gradle will insert missing information to manifest during compilation. – mar3kk Sep 17 '14 at 06:51
  • 5
    @sancho21 This is a known bug with the Google Play store during alpha and beta testing. It should resolve when you push to prod. Not an authoritative source, but see http://answers.unity3d.com/questions/683972/published-unity-android-app-has-lower-android-vers.html – Tom Lubitz Nov 24 '14 at 23:25
  • what if i want to define it in android manifest and not gradle? how do i do that? – aimiliano Jul 09 '15 at 14:01
  • 1
    @Dan J apktool put the minSdkVersion in another file apktool.yml and reuse it at compilation. – PiR Mar 18 '16 at 09:57
0

From the Android docs:

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.

https://developer.android.com/studio/publish/versioning.html#appversioning

TALE
  • 695
  • 10
  • 20
  • 1
    I think that part was talking about `versionCode `. You should have quoted the next one in `Specify API level requirements` section – Long Oct 23 '19 at 06:44