gradle 中implementation ,compile ,api的區別

首先在AndroidStudio3.x中compile已經過時

由implementation和api來代替

 

 

其次compile與api完全等同 3.x中可以完全將compile換成api

mplementation與api的區別

implementation編譯的依賴只作用於當前的module。即app module依賴了other module,

other module中使用implementation編譯的三方庫(如glide)只對other module起作用,

app module中無法使用該三方庫(glide);

如果other module 使用implementation依賴了base module,則APP module中無法使用base module。

舉個示例:

我們把其它module中的對com.alibaba.fastjson:1.2.6引用改成implementation

 

 

 那麼我們在app module裏面是無法使用的 因爲implementation 只對當前module生效

便會提示:錯誤: 程序包com.alibaba.fastjson不存在

 

 

 

總體介紹

compile(api)
這種是我們最常用的方式,使用該方式依賴的庫將會參與編譯和打包。

provided(compileOnly)
只在編譯時有效,不會參與打包,可以在自己的moudle中使用該方式依賴。比如com.android.support,gson這些使用者常用的庫,避免衝突。

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

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

debugCompile(debugImplementation)
debugCompile 只在debug模式的編譯和最終的debug apk打包時有效。

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章