How to define common android properties for all modules using gradle

前端 未结 8 2062
走了就别回头了
走了就别回头了 2020-12-04 14:20

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

android {
    compileSdkVersion 18
          


        
8条回答
  •  悲哀的现实
    2020-12-04 14:41

    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 :)

提交回复
热议问题