Android jar、so導包沖突

jar包沖突

報錯信息
Duplicate class com.google.gson.stream.MalformedJsonException found in modules jetified-gson-2.8.6 (com.google.code.gson:gson:2.8.6) and**** (******.jar)


Error:Could not find method exclude() for arguments [{group=com.google.code.gson, module=gson}] on directory '{include=*.jar, dir=libs}' of type org.gradle.api.internal.file.collections.DefaultConfigurableFileTree.
解決辦法

最后試來試去,才發現exclude需要寫在App 主Module 的build.gradle文件中才能生效,而且注意 project(‘:Speech’) 外面那層括號:

apply plugin: 'com.android.application'     //注意這是主Module

repositories {
    mavenCentral()
}

dependencies {
    // Module dependency
    implementation(project(':Speech')){
        //解決Gson重復依賴問題,與passport-1.4.2.jar有沖突
        exclude group: 'com.google.code.gson', module: 'gson'       
    }
}

so庫沖突

報錯信息
More than one file was found with OS independent path 'lib/x86/libfbjni.so'. If you are using jniLibs and CMake IMPORTED targets, 
解決方案

這種是網上普遍的解決方案,有的時候雖然so庫名字相同,但是邏輯不同,名字重復的時候選擇第一個不太科學;

packagingOptions {
         pickFirst 'lib/armeabi-v7a/libfbjni.so'
         pickFirst 'lib/arm64-v8a/libfbjni.so'
         pickFirst 'lib/x86_64/libfbjni.so'
         pickFirst 'lib/x86/libfbjni.so'
}

我們可以在:app主項目build.gradle文件最后中添加以下腳本打印出所有so庫目錄,對可以修改so庫刪除或者改名進行屏蔽。

tasks.whenTaskAdded { task ->
    if (task.name == 'mergeDebugNativeLibs') {
        println("------------------- mergeDebugNativeLibs -------------------")
        task.doFirst {
            println("------------------- find so files start -------------------")
            it.inputs.files.each { file ->
                printDir(new File(file.absolutePath))
            }
            println("------------------- find so files end -------------------")
        }
    }
}

def printDir(File file) {
    if (file != null) {
        if (file.isDirectory()) {
            file.listFiles().each {
                printDir(it)
            }
        } else if (file.absolutePath.endsWith(".so")) {
            println "find so file: $file.absolutePath"
        }
    }
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容