1苹粟,引進的第三方庫中所依賴的庫與主工程中同時存在且版本不同产舞。
例如:XImageView主項目與應(yīng)用ximageviewlib moudle都引用了并使用了
implementation 'com.android.support:appcompat-v7:28.0.0'
我們打出的包為 1424KB
現(xiàn)在我們將ximageviewlib中引用的庫改為:
implementation 'com.android.support:appcompat-v7:27.1.0'
發(fā)現(xiàn)打出的包大小一樣
同時將ximageviewlib的targetSdkVersion 28 改為 27后再試試
結(jié)果仍然是1424KB
同時將ximageviewlib的targetSdkVersion 28 和 compileSdkVersion 28夠改為27后
結(jié)果仍然是1424KB
我們將主項目
Android dependency 'com.android.support:support-core-utils' has different version for the compile (27.1.0) and runtime (28.0.0) classpath. You should manually set the same version via DependencyResolution
2,compileOnly與runtimeOnly應(yīng)用場景的不同與對apk大小的影響
3把将,support包與第三方依賴庫版本不一致的問題
解決方法(同樣的適用于其他的依賴沖突痒蓬。)
1)修改自己項目中的com.android.support的版本號魔眨,與所依賴的庫版本號一致,但是當我們依賴的庫中的com.android.support版本號有好幾個版本就不行了底扳。(不推薦)
2)常見方法
api("com.afollestad.material-dialogs:core:0.9.4.7") {
exclude group: 'com.android.support', module: 'support-v13'
}
比如對于 Gradle 遠程依賴的兩個第三方庫铸抑,他們內(nèi)部同時依賴相同的另一個輔助第三方庫。這個時候我們就沒辦法像第一種情況那樣手動刪除本地文件衷模。好在 Gradle 插件提供了相應(yīng)的解決方案鹊汛,即使用 exclude 語法,如:
compile 'com.yifeng.example:example-1:1.0'
compile ('com.yifeng.example:example-2:1.0') {
exclude group: 'com.android.support', module: 'support-v4'
}
如例子中所示阱冶,遠程依賴的第三方庫 example-1 與 example-2 內(nèi)部同時引入 support-v4 包刁憋,那么只需要在其中一個的引入地方添加 exclude 語句,根據(jù) group 和 module 過濾規(guī)則木蹬,將相同引入的 v4 包剔除在外即可至耻。
當需要在一個依賴中去除多個遞歸依賴項時,可以使用多條 exclude 語句届囚,比如:
compile ('com.wdullaer:materialdatetimepicker:3.2.2') {
exclude group: 'com.android.support', module: 'support-v4'
exclude group: 'com.android.support', module: 'design'
}
還有一種更簡單的寫法有梆,使用 Groove 語言的循環(huán)語句,模板如下(同樣適用于 module 規(guī)則):
compile() { dep ->
[group1, group2].each{ group -> dep.exclude group: group }
}
對于上面的例子意系,便可以改造成:
compile() { dep ->
['support-v4', 'support-v13', 'design'].each{ module -> dep.exclude module: module }
}
推薦這種方法泥耀,如果發(fā)生沖突了,依賴第三方庫時候排除掉對com.android.support包的依賴蛔添,這樣自己的項目隨便依賴什么版本都可以痰催。
另外還有一個建議,在我們自己創(chuàng)建library給別人使用時迎瞧,如果需要依賴com.android.support的話夸溶,建議用provided的方式依賴,這樣只會在編譯時有效凶硅,不會參與打包缝裁。避免給使用者帶來不便。
provided 'com.android.support:appcompat-v7:26.1.0'
provided 'com.android.support:design:26.1.0'
provided 'com.android.support:support-vector-drawable:26.1.0'
4足绅,嵌套庫依賴的版本控制問題怎么解決
比如
Manifest merger failed : uses-sdk:minSdkVersion 15 cannot be smaller than version 16 declared in library [com.zhxh:xbuttonlib:3.8] C:\Users\zhxh.gradle\caches\transforms-1\files-1.1\xbuttonlib-3.8.aar\fae3e2f73319b4c37bfc7b235d694cf6\AndroidManifest.xml as the library might be using APIs not available in 15
Suggestion: use a compatible library with a minSdk of at most 15,
or increase this project's minSdk version to at least 16,
or use tools:overrideLibrary="com.zhxh.xbuttonlib" to force usage (may lead to runtime failures)
暫時先寫這些捷绑,這兩天整理出來