2

I have 2 Android apps with 5 Android modules (Android library project). Total 7 Eclipse projects. I want to enable Gradle build for them. I added build.gradle in the root folder and listed all project in settings.gradle

include ':app1'
include ':app2'
...

However I discovered that I need to copy-paste section below in every one of 7 project

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.3"

    defaultConfig {
        minSdkVersion 1
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
        ... // and so on like source folders configuration
}

Reason: when I add apply plugin: 'android' inside root build.gradle (see Could not find method android() in root of multimodule project),
then I can't apply plugin: 'android-library' for libraries projects (see https://stackoverflow.com/questions/23864292/minimal-gradle-configuration-for-android-library-in-multimodule-project)

But it is what I want to avoid, having different version of Android and Android tools specified in different places.

How can I minify this configuration to have as many things as possible in one build.gradle config file.

Community
  • 1
  • 1
Paul Verest
  • 60,022
  • 51
  • 208
  • 332
  • This should be one question, not three. Why can't you simply use `apply plugin: "android-library"` in the root build script's `subprojects` block? – Peter Niederwieser May 26 '14 at 08:23
  • There are not only libraries but 2 apps that use those libraries. The answer for http://stackoverflow.com/questions/23864068/could-not-find-method-android-in-root-of-multimodule-project does not work for general case, that this 3rd question is. – Paul Verest May 26 '14 at 08:44
  • 1
    Then either add some logic to the root script to apply the right plugin to each subproject (there are different ways to do this), or move the common parts into a script named `android.gradle` and apply that script to each subproject after applying the correct plugin for that project (all in the subproject's build script). – Peter Niederwieser May 26 '14 at 09:06
  • That could be good answer. Just needs more details. – Paul Verest May 26 '14 at 09:28
  • For more information, see the "multi-project builds" chapter in the [Gradle User Guide](http://gradle.org/docs/current/userguide/userguide_single.html), and the many samples in the full Gradle distribution. – Peter Niederwieser May 26 '14 at 10:09

2 Answers2

8

You can use your build.gradle in root, or you can define some values in gradle.properties in root folder to achieve your scope.

For example:

root/build.gradle:

ext {
    compileSdkVersion = 19
    buildToolsVersion = "19.0.3"
}

module/build.gradle:

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion
}

OR using properties

root/gradle.properties:

VERSION_NAME=1.0.1
VERSION_CODE=11

module/build.gradle:

android {

    defaultConfig {
        versionName project.VERSION_NAME
        versionCode Integer.parseInt(project.VERSION_CODE)

    }
Paul Verest
  • 60,022
  • 51
  • 208
  • 332
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
0

Other option would be as Peter Niederwieser suggested

either add some logic to the root script to apply the right plugin to each subproject (there are different ways to do this), or move the common parts into a script named android.gradle and apply that script to each subproject after applying the correct plugin for that project (all in the subproject's build script).

Related docs is Chapter 56. Multi-project Builds

Paul Verest
  • 60,022
  • 51
  • 208
  • 332