背景:写了好久的Android代码,对于gradle一直不太理解,今天开始从头学习gradle;
build.gradle基本结构如下图:
buildscript {
repositories {
// 仓库
google()
jcenter()
mavenCentral()
}
dependencies {
// 依赖
classpath 'com.android.tools.build:gradle:3.3.1
}
}
apply plugin:'com.android.application' 应用模块
apply plugin: 'com.android.library' 依赖模块
基本任务:
assemble: 创建一个debug版本的APK
check: 运行所有的检查
Build:触发assemble和check
clean: 清楚项目的输出
顶层构建文件:
buildscript {
repositories {
// 仓库
jcenter()
}
dependencies {
// 依赖
classpath 'com.android.tools.build:gradle:3.3.1
}
}
allprojects {
// 被用于所有模块的属性
repositories {
// 仓库
jcenter()
}
}
模块的构建文件:
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 30 // 用来编译应用的Android APi版本
buildToolVersion "22.0.1" // 构建工具和编译器使用的版本号
defaultConfig {
applicationId "com.ford.sync.gradleapp" //作为应用的唯一标志
minSdkVersion 16 // 被用来配置运行应用的最小API级别
targetSdkVersion 30 //用于通知系统,该应用已经在特定的Android 版本通过测试
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
buildConfigField "boolean" ,"LOG_HTTP_CALLS","true"
}
debug {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
buildConfigField "boolean" ,"LOG_HTTP_CALLS","false"
// java代码中可使用BuildConfig.LOG_HTTP_CALLS来使用
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
原来package name有两个用途
1.作为应用的唯一标志 ----application id
2.R资源和类中作为包名 -----保留,继续用package name
minSdkVersion 被用来配置运行应用的最小API级别
targetSdkVersion 用于通知系统,该应用已经在特定的Android 版本通过测试
versionCode和versionName 覆盖AndroidManifest
defaultTasks 'clean','build' 指定默认的任务
Android应用依赖标志配置:
- compile
- apk
- provider
- testCompile
- androidTestCompile
网友评论