//顶级生成文件,您可以在其中添加所有子项目/模块通用的配置选项。
buildscript {
//构建过程依赖的仓库
repositories {
google()
//代码托管仓库
mavenCentral()
}
dependencies {
//Gradle 插件及使用版本
classpath “com.android.tools.build:gradle:4.2.0”
//注意:不要将应用程序依赖项放在这里;他们属于
//app(单个模块)/build.gradle文件中
}
}
//这里面配置整个项目依赖的仓库,这样每个module就不用配置仓库了
allprojects {
repositories {
//代码托管仓库,可以引用 jcenter() 上任何的开源项目
google()
mavenCentral()
jcenter() // 警告:此存储库即将关闭
}
}
// 运行gradle clean时,执行此处定义的task。
// 该任务继承自Delete,删除根目录中的build目录。
// 相当于执行Delete.delete(rootProject.buildDir)
task clean(type: Delete) {
delete rootProject.buildDir
}
三、app/build.gradle
上面讲到了project中的build.gradle,下面咱们看看app/build.gradle
//都表示是一个应用程序的模块,可独立运行
//(两种方式因Android Studio版本不同产生的)
//apply plugin: ‘com.android.application’
plugins {
id ‘com.android.application’
}
都表示是一个依赖库,不能独立运行可生成jar或者aar
//apply plugin: ‘com.android.library’
//plugins {
// id ‘com.android.library’
//}
android {
compileSdkVersion 30//项目的编译版本
defaultConfig {
applicationId “com.scc.demo”//包名
minSdkVersion 23//最低的兼容的Android系统版本
targetSdkVersion 30//目标版本,表示你在该Android系统版本已经做过充分的测试
versionCode 1//版本号
versionName “1.0.0”//版本名称
ndk {
abiFilters ‘armeabi-v7a’, ‘arm64-v8a’
//运行环境,要上传Google Play必须兼容64位,这里仅兼容ARM架构
//对于ARM架构,32 位库位于armeabi-v7a 中。64 位等效项是arm64-v8a。
//对于x86体系结构,查找x86(用于 32 位)和 x86_64(用于 64 位)。
}
}
buildTypes {//配置了多个版本(设置自动打包环境)
release {
minifyEnabled false
proguardFiles getDefaultProguardFile(‘proguard-android.txt’), ‘proguard-rules.pro’
}
//正常情况上面这个够用了,下面的用来了解
//指定生成安装文件的配置,常有两个子包:release,debug,
// 咱这边多一个子包,多一种配置方式
// 注:直接运行的都是debug安装文件
getByName(“release”) {
minifyEnabled true//是否开启收缩、混淆、优化,true开启
shrinkResources = true // 是否开启资源收缩 ,true开启
//applicationIdSuffix 未设置默认包名 applicationId “com.scc.demo”
proguardFiles(getDefaultProguardFile(“proguard-android.txt”), “proguard-rules.pro”)
}
getByName(“debug”) {
applicationIdSuffix = “.debug”//打包后会变成com.scc.demo.debug
minifyEnabled false
}
/**
- initWith 属性允许您从其他生成类型复制配置,
*/
create(“staging”) {
initWith(getByName(“debug”))
applicationIdSuffix = “.staging”
}
}
//flavorDimensions多维度配置
flavorDimensions “scc”
productFlavors {
create(“demo”) {
// Assigns this product flavor to the “version” flavor dimension.
// If you are using only one dimension, this property is optional,
// and the plugin automatically assigns all the module’s flavors to
// that dimension.
dimension = “scc”
applicationIdSuffix = “.demo”
versionNameSuffix = “-demo”
}
create(“full”) {
dimension = “scc”
applicationIdSuffix = “.full”
versionNameSuffix = “-full”
}
}
//指定jdk版本
//设置代码编译的版本,一般是在使用JDK1.8时,配置这个,使编译出来的jar包让别人使用时更通用
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {//指定当前项目的所有依赖关系:本地依赖、库依赖、远程依赖
//本地测试,声明测试用列库
testImplementation ‘junit:junit:4.+’
//androidTestImplementation仅为测试APK添加远程依赖项。
androidTestImplementation ‘androidx.test.ext:junit:1.1.2’
//库依赖:(跟app同级文件的项目)
implementation(project(“:mylibrary”))
//本地依赖:Gradle在项目app/libs/目录中声明对jar文件的依赖
//(因为 Gradle读取相对于build.gradle文件的路径)。
implementation fileTree(dir: ‘libs’, include: [‘*.jar’])
//或者,你可以按如下方式指定单个文件
implementation(name: ‘app-release11’, ext: ‘aar’)
implementation files(‘libs/scc.jar’, ‘libs/stt.jar’)
//注意:像这样的远程依赖项要求你声明适当的远程存储库,Gradle 应在其中查找库。
// 远程依赖:如果该库在本地尚不存在,则 Gradle 会在构建需要时(例如,当你单击将项目与 Gradle 文件同步 或运行构建时)
// 从远程站点拉取它。
//全称应为implementation group: ‘com.android.billingclient’,
// name: ‘billing’, version: ‘3.0.0’
implementation ‘com.android.billingclient:billing:3.0.0’
}
//声明是要使用谷歌服务框架(这个一般放在gradle最下面)
apply plugin: ‘com.google.gms.google-services’