Gradle依賴引入
關(guān)鍵詞說明
自Android studio版本更新至3.0后,連帶著com.android.tools.build:gradle 工具也升級到了3.0.0,在3.0.0中使用了最新的Gralde 4.0 里程碑版本作為gradle的編譯版本,該版本gradle編譯速度有所加速;
Gradle新老版本關(guān)鍵字
| 4.x+版本配置 | 已棄用配置 |
|---|---|
| api | compile |
| implement | compile |
| compileOnly | provided |
| runtimeOnly | apk |
| testImplementation | testCompile |
| androidTestImplementation | androidTestCompile |
| debugImplementation | debugCompile |
| releaseImplementation | releaseCompile |
- api
與compile對應(yīng),功能完全一樣,會添加依賴到編譯路徑,并且會將依賴打包到輸出(aar或apk),與implementation不同,這個(gè)依賴可以傳遞,其他module無論在編譯時(shí)和運(yùn)行時(shí)都可以訪問這個(gè)依賴的實(shí)現(xiàn),也就是會泄漏一些不應(yīng)該不使用的實(shí)現(xiàn)。舉個(gè)例子,A依賴B,B依賴C,如果都是使用api配置的話,A可以直接使用C中的類(編譯時(shí)和運(yùn)行時(shí)),而如果是使用implementation配置的話,在編譯時(shí),A是無法訪問C中的類的。
- implementation
與compile對應(yīng),會添加依賴到編譯路徑,并且會將依賴打包到輸出(aar或apk),但是在編譯時(shí)不會將依賴的實(shí)現(xiàn)暴露給其他module,也就是只有在運(yùn)行時(shí)其他module才能訪問這個(gè)依賴中的實(shí)現(xiàn);
簡單的說,就是使用implementation指令的依賴不會傳遞;
使用這個(gè)配置,可以顯著提升構(gòu)建時(shí)間,因?yàn)樗梢詼p少重新編譯的module的數(shù)量。Google建議盡量使用這個(gè)依賴配置;
- compileOnly
與provided對應(yīng),Gradle把依賴加到編譯路徑,編譯時(shí)使用,不會打包到輸出(aar或apk)。這可以減少輸出的體積,在只在編譯時(shí)需要,在運(yùn)行時(shí)可選的情況,很有用
- apk
只在生成apk的時(shí)候參與打包,編譯時(shí)不會參與,很少用。
- testImplementation
只在單元測試代碼的編譯以及最終打包測試apk時(shí)有效。
- androidTestImplementation
只在Android相關(guān)單元測試代碼的編譯以及最終打包測試apk時(shí)有效。
- debugImplementation
只在 debug 模式的編譯和最終的 debug apk 打包時(shí)有效
- releaseImplementation
僅僅針對 Release 模式的編譯和最終的 Release apk 打包。
引入依賴基本方式
理論上gradle支持三種類型的引用,方式如下:
dependencies {
implementation project(':projectABC')
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
}
1. 本地項(xiàng)目依賴 --> module依賴
dependencies {
implementation project(':projectABC')
}
這種依賴方式是直接依賴本地工程代碼,比如這個(gè) :projectABC 就是在整個(gè)工程項(xiàng)目配置的 settings.gradle 中進(jìn)行include操作;
例如:
dependencies {
include ':projectABC'
}
2. 本地二進(jìn)制依賴 --> jar和so等文件
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
}
這種依賴方式是依賴工程中 libs 目錄下的Jar等文件;
如果還想進(jìn)行單獨(dú)某個(gè)文件的引用
dependencies {
implementation files('libs/aaa.jar', 'libs/bbb.jar')
implementation files('x/y/z/ccc.jar')
}
注意:Gradle的路徑是相對于build.gradle文件來讀取的,所以上面是這樣的相對路徑
3.遠(yuǎn)端二進(jìn)制依賴
dependencies {
implementation 'androidx.appcompat:appcompat:1.0.2'
}
這是簡潔寫法,也可以進(jìn)行完整寫法,如:
dependencies {
implementation group: 'androidx.appcompat', name:'appcompat', version:'1.0.2'
}
引入依賴復(fù)雜方式
根據(jù)Task類型引入
有時(shí)候我們在引入的時(shí)候還需要考慮debug,release,test包的情況如
dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.0-beta-2'
releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:2.0-beta-2'
}
排除引用
有時(shí)候?yàn)榱私鉀Q引入的沖突,需要在引入遠(yuǎn)端包的同時(shí)排除這些包的某幾個(gè)依賴
dependencies {
implementation ('com.github.bumptech.glide:glide:4.9.0'){
exclude group:'com.android.support', module: 'support-fragment'
exclude group:'com.android.support', module: 'support-core-ui'
exclude group:'com.android.support', module: 'support-compat'
exclude group:'com.android.support', module: 'support-annotations'
}
}