结构
在Android项目中,Gradle脚本被分为三部分
1、Module工程项目下的 build.gradle
2、项目工程下的build.gradle
3、项目工程下的setting.gradle
Module下的build.gradle,主要分为三部分
1、plugins:Gradle插件集合,可以理解为import,在此处进行添加了对应的插件,在下方才可以写相应的方法。
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'com.zhpew.lib'
}
2、android : Android项目的一些配置
android {
namespace 'com.zhpew.gradleaop'
compileSdk 33
defaultConfig {
applicationId "com.zhpew.gradleaop"
minSdk 26
targetSdk 33
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'
}
}
此部分中除了kotlinOptions这一项,其它均来自com.android.application这个插件,kotlinOptions属于 org.jetbrains.kotlin.android
3、dependencies:App这个module依赖的外部代码包
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
项目工程下的build.gradle
只有一个插件的集合,必须要在项目工程的插件集合里添加了对应的插件,才可以在Module的build.gradle中使用。
plugins {
id 'com.android.application' version '7.4.1' apply false
id 'com.android.library' version '7.4.1' apply false
id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
id 'org.jetbrains.kotlin.jvm' version '1.6.10' apply false
id 'com.zhpew.lib' version '1.0.0' apply false
}
项目工程下的setting.gradle
插件管理下载配置
pluginManagement {
resolutionStrategy {
eachPlugin {
if (requested.id.id == 'com.zhpew.lib') {
useModule "com.zhpew.lib:myGradlePlugin:${requested.version}"
}
}
}
repositories {
maven {
url uri('./repos') //指定本地maven的路径,在项目根目录下
}
google()
mavenCentral()
gradlePluginPortal()
}
}
依赖管理下载配置
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}