1. 引入關鍵字列表
4.x+版本 | 老版本(棄用) | 說明 |
---|---|---|
api | compile | 打包並傳遞 |
implementation | compile | 打包不傳遞 |
compileOnly | provided | 只在編譯時用 |
runtimeOnly | apk | 只在運行時用 打包到apk |
testImplementation | testCompile | 只在test用 打包到測試包 |
androidTestImplementation | androidTestCompile | 只在android test用 打包到測試包 |
debugImplementation | debugCompile | 只在debug模式有效 打包到debug包 |
releaseImplementation | releaseCompile | 只在release模式有效 打包到release包 |
2. 關鍵字說明
api
打包輸出到aar或apk,並且依賴向上傳遞。
implementation
打包輸出到aar或apk,依賴不傳遞。
compileOnly
編譯時使用,不會打包到輸出(aar或apk)。
runtimeOnly
只在生成apk的時候參與打包,編譯時不會參與,很少用。
testImplementation
只在單元測試代碼的編譯以及最終打包測試apk時有效。
androidTestImplementation
只在Android相關單元測試代碼的編譯以及最終打包測試apk時有效。
debugImplementation
只在 debug 模式的編譯和最終的 debug apk 打包時有效
releaseImplementation
僅僅針對 Release 模式的編譯和最終的 Release apk 打包。
3. 各種依賴寫法
本地項目依賴
dependencies {
implementation project(':projectABC')
}
本地包依賴
dependencies {
// 批量引入
implementation fileTree(dir: 'libs', include: ['*.jar'])
// 單個引入
implementation files('libs/aaa.jar', 'libs/bbb.jar')
implementation files('x/y/z/ccc.jar')
}
遠程包依賴
dependencies {
// 簡寫
implementation 'androidx.appcompat:appcompat:1.0.2'
// 完整寫法
implementation group: 'androidx.appcompat', name:'appcompat', version:'1.0.2'
}
根據Task類型(debug, release, test)引入
dependencies {
// test
testImplementation 'junit:junit:4.12'
// android test
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
// debug
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.0-beta-2'
// release
releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:2.0-beta-2'
}
排除引用(解決引入衝突)
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'
}
}