新建project时,gradle会生成3个文件,分别时project下的build.gradle、settings.gradle和app模块下的build.gradle.
一、 project下的settings.gradle
rootProject.name='IntentService'
// 包含的项目模块
include ':app'
二、 project下的build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
// 代码仓库
repositories {
google()
jcenter()
}
// 代码块构建过程中所需要依赖的包
dependencies {
classpath 'com.android.tools.build:gradle:3.6.3'
// 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
}
三、 app模块下 build.gradle
可以覆盖project下build.gradle的任何属性
// android插件,官方提供,用于构建和打包android项目
apply plugin: 'com.android.application'
// 包含全部的android特有的配置
android {
// 编译应用Android api的版本号
compileSdkVersion 29
// 构建和编译使用的版本号
buildToolsVersion "30.0.1"
// 整个代码配置的核心属性
defaultConfig {
// manifest的package name,覆盖配置文件包的名字
applicationId "com.example.intentservice"
// 配置运行最小的android api的版本
minSdkVersion 19
// 用于通知系统这个应用已经在某些特定的Android版本通过测试,29是稳定的版本,不必启用任何向前兼容的行为
targetSdkVersion 29
// 版本号
versionCode 1
// 版本名称
versionName "1.0"
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 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
}