buildSrc
-
在项目根目录下新建一个名为buildSrc的文件夹(与项目里的app文件夹同级)。
-
在buildSrc文件夹里创建名为build.gradle.kts的文件
plugins {
`kotlin-dsl`
}
repositories {
jcenter()
}
- 在buildSrc文件夹里创建src/main/kotlin文件夹,在src/main/kotlin目录下新建一个包,并在该包下创建kt文件(Dep.kt)。如下图所示
Deps.kt 字符串需要使用双引号""
/**
* Created by Donkey
* on 2:25 PM
*/
package donkey.dependencies
object Versions {
const val compileSdk = 29
const val buildTools = "29.0.3"
const val minSdk = 21
const val targetSdk = 29
const val versionCode = 1
const val versionName = "1.0"
const val kotlin = "1.3.72"
const val core_ktx = "1.3.0"
const val appcompat = "1.1.0"
const val constraintLayout = "1.1.3"
const val junit = "4.12"
const val test_junit = "1.1.1"
const val test_espresso = "3.2.0"
}
object Deps {
const val kotlin_stdlib = "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${Versions.kotlin}"
const val core_ktx = "androidx.core:core-ktx:${Versions.core_ktx}"
const val appcompat = "androidx.appcompat:appcompat:${Versions.appcompat}"
const val constraintLayout = "androidx.constraintlayout:constraintlayout:${Versions.constraintLayout}"
const val junit = "junit:junit:${Versions.junit}"
const val test_junit = "androidx.test.ext:junit:${Versions.test_junit}"
const val test_espresso ="androidx.test.espresso:espresso-core:${Versions.test_espresso}"
}
build.gradle(module) 使用
import donkey.dependencies.Versions
import donkey.dependencies.Deps
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion Versions.compileSdk
buildToolsVersion Versions.buildTools
defaultConfig {
applicationId "com.wanandroid"
minSdkVersion Versions.minSdk
targetSdkVersion Versions.targetSdk
versionCode Versions.versionCode
versionName Versions.versionName
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation Deps.kotlin_stdlib
implementation Deps.core_ktx
implementation Deps.appcompat
implementation Deps.constraintLayout
testImplementation Deps.junit
androidTestImplementation Deps.test_junit
androidTestImplementation Deps.test_espresso
}
build.gradle(project)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
import donkey.dependencies.Versions
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$Versions.kotlin"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}