前言
在開發(fā)游戲SDK時會使用一些第三方庫,例如上文提到的 OkHttp ,或者集成某些第三方,而這些第三方使用的系統(tǒng)庫(例如 supportv7)和項目組使用的版本不一致,在編譯時就會出現(xiàn)版本沖突問題。解決辦法有兩個:
- 盡量不使用第三庫,例如針對 OkHttp ,如果沒有特殊的需求(比如需要中斷某個請求),可以不使用 OkHttp ,自行開發(fā)一個基于系統(tǒng) API 的網(wǎng)絡框架。
- 解決版本沖突。以下提供一些解決版本沖突的方法。
版本沖突可能出現(xiàn)的報錯提示及對應解決方案
- 加入AndroidX之后 targetVersion= 26,報錯:Android resource linked error
報錯原因:按字面意思就是找不到對應的資源。
解決思路:
- 檢查找不到的資源相關庫是否已添加;
- 如已添加,查看是否出現(xiàn)版本沖突,可能是使用了AndroidX 的庫,但是項目里有support庫。
- 可以檢查項目是否已配置:
android.useAndroidX=true
(表示“Android插件會啟用對應的AndroidX庫,而非Support庫”;) -
compileSdkVersion
版本提相對高一點,比如 29,不必一定和targetVersion
保持一致。
- 可以檢查項目是否已配置:
- 清除緩存(項目里的緩存)、或者電腦里的緩存,用戶/.gradle目錄。
- 用以下介紹的一半解決版本沖突方法嘗試解決。
2.A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade
3. java.lang.ClassNotFoundException: Didn’t find class "android.support.v4.content.ContextCompat”
- 這里是示例,按報錯提示是 support.v4 這個系統(tǒng)庫版本出現(xiàn)版本沖突。
解決沖突一般思路
-
使用
gradle
命令分析dependencies
,查看哪里出現(xiàn)沖突,在對應的第三方文件中去除對應的沖突文件。 查看依賴樹的方法有三種:- 在終端使用命令:
./gradlew app:dependencies
,app
代表需要分析的module
。 -
./gradlew app:dependencies --configuration releaseRuntimeClasspath
,releaseRuntimeClasspath
指具體的variants
類型。./gradlew :app:dependencies --configuration implementation
只查看implementation
的依賴樹。 - 點擊右側
gradle
-app
-tasks
-help
-dependencies
運行。 - 查看某個依賴庫中的依賴:
./gradlew :app:dependencyInsight --dependency <依賴名> --configuration compile
- 在終端使用命令:
-
分析依賴樹
+--- androidx.appcompat:appcompat:1.2.0 | | | +--- androidx.annotation:annotation:1.1.0 | | | +--- androidx.core:core:1.3.0 -> 1.3.1 | | | | +--- androidx.annotation:annotation:1.1.0 | | | | +--- androidx.lifecycle:lifecycle-runtime:2.0.0 -> 2.1.0 | | | | | +--- androidx.lifecycle:lifecycle-common:2.1.0 | | | | | | \--- androidx.annotation:annotation:1.1.0 | | | | | +--- androidx.arch.core:core-common:2.1.0 | | | | | | \--- androidx.annotation:annotation:1.1.0 | | | | | \--- androidx.annotation:annotation:1.1.0 | | | | +--- androidx.versionedparcelable:versionedparcelable:1.1.0 | | | | | +--- androidx.annotation:annotation:1.1.0 | | | | | \--- androidx.collection:collection:1.0.0 -> 1.1.0 | | | | | \--- androidx.annotation:annotation:1.1.0 | | | | \--- androidx.collection:collection:1.0.0 -> 1.1.0 (*) | | | +--- androidx.cursoradapter:cursoradapter:1.0.0 | | | | \--- androidx.annotation:annotation:1.0.0 -> 1.1.0 | | | +--- androidx.fragment:fragment:1.1.0 | | | | +--- androidx.annotation:annotation:1.1.0 | | | | +--- androidx.core:core:1.1.0 -> 1.3.1 (*) | | | | +--- androidx.collection:collection:1.1.0 (*) | | | | +--- androidx.viewpager:viewpager:1.0.0 | | | | | +--- androidx.annotation:annotation:1.0.0 -> 1.1.0 | | | | | +--- androidx.core:core:1.0.0 -> 1.3.1 (*) | | | | | \--- androidx.customview:customview:1.0.0 | | | | | +--- androidx.annotation:annotation:1.0.0 -> 1.1.0 | | | | | \--- androidx.core:core:1.0.0 -> 1.3.1 (*)
- 加
*
的表示這個依賴被忽略,因為其他頂級依賴中也依賴了這個傳遞的依賴, Gradle 會自動分析下載最適合的依賴。 - 1.0.0 -> 1.1.0 的表示會先使用高版本,如果需要使用低版本的 API ,需要排除掉高版本。
- 加
-
排除版本
-
排除掉某個 module
// 從 com.google.android.gms:play-services-base:15.0.1 這個依賴庫中排除掉 com.android.support 組,包括 supportv7、supportv4 api("com.google.android.gms:play-services-base:15.0.1") { exclude group: 'com.android.support' } // 其他示例 implementation("com.github.bumptech.glide:glide:4.13.0"){ exclude group:"androidx.vectordrawable" exclude group:"androidx.viewpager" exclude group:"androidx.loader" } // 從 com.google.android.gms:play-services-base:15.0.1 這個依賴庫中排除掉 supportV4 api("com.google.android.gms:play-services-base:15.0.1") { exclude module: 'support-v4' }
-
統(tǒng)一指定一個版本
configurations.all { // 遍歷所有依賴庫 resolutionStrategy.eachDependency { DependencyResolveDetails details -> def requested = details.requested // 查到所有 support 庫 if (requested.group == 'com.android.support') { // multidex 使用 1.0.3 ,其他使用 27.0.2 版本 if (!requested.name.startsWith("multidex")) { details.useVersion '27.0.2' }else { details.useVersion '1.0.3' } } } }
或者使用以下方法指定統(tǒng)一版本
configurations.all { resolutionStrategy { force 'com.android.support:support-v4:28.0.0' } }
4. OkHttp 4.x 版本和 3.x 版本的沖突。
報錯信息1:java.lang.NoSuchFieldError: No field Companion of type Lokhttp3/MediaType$Companion; in class Lokhttp3/MediaType; or its superclasses (declaration of ‘okhttp3.MediaType’ appears in /data/app/XXX.bilibili-l1q_xuHU_zW9STBi7KFdug==/base.apk)
解決辦法:
- 參考 OkHttp 官方文檔:https://square.github.io/okhttp/#releases
https://square.github.io/okhttp/changelogs/upgrading_to_okhttp_4/ 查看更新,4.x 可以兼容 3.x。 版本號不變,使用老版本的方法:
//使用 MediaType.parse ,不使用 MediaType.Comparion 如果是 3.9.0 以下也不能使用 MediaType.get // RequestBody.create 新版本是參數(shù)在前類型在后 // 3.X 版本是 類型在前RequestBody.create(JSON, JsonUtils.map2jsonObject(params.getParams()).toString());
報錯信息2:Failed resolution of: Lokhttp3/internal/Version;
解決辦法:在項目里增加 一個和 okhttp 庫的同樣缺少的包名和類。
-
批量修改第三方jar包包名
解決版本沖突還有一種方法,就是批量修改了第三方的包名,使用的時候使用的修改后的 的 jar 包,這樣能保證和其他人使用的第三方庫不沖突。但是這個方法有個很大的弊端,不建議使用。 弊端是,這種修改的方式是無法修改 反射調用的類名包名的,這樣修改 jar 包包名后反射調用的方法就會出錯。
修改第三方 jar 包包名方法:
1、下載一個工具 jarjar
,官網(wǎng)下載地址:code.google.com/p/jarjar/
2、新建一個文件夾,將jarjar
和需求修改包名的 jar 放到這個文件夾下,使用 txt 編寫一個修改規(guī)則。
3、txt 中輸入內容:文章來源:http://www.zghlxwxcb.cn/news/detail-426964.html
rule pattern result
# org.apache.commons 表示我們需要修改的 jar 包的包名,
# org.apache.commons.android.@1 表示我們需要修改的包名
rule org.apache.commons.** org.apache.commons.android.@1
4、通過 cmd 執(zhí)行命令行修改文章來源地址http://www.zghlxwxcb.cn/news/detail-426964.html
java -jar jarjar.jar process <rulesFile> <inJar> <outJar>
到了這里,關于Android 解決第三方庫版本沖突的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!