AndroidStudio build.gradle 配置
Android Studio是通过gradle来构建项目,gradle基于groovy语言。当用 AndroidStudio 创建工程时,会生成两个 build.gradle 文件,一个是工程的 build.gradle 文件,另一个是 module app 的 build.gradle 文件,接下来进行详细的介绍
工程 build.gradle 文件
作用
- 用于对整个工程进行配置 比如mave仓库
- 声明全局常量 用于统一版本控制
1. Android Gradle插件
-
'com.android.tools.build:gradle:3.5.2'
配置 Android Gradle插件版本,该插件添加了专用于编译 Android 应用的功能 - 查看官方文档 Gradle plugin Android Gradle DSL
2. 配置阿里云maven镜像
- 由于 maven 仓库时不时抽风,可以配置阿里maven镜像
- 阿里云maven仓库和文档https://maven.aliyun.com
allprojects {
repositories {
maven { url 'https://maven.aliyun.com/repository/public/' }
maven { url 'https://maven.aliyun.com/repository/google'}
// 当前配置的阿里云公共代理库 https://maven.aliyun.com
// public库是group库,其实代理了maven central和jcenter仓库
google()
jcenter()
}
3. 声明常量
- 声明常量,用于统一工程量版本号
ext {
// 声明一些常量 用于统一版本号
minSdkVersion = 15
targetSdkVersion = 26
compileSdkVersion = 26
buildToolsVersion = '29.0.2'
compileSdkVersion = 29
versionCode = 1
versionName = '1.0.1'
// App dependencies
junitVersion = '4.12'
v7Version = '26.1.0'
appcompatVersion='1.1.0'
}
示例
// 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.5.2'
// 这里 Android Gradle插件,该插件添加了专用于编译 Android 应用的功能
// 文档在这里 https://developer.android.google.cn/studio/releases/gradle-plugin
// 文档在这里 http://google.github.io/android-gradle-dsl/current/
}
}
allprojects {
repositories {
maven { url 'https://maven.aliyun.com/repository/public/' }
maven { url 'https://maven.aliyun.com/repository/google'}
// 当前配置的阿里云公共代理库 https://maven.aliyun.com
// public库是group库,其实代理了maven central和jcenter仓库
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
ext {
// 声明一些常量 用于统一版本号
minSdkVersion = 15
targetSdkVersion = 26
compileSdkVersion = 26
buildToolsVersion = '29.0.2'
compileSdkVersion = 29
versionCode = 1
versionName = '1.0.1'
// App dependencies
junitVersion = '4.12'
v7Version = '26.1.0'
appcompatVersion='1.1.0'
}
Module build.gradle 文件
作用
- 用于对当前module配置
1. 配置module类型(文件根节点)
-
apply plugin: 'com.android.application'
表示module为应用程序 -
apply plugin: 'com.android.library'
表示module为 Library 库
2. Android相关配置(android{}节点下)
2.1 NDK 配置 (android - defaultConfig节点下)
- abiFilters 用于设置编译哪些平台的so文件
android {
defaultConfig {
externalNativeBuild {
cmake {
cppFlags ""
}
ndk {
abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64', 'mips', 'mips64'
}
}
}
}
2.2 CMake 配置(android{}节点下)
- 配置CMakeLists路径
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.10.2"
}
}
2.3 sourceSets 文件映射关系配置(android{}节点下)
- assets
assets.srcDirs 'src/main/assets', 'src/main/zincAssets'
- jniLibs
jniLibs.srcDirs '目录1','目录2'
- manifest
manifest.srcFile 'src/main/ZincManifest.xml'
//文件映射关系
sourceSets {
main {//表示为main
jniLibs.srcDirs = ['libs']//nativie文件的目录
}
}
3. 添加相关依赖(dependencies{}节点下)
-
api与implementation区别
- implementation 表示只依赖,无法给其他上层模块使用,用于隔离;api 表示除了依赖之后还能给上层模块使用
-
引用全局版本常量
implementation "androidx.appcompat:appcompat:$rootProject.ext.appcompatVersion"
解决依赖冲突
implementation("com.zhihu.android:matisse:0.5.2-beta4") {
exclude group: 'com.android.support'//剔除所有support包
exclude group: 'com.android.support', module: 'design' //剔除support包中的design 模块
}
- 查看依赖树
- 命令行执行:gradlew app:dependencies 查看依赖树
示例
apply plugin: 'com.android.application'// 表示module为应用程序
//apply plugin: 'com.android.library' // 表示module为 Library 库
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
applicationId "com.example.testc"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode rootProject.ext.versionCode
versionName rootProject.ext.versionName
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags ""
}
ndk {
// abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64', 'mips', 'mips64'
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.10.2"
}
}
signingConfigs {
debugConfig {
keyAlias 'voice'
keyPassword 'biggerthanbigger'
storeFile file('release.jks')
storePassword 'biggerthanbigger'
}
release {
keyAlias 'voice'
keyPassword 'biggerthanbigger'
storeFile file('release.jks')
storePassword 'biggerthanbigger'
}
}
//文件映射关系
sourceSets {
main {
jniLibs.srcDirs = ['libs']//nativie文件的目录
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "androidx.appcompat:appcompat:$rootProject.ext.appcompatVersion"
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation "junit:junit:$rootProject.ext.junitVersion"
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
implementation("com.zhihu.android:matisse:0.5.2-beta4") {
exclude group: 'com.android.support'
exclude group: 'com.android.support', module: 'design'
// 命令行执行:gradlew app:dependencies 查看依赖树
}
}