How to define common android properties for all modules using gradle

AndroidGradle

Android Problem Overview


I create a simple project in AndroidStudio with a few modules. Each module's gradle script contains the following code:

android {
    compileSdkVersion 18
    buildToolsVersion "18.1.1"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 18
    }
}

How can I move this code to the main build.gradle script (project's script)? This code is common for all the submodules.

Android Solutions


Solution 1 - Android

You could create a build.gradle at the root of your project (i.e. the folder that contains all your modules), and use it to configure your rootProject.

For instance, if you have:

MyApp
  - Module1/
      - build.gradle
  - Module2/
      - build.gradle
  - settings.gradle

You can add a build.gradle next to settings.gradle.

In the example above you actually have 3 Gradle projects: Module1, Module2 and the rootProject.

So inside this build.gradle, you could do:

// use the ext object to add any properties to the project
project.ext {
   compileSdkVersion = 18
}

Then in your modules, you can do:

android {
    // here we reference the root project with the "rootProject" object.
    compileSdkVersion rootProject.ext.compileSdkVersion
}

Solution 2 - Android

Defining this in the top-most build.gradle seems to work

subprojects {
    afterEvaluate {project ->
        if (project.hasProperty("android")) {
            android {
                compileSdkVersion 22
                buildToolsVersion '22.0.1'
            }
        }
    }
}

Solution 3 - Android

This works for me in Android Studio 0.8.9. using the default gradle wrapper 1.12-all.

App is a library used by Lite and Pro where Lite/Pro are two different flavours of the app I'm making. I wanted to share config between all modules. The global config is located in the root gradle.build file and all subprojects/modules can read these properties.

My project structure is:

Project/
 gradle.build
 settings.gradle
 App/
  gradle.build
 Lite/
  gradle.build
 Pro/
  gradle.build

In Project/gradle.build I've added a subprojects config:

subprojects {
    ext.global_compileSdkVersion = 19
    ext.global_buildToolsVersion = "20.0.0"
    ...
}

ext.[var name] adds variables whch can be read in the subprojects. I've added the prefix "global_" so it will be easier to see my properties. (Also note that I'm using an equals sign to assign the value to the variable)

In each subproject/module the gradle file looks like this:

android {
    compileSdkVersion global_compileSdkVersion
    buildToolsVersion global_buildToolsVersion
    ...
}

Note!

The Android Studio IDE doesn't seem to know about "ext" in subprojects, but gradle does. So it shows up as a warning when viewing the gradle files, but builds will still work. Because it doesn't know about "ext" it doesn't seem to know about the varables you add either, so these will also be underlined in the IDE as warnings. But it works :)

Solution 4 - Android

Hmm interesting that I did not find solution that works as I expet it should in Android Studio. But only this solution I present here works so there is no warning in Android Studio editor and works.

define in root build.gradle as everybody do:

buildscript {
    ext {
        projectAppCompatVersion = 'com.android.support:appcompat-v7:23.3.0'
        projectSupportVersion = 'com.android.support:support-v4:23.3.0'
        projectCompileSdkVersion = 23
        projectMinSdkVersion = 22      // 15 android 4.0.3 for release
        projectTargetSdkVersion = 23   //23 requires permission requests , 22 for release
        projectBuildToolsVersion = "24.0.0rc2"
    }
 }

and to access without warnings:

compileSdkVersion project.properties.projectCompileSdkVersion
buildToolsVersion project.properties.projectBuildToolsVersion

but code completion and variables resolution is not working as I would expect for android studio.

Solution 5 - Android

In build.gradle of main project you should write something like next:

project.extensions.add("buildToolsVersion", "19.0.3")

In subproject you can use this extensions:

buildToolsVersion rootProject.buildToolsVersion

More info you can find [here][1]

[1]: http://www.gradle.org/docs/current/dsl/org.gradle.api.plugins.ExtensionAware.html "here"

Solution 6 - Android

Create a custom property for your project. To set a single property use:

project.ext.compileSdkVersion = 21

to set multiple properties use:

project.ext {
    compileSdkVersion = 21
    buildToolsVersion = "21.0.1"
    targetSdkVersion = 21
}

Solution 7 - Android

Simply referencing 'android' closure from top level build doesn't work if top level build is not an android build. The 'android' method is not available until after evaluate. Originally, I had a working solution based on setting the config afterEvaluate but it no longer works (Android Studio 0.6.0):

> Android tasks have already been created. This happens when calling android.applicationVariants, android.libraryVariants or android.testVariants. Once these methods are called, it is not possible to continue configuring the model.

Solution 8 - Android

There is a new way simpler method, in the updated Android Studio. You have a file named "gradle.properties" in the top project hierarchy exactly for that. Simply define your parameters there:

common_parameter='I am common"

And use it in the modules' gradle files (build.gradle) as such:

module_parameter = common_parameter

That's it.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHotIceCreamView Question on Stackoverflow
Solution 1 - AndroidXavier DucrohetView Answer on Stackoverflow
Solution 2 - Androiduser4979992View Answer on Stackoverflow
Solution 3 - AndroidPeterView Answer on Stackoverflow
Solution 4 - AndroidRenetikView Answer on Stackoverflow
Solution 5 - AndroidHotIceCreamView Answer on Stackoverflow
Solution 6 - AndroidbrunodlesView Answer on Stackoverflow
Solution 7 - AndroidMarkwView Answer on Stackoverflow
Solution 8 - AndroidRonen RabinoviciView Answer on Stackoverflow