本文主要是介绍Android 11 ,Android Studio 三方APP调用系统 system/lib 目录下so库使用过程中的BUG(坑)。,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
背景介绍:最近在做文件管理器,需要把图库的功能集成到文件管理器中,实现在文件管理器中打开图片、切换图片的功能。在实现gif图片打开功能的时候,需要用到系统现成的gifplayer so库,导入Android studio的时候遇到了一堆问题,解决了感觉有必要记录一下主要的两个问题。
一、Android studio 导入so库。
按照下图创建目录导入。
java代码中使用System.loadLibrary导入so库使用:
package com.xuhao;import android.graphics.Bitmap;
import android.os.Handler;
import android.os.HandlerThread;import java.io.File;public class GifPlayer {static {System.loadLibrary("gifplayer");System.loadLibrary("gifplayer_jni");}private native long native_create();private native boolean native_load(long ptr,String gifPath);private native void native_play(long ptr, boolean loop, Bitmap bitmap,Runnable runnable);private native int native_getWidth(long ptr);private native int native_getHeight(long ptr);private native void native_pause(long ptr);private native void native_resume(long ptr);private native void native_stop(long ptr);private native void native_release(long ptr);public interface OnDrawGifCallBack {void onDrawFrame(Bitmap bitmap);}private Bitmap mBitmap;private OnDrawGifCallBack mOnDrawGifCallBack;private long mGifPlayerPtr = 0L;private final static String TAG = "GifPlayer";private HandlerThread mHandlerThread;private Handler mWorkHandler;public GifPlayer() {mHandlerThread = new HandlerThread("GifPlayer");mHandlerThread.start();mWorkHandler = new Handler(mHandlerThread.getLooper());mGifPlayerPtr = native_create();}public void setOnDrawGifCallBack(OnDrawGifCallBack onDrawGifCallBack) {this.mOnDrawGifCallBack = onDrawGifCallBack;}public void play(final String gifPath, final boolean loop){boolean ret = checkIfFileExist(gifPath);if (!ret) {throw new IllegalArgumentException("This isn't a valid gif path");} else {mWorkHandler.post(new Runnable() {@Overridepublic void run() {if (native_load(mGifPlayerPtr, gifPath)) {mBitmap = Bitmap.createBitmap(native_getWidth(mGifPlayerPtr), native_getHeight(mGifPlayerPtr), Bitmap.Config.ARGB_8888);native_play(mGifPlayerPtr, loop, mBitmap, new Runnable() {@Overridepublic void run() {if (mOnDrawGifCallBack != null) {mOnDrawGifCallBack.onDrawFrame(mBitmap);}}});}}});}}public void pause() {native_pause(mGifPlayerPtr);}public void resume() {native_resume(mGifPlayerPtr);}public void stop() {native_stop(mGifPlayerPtr);}public void release() {native_stop(mGifPlayerPtr);mWorkHandler.post(new Runnable() {@Overridepublic void run() {native_release(mGifPlayerPtr);mBitmap = null;mWorkHandler = null;mHandlerThread.quit();mHandlerThread = null;}});}private boolean checkIfFileExist(String gifPath) {if (gifPath != null && gifPath.length() != 0) {File file = new File(gifPath);if (file.exists() && _getFormatName(gifPath).equals("gif")) {return true;}}return false;}private String _getFormatName(String gifPath) {if (gifPath != null && gifPath.length() != 0) {gifPath = gifPath.trim();String s[] = gifPath.split("\\.");if (s.length >= 2) {return s[s.length - 1];}}return "";}}
注:java文件所在的包名必须和系统中so库源码jni的名字路径相同,这一点非常重要,是规则必须遵守。比如系统中的jni文件叫com_xuhao_GifPlayer.cpp,那么Android Studio 中必须把GifPlayer.java放在路径com.xuhao下才行。
到目前为止都是在按流程走。
二、解决报错
1、第一个报错
E AndroidRuntime: java.lang.UnsatisfiedLinkError: dlopen failed: library "/system/lib/libgifplayer_jni.so" needed or dlopened by "/apex/com.android.art/lib/libnativeloader.so" is not accessible for the namespace "classloader-namespace"
解决方法:
上面的报错,意思是无法导入system/lib/目录下的libgifplayer_jni.so库,libgifplayer_jni.so不是public的,需要加入白名单,Android 11 的白名单文件位置为:system\core\rootdir\etc\public.libraries.android.txt ,在该文件末尾加上要使用的so库名字:
2、第二个报错
08-28 21:35:04.482 6950 6950 I ilicon.explore: The ClassLoaderContext is a special shared library.
08-28 21:35:04.504 6950 6950 I chatty : uid=1000(system) com.hisilicon.explorer identical 2 lines
08-28 21:35:04.512 6950 6950 I ilicon.explore: The ClassLoaderContext is a special shared library.
08-28 21:35:04.537 6950 6950 D NetworkSecurityConfig: Using Network Security Config from resource network_security_config debugBuild: false
08-28 21:35:04.554 6950 6950 W ContextImpl: Calling a method in the system process without a qualified user: android.app.ContextImpl.startService:1669 android.content.ContextWrapper.startService:720 com.hisilicon.explorer.contentprovider.ServerDataProvider.f:3 com.hisilicon.explorer.contentprovider.ServerDataProvider.onCreate:4 android.content.ContentProvider.attachInfo:2388
08-28 21:35:04.556 6950 6950 W ContextImpl: Calling a method in the system process without a qualified user: android.app.ContextImpl.bindService:1748 android.content.ContextWrapper.bindService:756 com.hisilicon.explorer.contentprovider.ServerDataProvider.f:7 com.hisilicon.explorer.contentprovider.ServerDataProvider.onCreate:4 android.content.ContentProvider.attachInfo:2388
08-28 21:35:04.539 6950 6950 D NetworkSecurityConfig: Using Network Security Config from resource network_security_config debugBuild: false
08-28 21:35:04.576 6950 6950 E ilicon.explore: ----- class 'Lcom/softwinner/GifPlayer;' cl=0x12d11a28 -----
08-28 21:35:04.576 6950 6950 E ilicon.explore: objectSize=176 (172 from super)
08-28 21:35:04.576 6950 6950 E ilicon.explore: access=0x0008.0001
08-28 21:35:04.576 6950 6950 E ilicon.explore: super='java.lang.Class<java.lang.Object>' (cl=0x0)
08-28 21:35:04.576 6950 6950 E ilicon.explore: vtable (1 entries, 11 in super):
08-28 21:35:04.576 6950 6950 E ilicon.explore: 0: void com.softwinner.GifPlayer.g()
08-28 21:35:04.576 6950 6950 E ilicon.explore: direct methods (11 entries):
08-28 21:35:04.576 6950 6950 E ilicon.explore: 0: void com.softwinner.GifPlayer.<clinit>()
08-28 21:35:04.576 6950 6950 E ilicon.explore: 1: void com.softwinner.GifPlayer.<init>()
08-28 21:35:04.576 6950 6950 E ilicon.explore: 2: long com.softwinner.GifPlayer.a(com.softwinner.GifPlayer)
08-28 21:35:04.576 6950 6950 E ilicon.explore: 3: android.graphics.Bitmap com.softwinner.GifPlayer.b(com.softwinner.GifPlayer, android.graphics.Bitmap)
08-28 21:35:04.576 6950 6950 E ilicon.explore: 4: void com.softwinner.GifPlayer.c(com.softwinner.GifPlayer, long)
08-28 21:35:04.576 6950 6950 E ilicon.explore: 5: android.os.Handler com.softwinner.GifPlayer.d(com.softwinner.GifPlayer, android.os.Handler)
08-28 21:35:04.576 6950 6950 E ilicon.explore: 6: android.os.HandlerThread com.softwinner.GifPlayer.e(com.softwinner.GifPlayer)
08-28 21:35:04.576 6950 6950 E ilicon.explore: 7: android.os.HandlerThread com.softwinner.GifPlayer.f(com.softwinner.GifPlayer, android.os.HandlerThread)
08-28 21:35:04.576 6950 6950 E ilicon.explore: 8: long com.softwinner.GifPlayer.native_create()
08-28 21:35:04.576 6950 6950 E ilicon.explore: 9: void com.softwinner.GifPlayer.native_release(long)
08-28 21:35:04.576 6950 6950 E ilicon.explore: 10: void com.softwinner.GifPlayer.native_stop(long)
08-28 21:35:04.576 6950 6950 E ilicon.explore: instance fields (4 entries):
08-28 21:35:04.576 6950 6950 E ilicon.explore: 0: android.graphics.Bitmap com.softwinner.GifPlayer.a
08-28 21:35:04.576 6950 6950 E ilicon.explore: 1: long com.softwinner.GifPlayer.b
08-28 21:35:04.576 6950 6950 E ilicon.explore: 2: android.os.HandlerThread com.softwinner.GifPlayer.c
08-28 21:35:04.576 6950 6950 E ilicon.explore: 3: android.os.Handler com.softwinner.GifPlayer.d
08-28 21:35:04.576 6950 6950 E ilicon.explore: Failed to register native method com.softwinner.GifPlayer.native_load(JLjava/lang/String;)Z in /data/app/~~r-FMElIYiOMGNg22Rt8Mqg==/com.hisilicon.explorer-hzeXaFdGb6EVm8n37HswcA==/base.apk
08-28 21:35:04.576 6950 6950 D AndroidRuntime: Shutting down VM
08-28 21:35:04.578 6950 6950 E AndroidRuntime: FATAL EXCEPTION: main
08-28 21:35:04.578 6950 6950 E AndroidRuntime: Process: com.hisilicon.explorer, PID: 6950
08-28 21:35:04.578 6950 6950 E AndroidRuntime: java.lang.NoSuchMethodError: no static or non-static method "Lcom/softwinner/GifPlayer;.native_load(JLjava/lang/String;)Z"
08-28 21:35:04.578 6950 6950 E AndroidRuntime: at java.lang.Runtime.nativeLoad(Native Method)
08-28 21:35:04.578 6950 6950 E AndroidRuntime: at java.lang.Runtime.nativeLoad(Runtime.java:1131)
08-28 21:35:04.578 6950 6950 E AndroidRuntime: at java.lang.Runtime.loadLibrary0(Runtime.java:1085)
08-28 21:35:04.578 6950 6950 E AndroidRuntime: at java.lang.Runtime.loadLibrary0(Runtime.java:1008)
08-28 21:35:04.578 6950 6950 E AndroidRuntime: at java.lang.System.loadLibrary(System.java:1664)
08-28 21:35:04.578 6950 6950 E AndroidRuntime: at com.softwinner.GifPlayer.<clinit>(GifPlayer.java:2)
08-28 21:35:04.578 6950 6950 E AndroidRuntime: at com.hisilicon.explorer.ExplorerApplication.onCreate(ExplorerApplication.java:8)
08-28 21:35:04.578 6950 6950 E AndroidRuntime: at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1192)
08-28 21:35:04.578 6950 6950 E AndroidRuntime: at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6743)
08-28 21:35:04.578 6950 6950 E AndroidRuntime: at android.app.ActivityThread.access$1300(ActivityThread.java:237)
08-28 21:35:04.578 6950 6950 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1921)
08-28 21:35:04.578 6950 6950 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:106)
08-28 21:35:04.578 6950 6950 E AndroidRuntime: at android.os.Looper.loop(Looper.java:223)
08-28 21:35:04.578 6950 6950 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:7695)
08-28 21:35:04.578 6950 6950 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
08-28 21:35:04.578 6950 6950 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
08-28 21:35:04.578 6950 6950 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
提取关键报错:
java.lang.NoSuchMethodError: no static or non-static method "Lcom/softwinner/GifPlayer;.native_load(JLjava/lang/String;)Z"
意思就是so库加载进去了,但是GifPlayer 中声明的native_load方法找不到。。。。。。,奇怪了,看了一圈都没有任何问题,把人整麻了。
解决方法:
后面发现so库的Android.mk中有如下配置:
LOCAL_PROGUARD_ENABLED:= disabled
这个配置的意思就是关闭代码混淆,那么我的Android studio中的配置是不是也要把代码混淆关闭才能正常使用呢?试了一下,果然。build.gradle如下修改:把和代码混淆相关的功能都注释关闭。
buildTypes {release {
// minifyEnabled true //是否启动混淆 true:打开 false:关闭 混淆后无法联网 ,无法使用libgifplayer so库
// shrinkResources true//去掉无用资源
// proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'}}
这里我大概分析一下BUG产生的原因:java调so库是能调到的,但是so库返回结果的时候,由于APK使用了代码混淆,就不知道该返回给谁了。
后记:本文只针对Android 11 三方APP导入system/lib 目录下so库使用的情况,其它版本读者请细细斟酌一下,本文只做个参考。
这篇关于Android 11 ,Android Studio 三方APP调用系统 system/lib 目录下so库使用过程中的BUG(坑)。的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!