Android 11 ,Android Studio 三方APP调用系统 system/lib 目录下so库使用过程中的BUG(坑)。

2024-08-30 08:36

本文主要是介绍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(坑)。的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1120322

相关文章

一文详解如何使用Java获取PDF页面信息

《一文详解如何使用Java获取PDF页面信息》了解PDF页面属性是我们在处理文档、内容提取、打印设置或页面重组等任务时不可或缺的一环,下面我们就来看看如何使用Java语言获取这些信息吧... 目录引言一、安装和引入PDF处理库引入依赖二、获取 PDF 页数三、获取页面尺寸(宽高)四、获取页面旋转角度五、判断

C++中assign函数的使用

《C++中assign函数的使用》在C++标准模板库中,std::list等容器都提供了assign成员函数,它比操作符更灵活,支持多种初始化方式,下面就来介绍一下assign的用法,具有一定的参考价... 目录​1.assign的基本功能​​语法​2. 具体用法示例​​​(1) 填充n个相同值​​(2)

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

Linux进程CPU绑定优化与实践过程

《Linux进程CPU绑定优化与实践过程》Linux支持进程绑定至特定CPU核心,通过sched_setaffinity系统调用和taskset工具实现,优化缓存效率与上下文切换,提升多核计算性能,适... 目录1. 多核处理器及并行计算概念1.1 多核处理器架构概述1.2 并行计算的含义及重要性1.3 并

Spring boot整合dubbo+zookeeper的详细过程

《Springboot整合dubbo+zookeeper的详细过程》本文讲解SpringBoot整合Dubbo与Zookeeper实现API、Provider、Consumer模式,包含依赖配置、... 目录Spring boot整合dubbo+zookeeper1.创建父工程2.父工程引入依赖3.创建ap

Linux下进程的CPU配置与线程绑定过程

《Linux下进程的CPU配置与线程绑定过程》本文介绍Linux系统中基于进程和线程的CPU配置方法,通过taskset命令和pthread库调整亲和力,将进程/线程绑定到特定CPU核心以优化资源分配... 目录1 基于进程的CPU配置1.1 对CPU亲和力的配置1.2 绑定进程到指定CPU核上运行2 基于

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

golang程序打包成脚本部署到Linux系统方式

《golang程序打包成脚本部署到Linux系统方式》Golang程序通过本地编译(设置GOOS为linux生成无后缀二进制文件),上传至Linux服务器后赋权执行,使用nohup命令实现后台运行,完... 目录本地编译golang程序上传Golang二进制文件到linux服务器总结本地编译Golang程序

深入理解Go语言中二维切片的使用

《深入理解Go语言中二维切片的使用》本文深入讲解了Go语言中二维切片的概念与应用,用于表示矩阵、表格等二维数据结构,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录引言二维切片的基本概念定义创建二维切片二维切片的操作访问元素修改元素遍历二维切片二维切片的动态调整追加行动态

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录