[英]Build.Gradle Templating
Does anyone know of a way to template build.gradle files as modules are created?有谁知道在创建模块时模板 build.gradle 文件的方法? Right now the issue I have is that I want to be able to have the module's build.gradle contain only a small amount of information since I'd be setting the shared configuration within the top level build.gradle file.
现在我遇到的问题是我希望能够让模块的 build.gradle 仅包含少量信息,因为我将在顶级 build.gradle 文件中设置共享配置。 I have a buildSrc folder setup to centralize the versioning as well.
我也有一个 buildSrc 文件夹设置来集中版本控制。
Top-level build.gradle file:顶层 build.gradle 文件:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath Deps.tools_gradleandroid
classpath Deps.tools_kotlin
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10'
}
}
subprojects {
afterEvaluate { project ->
if (project.hasProperty('android')) {
android {
buildToolsVersion Config.buildTools
compileSdkVersion Config.compileSdk
defaultConfig {
minSdkVersion Config.minSdk
targetSdkVersion Config.targetSdk
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility Config.javaVersion
targetCompatibility Config.javaVersion
}
kotlinOptions {
jvmTarget = Config.javaVersion.toString()
}
}
}
}
}
A module's expected build.gradle file:一个模块的预期 build.gradle 文件:
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
defaultConfig {
applicationId "com.company.expectedmodule"
}
}
dependencies {
implementation Deps.androidx_core
implementation Deps.androidx_appcompat
implementation Deps.androidx_material
testImplementation Deps.testlib_junit
androidTestImplementation Deps.testandroidx_junit
androidTestImplementation Deps.testandroidx_espressocore
}
What the build.gradle file looks like when I create a 'New Module':当我创建“新模块”时,build.gradle 文件的样子:
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.company.newmodule"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.5.0'
implementation 'com.google.android.material:material:1.6.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
EDIT: For sake of simplicity I've moved the dependencies bracket to the top-level build.gradle file and also changed the modules to be libraries instead and reduced the code of a library module to below:编辑:为简单起见,我已将依赖项括号移至顶级 build.gradle 文件,并将模块更改为库,并将库模块的代码减少到以下:
plugins {
id 'com.android.library'
id 'org.jetbrains.kotlin.android'
}
android {
}
recommended way of sharing build logic in Gradle is by using convention plugins.在 Gradle 中共享构建逻辑的推荐方法是使用约定插件。 Like in this sample .
就像在这个示例中一样。
Also precompiled script plugins can be used to write plugins in syntax similar to regular build scripts. 预编译的脚本插件也可用于编写语法类似于常规构建脚本的插件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.