Android Studio AndroidManifest.xml vs build.gradle

Gradle VS Menifest as below... Gradle overrides the manifest values and I prefer to update build.gradle file in Android studio and menifest file in android eclipse. Gradle supports Application which can be controled via Android studio framework. version code, version name, target SDK and many other libs refrence. change or update by one click in Android Studio, after that we can build project and genrate new apk.

For Android Studio:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.android.demo"
        minSdkVersion 18
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
    compile 'com.google.firebase:firebase-core:9.4.0'
    compile 'com.google.firebase:firebase-messaging:9.4.0'
    compile 'com.android.volley:volley:1.0.0'
    compile 'com.google.code.gson:gson:2.6.1'

}
apply plugin: 'com.google.gms.google-services'

For Android Eclipse:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.app"

android:versionCode="1"

android:versionName="1.0" >

I will try to address as many questions as possible, but I will start by suggesting that you do not use the generated build.gradle from the eclipse migration. Create a new project in Android Studio and use the build.gradle that it generates as a template for what you should be using, ie copy it's contents to your real project and change the values that make sense. Spend the time understanding and getting the build.gradle right, it will save you time in the future. Also mimic the file structure of the new project as best you can. The great thing about gradle is that it (usually) gives you meaningful errors.

compileSdkVersion - must always use the highest for maximum compatibility to newer phones?

targetedSdkVersion - my own preference on the optimal run conditions of my app?

compile and targeted should, in most cases, be the same. The compile value obviously tells the compiler which version to compile against and the target version tells the runtime which compatibility features to use. For example if you are targeting v21 and the app is running on a phone running v23, it will enable some compatibility features to enable your app to run a little nicer.

buildToolsVersion - I read this must ALWAYS be using the latest version. Can someone explain why?

The build tools you can think of as the compiler. If you have set compileSdkVersion 23, then you will need the 23.+ version of the build tools. But, to answer your question, let's say there was a bug with version 23.0 of the build tools (eg. it wasn't building native code properly) then Google would release 23.1 of the build tools. Now if your code doesn't compile native code, then the update doesn't really apply to you - you don't need it, but hey, it's always good to update incase you do. Moreover, if your compileSdkVersion is 23 and you have build tools version 24, well version 24 of the build tools is quite capable of building version 23.

Do compile & buildtools version have to be the same? Can they be different?

Hopefully this is answered above, but the answer is yes they can be different, but the build tools major version must always be greater than the compileSdkVersion.

If I have both an AndroidManifest.xml and a build.gradle, how does Android Studio know which to use for the compile,min,targeted,buildtools versions?

As an extension to question 1, what happens when there is a discrepancy between the 2 files (if someone forgot for some reason and decided to add stuff in one of them?)

The build.gradle will override values in the AndroidManifest.xml file, but to avoid confusion I would put all the above mentioned values into the build.gradle, and remove them from the manifest. That's where they belong. The build.gradle can do some really cool things, it can override values in the manifest and even merge two manifest files.

Since there are duplicate attributes between the 2, does this mean that starting with apps generated from Android Studio, I don't need to touch AndroidManifest.xml at all?

What about the so the app doesn't force close when it can't find the activity? Does build.gradle take care of that automatically? Or controlling orientation and other finer features (am I stuck with modifying only the java files on Android Studio?)

(if it doesn't, then having 2 files to control the app is kind of redundant and maybe they should have just stuck to AndroidManifest.xml?)

Definitely not. It just means that you have to do some things in the build.gradle and some in the AndroidManifest.xml. For example, if you add an activity, you must edit the AndroidManifest.xml as usual. If you want to alter the properties of the activity (rotation, theme etc) this is also still done in the AndroidManifest.xml. If you want to start using a new library from Maven Central, you will have to add it to the build.gradle. If you want to change the keys you use to sign the app with when you make a release build, or change the versionName of your app: build.gradle. Basically the build.gradle gives you a higher degree of control over your builds, I really recommend checking out what you can do here: http://developer.android.com/tools/building/configuring-gradle.html

AndroidManifest.xml is still needed, build.gradle just overrides settings if it has the same attributes.

Question: So BOTH are still needed in Android Studio, correct?

Correct. You still need both.

compile & buildtools version DON'T have to be the same BUT buildtools must always be higher than compileSdkVersion.

Question: Is this because Google creates a new buildtools version for each new Sdk and the higher version are backward compatible? Therefore, higher buildtools will build lower compileSdkVersion while the reverse is not true, correct?

Also correct!


Android Manifest file is useful for Activity, Permissions required and Declaring Services in Android applications, but build.gradle useful for library declaration etc.