Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio AndroidManifest.xml vs build.gradle

If anyone could help me understand some things regarding Android Studio, it would be most enlightening.

So, I have switched from Eclipse to Android Studio around a month ago and so far have only been working on my migrated Apps. As such, I have been tinkering around with only the AndroidManifest.xml file as was customary in Eclipse.

However, recently, I have started creating a new project for the sake of learning Android Studio's differences from Eclipse from ground up. Aside from the very irritating appcompat_v7 issues I have encountered, I'm also confused with some things regarding build.gradle.

Below is a gradle code block from an app created from Android Studio:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion '22.0.1'

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

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v4:22.2.0'
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.android.support:mediarouter-v7:22.2.0'
}

On the other hand, below is a code block from build.gradle of a migrated Eclipse project:

apply plugin: 'android'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile project(':google-play-services_lib')
}

android {
    compileSdkVersion 21
    buildToolsVersion '22.0.1'

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        // Move the tests to tests/java, tests/res, etc...
        instrumentTest.setRoot('tests')

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
}

Am I right in some of the assumptions I have taken from reading?

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

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

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

Now to my questions with regards to manifest vs gradle:

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

  2. 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?

  3. 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?)

  4. 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 <activity></activity> 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?)

Sorry for the long, maybe winding, questions but it really is quite confusing to me.

Thanks in advance.

Update:

After reading What is Gradle in Android Studio? and http://developer.android.com/tools/studio/index.html, my new questions:

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

  2. 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?

like image 485
publicknowledge Avatar asked Jul 15 '15 05:07

publicknowledge


People also ask

Why do you need AndroidManifest xml file?

Every app project must have an AndroidManifest. xml file (with precisely that name) at the root of the project source set. The manifest file describes essential information about your app to the Android build tools, the Android operating system, and Google Play.

Which are the two types of plugins in Gradle?

There are two general types of plugins in Gradle, binary plugins and script plugins.

What is the build Gradle file?

gradle. Gradle is a build system (open source) that is used to automate building, testing, deployment, etc.

Where is the AndroidManifest xml file in Android Studio?

Every project in Android includes a Manifest XML file, which is AndroidManifest. xml, located in the root directory of its project hierarchy. The manifest file is an important part of our app because it defines the structure and metadata of our application, its components, and its requirements.


2 Answers

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!

like image 66
shredder Avatar answered Oct 09 '22 05:10

shredder


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

like image 20
Ranjeet Kushwaha Avatar answered Oct 09 '22 03:10

Ranjeet Kushwaha