Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define common android properties for all modules using gradle

Tags:

android

gradle

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.

like image 339
HotIceCream Avatar asked Dec 05 '13 15:12

HotIceCream


People also ask

Where is Android Gradle properties?

properties in the project folder or in the C:\Users\Username. gradle.

How do I set Gradle system property?

To define system properties for our Gradle build we can use the command line option --system-prop or -D . But we can also add the values for system properties in the gradle. properties file of our project. This file contains project properties we want to externalized, but if we prefix the property name with systemProp.

How do you pass system properties in Gradle command line?

Using the -D command-line option, you can pass a system property to the JVM which runs Gradle. The -D option of the gradle command has the same effect as the -D option of the java command. You can also set system properties in gradle. properties files with the prefix systemProp.


1 Answers

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 } 
like image 78
Xavier Ducrohet Avatar answered Oct 06 '22 16:10

Xavier Ducrohet