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

news2024/9/23 7:26:15

背景介绍:最近在做文件管理器,需要把图库的功能集成到文件管理器中,实现在文件管理器中打开图片、切换图片的功能。在实现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() {
                @Override
                public 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() {
                            @Override
                            public 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() {
            @Override
            public 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'
        }
    }

后记:本文只针对Android 11 三方APP导入system/lib 目录下so库使用的情况,其它版本读者请细细斟酌一下,本文只做个参考。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2084816.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

游戏发行技术体系之SDK技术体系

在上篇文章中提现过&#xff0c;SDK主要分为充值、登录、合规、数据、聚合和后台管理。 下面是针对SDK技术体系进行拆分 这个地方没有写账号&#xff0c;是因为我会账号独立出来&#xff0c;这块会在后面的账号技术体系单独规划。

爆改YOLOv8|利用分层特征融合策略MSBlock改进yolov8,暴力涨点

1&#xff0c;本文介绍 MSBlock 是一种分层特征融合策略&#xff0c;用于改进卷积神经网络中的特征融合效果。它通过分层次地融合不同尺度的特征图来提高网络的表达能力和性能。MSBlock 采用多尺度特征融合的方法&#xff0c;确保网络能够有效地捕捉不同层次和尺度的信息&…

FPGA实现HDMI传输(二)

之前的文章简单介绍了HDMI接口、TMDS编码以及ADV611工作原理和寄存器配置&#xff0c;本篇博客将给出具体的代码以及板级验证结果&#xff0c;代码参考自米联客的教程。 一.ADV7611配置 1.i2c驱动模块 timescale 1ns / 1psmodule uii2c# (parameter WMEN_LEN …

Git使用教程(小白也能看懂)

git的使用教程&#xff08;学习和工作中都能用到&#xff09; 1、所需工具 git bash 1&#xff09;安装git bash 官网&#xff1a;https://git-scm.com/download/win 2&#xff09;在文件夹空白处鼠标右键 若出现 上图说明安装成功 2、使用 1&#xff09;我们想要克隆一…

DHT11 实现温湿度传感器

DHT11 DHT11是一款含有已校准数字信号输出的温湿度复合传感器&#xff0c;它结合了电阻式感湿元件和一个NTC测温元件&#xff0c;并与一个高性能8位单片机相连接。通过采用专用的数字模块采集技术和温湿度传感技术&#xff0c;DHT11确保了产品的高可靠性和长期稳定性 DHT11时序…

android studio 新建java工程, 安卓新建项目,android studio2024 如何新建java项目

主要解决&#xff0c;新增安卓工程&#xff0c;没有java选项 1. 点击左上角FIle -> New -> 2. 选择 no activity 选项&#xff0c; 然后next 3. langua 就可以选择java 了。name自己定义项目名称&#xff0c;项目存储地址&#xff0c;包名。 配置完成选择finish. 4. fin…

【C++】vector(下)--上篇

个人主页~ vector&#xff08;上&#xff09;~ vector 二、vector的模拟实现1、了解组成2、vector.h&#xff08;1&#xff09;为什么有了size_t参数的vector构造函数还要再写一个int参数的重载vector构造函数&#xff08;2&#xff09;为什么reserve不用memcpy&#xff08;3&…

Mysql剖析(三)----MySql的事务详解

事务&#xff08;Transaction&#xff09;&#xff1a;一般是指要做的或所做的事情。在计算机术语中是指访问并可能更新数据库中的各种数据项的一个程序执行单元&#xff08;unit&#xff09;。事务通常由高级数据库操纵语言或编程语言(如SQL、C或Java)书写的用户程序的执行所引…

Python脚本实现向飞书发送卡片消息

目录 1 先创建一个卡片消息2 Python脚本2.1 告警测试2.2 告警恢复 总结 1 先创建一个卡片消息 飞书卡片搭建工具 根据自己的需要创建一个消息卡片&#xff1a; 可以在 卡片源代码 中看到这个卡片的代码信息 2 Python脚本 2.1 告警测试 test.py 文件 ""&quo…

MacOS安装Docker Desktop

目录 安装Docker Desktop 1.下载 2.安装 3.验证 安装Docker Desktop 1.下载 访问Docker官方网站下载页面&#xff1a;https://www.docker.com/products/docker-desktop 选择自己所需的版本下载即可 2.安装 3.验证 1.查询版本号 docker --version 2.也可以运行一个简单的…

【vue、Electron】搭建一个Electron vue项目过程、将前端页面打包成exe 桌面应用

文章目录 前言使用 electron-vue 创建项目1. 安装 vue-cli&#xff08;如果未安装&#xff09;2. 使用 electron-vue 模板创建项目3. 安装和配置 electron-builder4. 运行Electron项目5. 打包应用 可能遇到的问题解决Electron vue首次启动巨慢无法加载执行npm run electron:bui…

从地图信息实时检测路口的各向通行状况、红绿灯及溢出情况

高德地图、百度地图都能获得实时的道路信息。 C# 编写的路况信息爬虫可获得准实时&#xff08;1分钟间隔&#xff09;的路口的各向通行状况、红绿灯及溢出情况。 优势&#xff1a; 投入少&#xff0c;效果好&#xff0c;无需安装设备&#xff1b; 缺陷&#xff1a; 时间间隔…

数据集不够用?3DGS助力生成带标注的全新扩展数据!

作者主页&#xff1a;https://louiszengcn.github.io/ 论文标题&#xff1a; Realistic Surgical Image Dataset Generation Based On 3D Gaussian Splatting 导读&#xff1a; 近年来&#xff0c;随着AI技术的飞速发展&#xff0c;数据量的增加为数据驱动的神经网络提供了更强…

汽车免拆诊断案例 | 2012 款大众速腾车发动机偶尔抖动

故障现象 一辆2012款大众速腾车&#xff0c;搭载CST发动机和干式双离合变速器&#xff0c;累计行驶里程约为17万km。车主反映&#xff0c;发动机偶尔抖动。 故障诊断 接车后试车&#xff0c;确认发动机怠速偶尔抖动&#xff0c;且在D挡起步时抖动明显。用故障检测仪检测&…

专题◉万字长文!盘点过去一年最出圈的Prompt项目教程,有3份在悄悄更新

1. OpenAI 官方出品 | 提示工程最权威的教程 (最新版) 2023年6月&#xff0c;OpenAI 发布了一篇〖*GPT Best Practice (GPT 最佳实践)* 〗教程&#xff0c;详细介绍 ChatGPT Prompt 交互策略&技巧&#xff0c;并且给出了示例说明。 一年时间过去了&#xff0c;OpenAI 不…

Unity射击游戏开发教程:(33)控制飞机左右移动

在本文中,我将介绍如何在玩家朝该方向移动时添加向左和向右转动的动画。 我们已经将左转和右转动画导入到我们的项目中。在我使用的动画中,它们只有两帧,因为我们希望飞机倾斜得很快,这样玩家在按下相应按钮时就能直接感觉到转弯。 </

为什么帕金森病患者在紧张或焦虑状态下震颤会加重?

为什么帕金森病患者在紧张或焦虑状态下震颤会加重&#xff1f; 帕金森病患者在紧张或焦虑状态下震颤加重的原因可能与多个因素有关。首先&#xff0c;帕金森病与大脑中多巴胺水平的降低有关&#xff0c;这种神经递质的失衡会影响运动控制。当患者感到紧张时&#xff0c;大脑皮…

嵌入式软件开发之状态机与事件驱动分析

状态机是一种程序架构,事件驱动也是一种程序架构,在某些开发场景里面,两者是可以一同合并使用的。 事件驱动的概念 生活中有很多事件驱动的例子,上自习瞒着老师偷睡觉就是很生动的一个。 我们都是从高中时代走过来的,高中的学生苦啊,觉得睡觉是世界上最奢侈的东西, 有时…

运维监控工具 PIGOSS BSM :PostgreSQL数据库监控指标

在PostgreSQL数据库中&#xff0c;为了确保其稳定运行和性能优化&#xff0c;我们需要监控一系列关键的指标。以下是一些主要的PostgreSQL监控指标介绍&#xff1a; 连接数&#xff08;Connections&#xff09;&#xff1a; 定义&#xff1a;连接数是指当前正在与数据库建立连…

数据库管理-第236期 数据库一体机的价值(20240829)

数据库管理236期 2024-08-29 数据库管理-第236期 数据库一体机的价值&#xff08;20240829&#xff09;1 误区2 硬件3 操作系统4 数据库总结 数据库管理-第236期 数据库一体机的价值&#xff08;20240829&#xff09; 作者&#xff1a;胖头鱼的鱼缸&#xff08;尹海文&#xff…