How to define common android properties for all modules using gradle

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

提交回复
热议问题