某些情况下安卓引入so冲突的解决
生活随笔
收集整理的這篇文章主要介紹了
某些情况下安卓引入so冲突的解决
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
前言
年前在做一個項目的時候,為了減小apk的大小,所以就把除了'armeabi'的so都給刪了,經(jīng)測試無不良情況.
前一段時間又要改某個sdk,換了一個so庫,必須要用64位的(arm64v8a),由于項目時間長了,換了次svn地址,所以就悲劇了,項目以前的64位so都被刪了也找不回來了,而新so庫必須要用64位的,這下就有問題了(加入arm64v8a,就需要其他so也支持64位的,直接把64位的放到armabi下引入會報錯)
解決方案
后來想到loadLibrary的方法有兩個:
1.System.loadLibrary
該方法直接去jniLibs文件夾內(nèi)尋找so并加載.
比如:jniLibs/armabi/libSDK.so? ? ? 調(diào)用方法為:System.loadLibrary("SDK");? 需要掐頭去尾
2.System.load
該方法可以加載本地File路徑的形式加載
于是可以把so文件放在本地,一般來說一種通過網(wǎng)絡下載到本地,另一種通過assets資源文件的形式復制到本地,我選用第二種方式
代碼實現(xiàn)
assets資源拷貝到本地的工具類
import android.content.Context; import android.content.res.AssetManager;import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.List;public class AssetCopyer {private String asset_list_fileName;private final Context mContext;private final AssetManager mAssetManager;private File mAppDirectory;public AssetCopyer(Context context, String asset_list_fileName) {mContext = context;mAssetManager = context.getAssets();this.asset_list_fileName = asset_list_fileName;}/*** 將assets目錄下指定的文件拷貝到sdcard中** @return 文件列表 // * @return 是否拷貝成功, true 成功;false 失敗* @throws IOException*/public List<File> copy() throws IOException {List<String> srcFiles = new ArrayList<>();//獲取系統(tǒng)在SDCard中為app分配的目錄,eg:/sdcard/Android/data/$(app's package)//該目錄存放app相關的各種文件(如cache,配置文件等),unstall app后該目錄也會隨之刪除mAppDirectory = mContext.getExternalFilesDir(null);if (null == mAppDirectory) {return null;}//讀取assets/$(subDirectory)目錄下的assets.lst文件,得到需要copy的文件列表List<String> assets = getAssetsList();for (String asset : assets) {//如果不存在,則添加到copy列表if (!new File(mAppDirectory, asset).exists()) {srcFiles.add(asset);}}List<File> fileList=new ArrayList<>();//依次拷貝到App的安裝目錄下for (String file : srcFiles) {fileList.add(copy(file));}return fileList;}/*** 獲取需要拷貝的文件列表(記錄在assets/assets.lst文件中)** @return 文件列表* @throws IOException*/protected List<String> getAssetsList() throws IOException {List<String> files = new ArrayList<>();/*InputStream listFile = mAssetManager.open(new File(asset_list_fileName).getPath());BufferedReader br = new BufferedReader(new InputStreamReader(listFile));String path;while (null != (path = br.readLine())) {files.add(path);}*/ //todo 懶省事,就不用資源內(nèi)的文件,而是直接用so文件名字進行拼接了for (String s : asset_list_fileName.split("##"))files.add(s);return files;}/*** 執(zhí)行拷貝任務** @param asset 需要拷貝的assets文件路徑* @return 拷貝成功后的目標文件句柄* @throws IOException*/protected File copy(String asset) throws IOException {InputStream source = mAssetManager.open(new File(asset).getPath());File destinationFile = new File(mAppDirectory, asset);if (destinationFile.exists()) {return destinationFile;}destinationFile.getParentFile().mkdirs();OutputStream destination = new FileOutputStream(destinationFile);byte[] buffer = new byte[1024];int nread;while ((nread = source.read(buffer)) != -1) {if (nread == 0) {nread = source.read();if (nread < 0)break;destination.write(nread);continue;}destination.write(buffer, 0, nread);}destination.close();return destinationFile;} }先把有相應的so文件放入assets文件夾中
然后調(diào)用工具類拷貝so文件,調(diào)用System.load()方法來加載相應的so文件
String files = "libIAL.so##libSDL.so";List<File> copy = new AssetCopyer(context, files).copy();for (File f : copy)System.load(f.getAbsolutePath());然后成功的引入了so文件
總結(jié)
以上是生活随笔為你收集整理的某些情况下安卓引入so冲突的解决的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 图像混合模式:Android Paint
- 下一篇: Android技能树 — LayoutI