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

相关文章

python中的flask_sqlalchemy的使用及示例详解

《python中的flask_sqlalchemy的使用及示例详解》文章主要介绍了在使用SQLAlchemy创建模型实例时,通过元类动态创建实例的方式,并说明了如何在实例化时执行__init__方法,... 目录@orm.reconstructorSQLAlchemy的回滚关联其他模型数据库基本操作将数据添

Spring配置扩展之JavaConfig的使用小结

《Spring配置扩展之JavaConfig的使用小结》JavaConfig是Spring框架中基于纯Java代码的配置方式,用于替代传统的XML配置,通过注解(如@Bean)定义Spring容器的组... 目录JavaConfig 的概念什么是JavaConfig?为什么使用 JavaConfig?Jav

Java使用Spire.Doc for Java实现Word自动化插入图片

《Java使用Spire.DocforJava实现Word自动化插入图片》在日常工作中,Word文档是不可或缺的工具,而图片作为信息传达的重要载体,其在文档中的插入与布局显得尤为关键,下面我们就来... 目录1. Spire.Doc for Java库介绍与安装2. 使用特定的环绕方式插入图片3. 在指定位

Springboot3 ResponseEntity 完全使用案例

《Springboot3ResponseEntity完全使用案例》ResponseEntity是SpringBoot中控制HTTP响应的核心工具——它能让你精准定义响应状态码、响应头、响应体,相比... 目录Spring Boot 3 ResponseEntity 完全使用教程前置准备1. 项目基础依赖(M

Java使用Spire.Barcode for Java实现条形码生成与识别

《Java使用Spire.BarcodeforJava实现条形码生成与识别》在现代商业和技术领域,条形码无处不在,本教程将引导您深入了解如何在您的Java项目中利用Spire.Barcodefor... 目录1. Spire.Barcode for Java 简介与环境配置2. 使用 Spire.Barco

Android使用java实现网络连通性检查详解

《Android使用java实现网络连通性检查详解》这篇文章主要为大家详细介绍了Android使用java实现网络连通性检查的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录NetCheck.Java(可直接拷贝)使用示例(Activity/Fragment 内)权限要求

C# 预处理指令(# 指令)的具体使用

《C#预处理指令(#指令)的具体使用》本文主要介绍了C#预处理指令(#指令)的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录1、预处理指令的本质2、条件编译指令2.1 #define 和 #undef2.2 #if, #el

C#中Trace.Assert的使用小结

《C#中Trace.Assert的使用小结》Trace.Assert是.NET中的运行时断言检查工具,用于验证代码中的关键条件,下面就来详细的介绍一下Trace.Assert的使用,具有一定的参考价值... 目录1、 什么是 Trace.Assert?1.1 最简单的比喻1.2 基本语法2、⚡ 工作原理3

C# IPAddress 和 IPEndPoint 类的使用小结

《C#IPAddress和IPEndPoint类的使用小结》本文主要介绍了C#IPAddress和IPEndPoint类的使用小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定... 目录一、核心作用网络编程基础类二、IPAddress 类详解三种初始化方式1. byte 数组初始化2. l

C语言逗号运算符和逗号表达式的使用小结

《C语言逗号运算符和逗号表达式的使用小结》本文详细介绍了C语言中的逗号运算符和逗号表达式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习... 在C语言中逗号“,”也是一种运算符,称为逗号运算符。 其功能是把两个表达式连接其一般形式为:表达