Gradle依賴引入介紹

1. 引入關(guān)鍵字列表

4.x+版本 老版本(棄用) 說明
api compile 打包并傳遞
implementation compile 打包不傳遞
compileOnly provided 只在編譯時(shí)用
runtimeOnly apk 只在運(yùn)行時(shí)用 打包到apk
testImplementation testCompile 只在test用 打包到測試包
androidTestImplementation androidTestCompile 只在android test用 打包到測試包
debugImplementation debugCompile 只在debug模式有效 打包到debug包
releaseImplementation releaseCompile 只在release模式有效 打包到release包

2. 關(guān)鍵字說明

api

打包輸出到aar或apk,并且依賴向上傳遞。

implementation

打包輸出到aar或apk,依賴不傳遞。

compileOnly

編譯時(shí)使用,不會(huì)打包到輸出(aar或apk)。

runtimeOnly

只在生成apk的時(shí)候參與打包,編譯時(shí)不會(huì)參與,很少用。

testImplementation

只在單元測試代碼的編譯以及最終打包測試apk時(shí)有效。

androidTestImplementation

只在Android相關(guān)單元測試代碼的編譯以及最終打包測試apk時(shí)有效。

debugImplementation

只在 debug 模式的編譯和最終的 debug apk 打包時(shí)有效

releaseImplementation

僅僅針對(duì) Release 模式的編譯和最終的 Release apk 打包。

3. 各種依賴寫法

本地項(xiàng)目依賴

dependencies {
    implementation project(':projectABC')
}

本地包依賴

dependencies {
    // 批量引入
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    // 單個(gè)引入
    implementation files('libs/aaa.jar', 'libs/bbb.jar')
    implementation files('x/y/z/ccc.jar')
}

遠(yuǎn)程包依賴

dependencies {
    // 簡寫
    implementation 'androidx.appcompat:appcompat:1.0.2'
    // 完整寫法
    implementation  group: 'androidx.appcompat', name:'appcompat', version:'1.0.2'
}
根據(jù)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'
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容