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不同罗丰,這個依賴可以傳遞,其他module無論在編譯時和運(yùn)行時都可以訪問這個依賴的實現(xiàn)再姑,也就是會泄漏一些不應(yīng)該不使用的實現(xiàn)萌抵。舉個例子,A依賴B,B依賴C绍填,如果都是使用api配置的話霎桅,A可以直接使用C中的類(編譯時和運(yùn)行時),而如果是使用implementation配置的話讨永,在編譯時滔驶,A是無法訪問C中的類的。
- implementation
與compile對應(yīng)卿闹,會添加依賴到編譯路徑揭糕,并且會將依賴打包到輸出(aar或apk),但是在編譯時不會將依賴的實現(xiàn)暴露給其他module锻霎,也就是只有在運(yùn)行時其他module才能訪問這個依賴中的實現(xiàn);
簡單的說著角,就是使用implementation指令的依賴不會傳遞;
使用這個配置,可以顯著提升構(gòu)建時間旋恼,因為它可以減少重新編譯的module的數(shù)量吏口。Google建議盡量使用這個依賴配置;
- compileOnly
與provided對應(yīng),Gradle把依賴加到編譯路徑冰更,編譯時使用产徊,不會打包到輸出(aar或apk)。這可以減少輸出的體積蜀细,在只在編譯時需要舟铜,在運(yùn)行時可選的情況,很有用
- apk
只在生成apk的時候參與打包审葬,編譯時不會參與深滚,很少用奕谭。
- testImplementation
只在單元測試代碼的編譯以及最終打包測試apk時有效涣觉。
- androidTestImplementation
只在Android相關(guān)單元測試代碼的編譯以及最終打包測試apk時有效。
- debugImplementation
只在 debug 模式的編譯和最終的 debug apk 打包時有效
- releaseImplementation
僅僅針對 Release 模式的編譯和最終的 Release apk 打包血柳。
引入依賴基本方式
理論上gradle支持三種類型的引用官册,方式如下:
dependencies {
implementation project(':projectABC')
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
}
1. 本地項目依賴 --> module依賴
dependencies {
implementation project(':projectABC')
}
這種依賴方式是直接依賴本地工程代碼,比如這個 :projectABC 就是在整個工程項目配置的 settings.gradle 中進(jìn)行include操作;
例如:
dependencies {
include ':projectABC'
}
2. 本地二進(jìn)制依賴 --> jar和so等文件
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
}
這種依賴方式是依賴工程中 libs 目錄下的Jar等文件难捌;
如果還想進(jìn)行單獨某個文件的引用
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類型引入
有時候我們在引入的時候還需要考慮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'
}
排除引用
有時候為了解決引入的沖突击敌,需要在引入遠(yuǎn)端包的同時排除這些包的某幾個依賴
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'
}
}