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

相关文章

C/C++ chrono简单使用场景示例详解

《C/C++chrono简单使用场景示例详解》:本文主要介绍C/C++chrono简单使用场景示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友... 目录chrono使用场景举例1 输出格式化字符串chrono使用场景China编程举例1 输出格式化字符串示

pytest+allure环境搭建+自动化实践过程

《pytest+allure环境搭建+自动化实践过程》:本文主要介绍pytest+allure环境搭建+自动化实践过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、pytest下载安装1.1、安装pytest1.2、检测是否安装成功二、allure下载安装2.

Python验证码识别方式(使用pytesseract库)

《Python验证码识别方式(使用pytesseract库)》:本文主要介绍Python验证码识别方式(使用pytesseract库),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全... 目录1、安装Tesseract-OCR2、在python中使用3、本地图片识别4、结合playwrigh

Python使用Code2flow将代码转化为流程图的操作教程

《Python使用Code2flow将代码转化为流程图的操作教程》Code2flow是一款开源工具,能够将代码自动转换为流程图,该工具对于代码审查、调试和理解大型代码库非常有用,在这篇博客中,我们将深... 目录引言1nVflRA、为什么选择 Code2flow?2、安装 Code2flow3、基本功能演示

使用vscode搭建pywebview集成vue项目实践

《使用vscode搭建pywebview集成vue项目实践》:本文主要介绍使用vscode搭建pywebview集成vue项目实践,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录环境准备项目源码下载项目说明调试与生成可执行文件核心代码说明总结本节我们使用pythonpywebv

Pytorch介绍与安装过程

《Pytorch介绍与安装过程》PyTorch因其直观的设计、卓越的灵活性以及强大的动态计算图功能,迅速在学术界和工业界获得了广泛认可,成为当前深度学习研究和开发的主流工具之一,本文给大家介绍Pyto... 目录1、Pytorch介绍1.1、核心理念1.2、核心组件与功能1.3、适用场景与优势总结1.4、优

C++类和对象之默认成员函数的使用解读

《C++类和对象之默认成员函数的使用解读》:本文主要介绍C++类和对象之默认成员函数的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、默认成员函数有哪些二、各默认成员函数详解默认构造函数析构函数拷贝构造函数拷贝赋值运算符三、默认成员函数的注意事项总结一

使用Python和Matplotlib实现可视化字体轮廓(从路径数据到矢量图形)

《使用Python和Matplotlib实现可视化字体轮廓(从路径数据到矢量图形)》字体设计和矢量图形处理是编程中一个有趣且实用的领域,通过Python的matplotlib库,我们可以轻松将字体轮廓... 目录背景知识字体轮廓的表示实现步骤1. 安装依赖库2. 准备数据3. 解析路径指令4. 绘制图形关键

Windows 系统下 Nginx 的配置步骤详解

《Windows系统下Nginx的配置步骤详解》Nginx是一款功能强大的软件,在互联网领域有广泛应用,简单来说,它就像一个聪明的交通指挥员,能让网站运行得更高效、更稳定,:本文主要介绍W... 目录一、为什么要用 Nginx二、Windows 系统下 Nginx 的配置步骤1. 下载 Nginx2. 解压

详解如何使用Python从零开始构建文本统计模型

《详解如何使用Python从零开始构建文本统计模型》在自然语言处理领域,词汇表构建是文本预处理的关键环节,本文通过Python代码实践,演示如何从原始文本中提取多尺度特征,并通过动态调整机制构建更精确... 目录一、项目背景与核心思想二、核心代码解析1. 数据加载与预处理2. 多尺度字符统计3. 统计结果可