如何使用 gradle 为所有模块定义通用的 android 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20404476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:48:22  来源:igfitidea点击:

How to define common android properties for all modules using gradle

androidgradle

提问by HotIceCream

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

我在 AndroidStudio 中用几个模块创建了一个简单的项目。每个模块的 gradle 脚本包含以下代码:

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.

如何将此代码移动到主 build.gradle 脚本(项目的脚本)?此代码对于所有子模块都是通用的。

采纳答案by Xavier Ducrohet

You could create a build.gradleat the root of your project (i.e. the folder that contains all your modules), and use it to configure your rootProject.

您可以build.gradle在项目的根目录(即包含所有模块的文件夹)创建一个,并使用它来配置您的 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.

您可以在旁边添加一个 build.gradle settings.gradle

In the example above you actually have 3 Gradle projects: Module1, Module2and the rootProject.

在上面的示例中,您实际上有 3 个 Gradle 项目:Module1Module2rootProject

So inside this build.gradle, you could do:

所以在这个里面build.gradle,你可以这样做:

// 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
}

回答by user4979992

Defining this in the top-most build.gradle seems to work

在最顶层的 build.gradle 中定义它似乎有效

subprojects {
    afterEvaluate {project ->
        if (project.hasProperty("android")) {
            android {
                compileSdkVersion 22
                buildToolsVersion '22.0.1'
            }
        }
    }
}

回答by Peter

This works for me in Android Studio 0.8.9. using the default gradle wrapper 1.12-all.

这在 Android Studio 0.8.9 中对我有用。使用默认的 gradle 包装器 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.

App 是 Lite 和 Pro 使用的库,其中 Lite/Pro 是我正在制作的应用程序的两种不同风格。我想在所有模块之间共享配置。全局配置位于根 gradle.build 文件中,所有子项目/模块都可以读取这些属性。

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:

在 Project/gradle.build 我添加了一个子项目配置:

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)

ext.[var name] 添加可以在子项目中读取的变量。我添加了前缀“global_”,以便更容易查看我的属性。(另请注意,我使用等号将值分配给变量)

In each subproject/module the gradle file looks like this:

在每个子项目/模块中,gradle 文件如下所示:

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

Android Studio IDE 似乎不知道子项目中的“ext”,但 gradle 知道。所以它在查看 gradle 文件时显示为警告,但构建仍然有效。因为它不知道“ext”,它似乎也不知道你添加的变量,所以这些也会在 IDE 中作为警告加下划线。但它有效:)

回答by Renetik

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.

嗯,有趣的是我没有找到像我在 Android Studio 中所期望的那样工作的解决方案。但只有我在这里介绍的这个解决方案有效,所以 Android Studio 编辑器中没有警告并且有效。

define in root build.gradle as everybody do:

像大家一样在根 build.gradle 中定义:

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.

但是代码完成和变量解析并不像我对 android studio 所期望的那样工作。

回答by HotIceCream

In build.gradle of main project you should write something like next:

在主项目的 build.gradle 中,您应该编写如下内容:

project.extensions.add("buildToolsVersion", "19.0.3")

In subproject you can use this extensions:

在子项目中,您可以使用此扩展:

buildToolsVersion rootProject.buildToolsVersion

More info you can find here

您可以在此处找到更多信息

回答by brunodles

Create a custom property for your project. To set a single property use:

为您的项目创建自定义属性。要设置单个属性,请使用:

project.ext.compileSdkVersion = 21

to set multiple properties use:

设置多个属性使用:

project.ext {
    compileSdkVersion = 21
    buildToolsVersion = "21.0.1"
    targetSdkVersion = 21
}

回答by Markw

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 构建,则简单地从顶级构建中引用“android”闭包是行不通的。'android' 方法在评估之后才可用。最初,我有一个基于 afterEvaluate 设置配置的工作解决方案,但它不再有效(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.

Android 任务已经创建。调用 android.applicationVariants、android.libraryVariants 或 android.testVariants 时会发生这种情况。一旦调用了这些方法,就无法继续配置模型。

回答by Ronen Rabinovici

There is a new way simpler method, in the updated Android Studio. You have a file named "gradle.properties" in the top project hierarchy exactly for that. Simply define your parameters there:

在更新的 Android Studio 中有一种更简单的新方法。您在顶部项目层次结构中有一个名为“gradle.properties”的文件,正是为此。只需在那里定义您的参数:

common_parameter='I am common"

And use it in the modules' gradle files (build.gradle) as such:

并在模块的 gradle 文件 (build.gradle) 中使用它,如下所示:

module_parameter = common_parameter

That's it.

就是这样。