How to define common android properties for all modules using gradle

前端 未结 8 1965
走了就别回头了
走了就别回头了 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 :)

    0 讨论(0)
  • 2020-12-04 14:43

    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
    }
    
    0 讨论(0)
  • 2020-12-04 14:49

    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.

    0 讨论(0)
  • 2020-12-04 14:49

    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

    0 讨论(0)
  • 2020-12-04 14:51

    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'
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-04 14:51

    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.

    0 讨论(0)
提交回复
热议问题