AndroidStudio build.gradle 配置
Android Studio是通過gradle來構建項目放可,gradle基于groovy語言泽艘。當用 AndroidStudio 創(chuàng)建工程時,會生成兩個 build.gradle 文件恩掷,一個是工程的 build.gradle 文件蹄梢,另一個是 module app 的 build.gradle 文件嫉父,接下來進行詳細的介紹
工程 build.gradle 文件
作用
- 用于對整個工程進行配置 比如mave倉庫
- 聲明全局常量 用于統(tǒng)一版本控制
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. 聲明常量
- 聲明常量脓诡,用于統(tǒng)一工程量版本號
ext {
// 聲明一些常量 用于統(tǒng)一版本號
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 {
// 聲明一些常量 用于統(tǒng)一版本號
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類型(文件根節(jié)點)
-
apply plugin: 'com.android.application'
表示module為應用程序 -
apply plugin: 'com.android.library'
表示module為 Library 庫
2. Android相關配置(android{}節(jié)點下)
2.1 NDK 配置 (android - defaultConfig節(jié)點下)
- abiFilters 用于設置編譯哪些平臺的so文件
android {
defaultConfig {
externalNativeBuild {
cmake {
cppFlags ""
}
ndk {
abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64', 'mips', 'mips64'
}
}
}
}
2.2 CMake 配置(android{}節(jié)點下)
- 配置CMakeLists路徑
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.10.2"
}
}
2.3 sourceSets 文件映射關系配置(android{}節(jié)點下)
- 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{}節(jié)點下)
-
api與implementation區(qū)別
- 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 模塊
}
- 查看依賴樹
- 命令行執(zhí)行: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'
// 命令行執(zhí)行:gradlew app:dependencies 查看依賴樹
}
}