Android導入動態鏈庫可能會出現的錯誤

當你在Android工程中導入.so動態庫的時候,需要注意的幾點:

1、如果要直接使用別人項目中的庫,System.loadLibrary();方法所在的包名要注意,因爲別人在編譯這個動態鏈庫的時候,生成的頭文件的名字是依賴包名的。所以如果想直接用別人的庫,最直接方便的方法就是保持包名不變。

2、當你的測試機器是arm64位,出現couldn't find "xxxx.so"這種錯誤的時候,可能出錯原因是libs或者jniLibs中的arm64-v8a目錄下沒有對應的.so庫,如果沒有arm64-v8a對應的庫,可以刪除掉arm64-v8a這個目錄,因爲arm64能向下兼容,如下圖:

保留這幾個目錄就可以了。

有時候,工程中沒有arm64-v8a,但是最後打包出來發現apk包中有arm64-v8a這個目錄,但該目錄下沒有對應的庫(筆者就遇到過這種錯誤)。這時候,clean一下工程,然後module的build.gradle中的defaultConfig中添加以下代碼:

 ndk {
            abiFilters "armeabi", "armeabi-v7a", "x86", "mips"
        }
如下:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"
    defaultConfig {
        applicationId "com.paxsz.ossdemo"
        minSdkVersion 19
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        ndk {
            abiFilters "armeabi", "armeabi-v7a", "x86", "mips"
        }
    }
    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
repositories {
    flatDir {
        dirs project(':taesdk').file('libs')
    }
}
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.0.1'
    testCompile 'junit:junit:4.12'

}
這時候再重新編譯一下,應該問題就解決了。
BTW:

如果測試機器root之後,可以使用終端來查看安裝的軟件中是否包含有需要加載的動態庫。按照下面步驟進行:

第一步:su命令獲取root權限;

第二步:cd data/app && ls;這是能看到手機中安裝的各個軟件。找到你要查看到的軟件.

第三步:cd packagename(要查看的軟件包名);

第四步:cd lib && ls;

這時候就能看到手機加載的庫的目錄,cd 進去之後,就能看到動態庫了。

發佈了35 篇原創文章 · 獲贊 35 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章