xxxx.so : text relocations 问题解决方法 (最新)

来源:互联网 发布:ntfs for mac 破解 编辑:程序博客网 时间:2024/04/29 11:04

以下是对别人答案的引用:

//////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

问题表现形式

错误或警告日志

当targetSdkVersion>=23且使用debug签名时,在6.0+的Android设备上运行App会输出以下错误Log:

E/linker: /data/app/packagename/lib/arm/libxxx.so: has text relocations 
W/System.err: java.lang.UnsatisfiedLinkError: dlopen failed: /data/app/packagename/lib/arm/libxxx.so: has text relocations

注意:当targetSdkVersion>=23,在低于6.0的设备上运行是正常的,至于原因,本文最后会谈到。

当targetSdkVersion<23且使用debug签名时,在6.0+的Android设备上虽然运行正常,但会输出以下警告Log:

W/linker: “/data/app/packagename/lib/arm/libxxx.so”: has W+E (writable and executable) load segments. This is a security risk shared libraries with W+E load segments will not be supported in a future Android release. Please fix the library.

W/linker: /data/app/packagename/lib/arm/libxxx.so has text relocations. This is wasting memory and prevents security hardening. Please fix.

App启动时弹出提醒对话框

当targetSdkVersion<23且使用debug签名的APK运行在高版本系统上(此处使用原生7.0测试),每次启动时会弹出一个对话框,其内容如下:

Detected problems with app native libraries (please consult log for detail) : libxxx.so: text relocations

上述各种表象可以发现是同一个问题所致,即libxxx.so: text relocations。

问题原因

“libxxx.so: text relocations”这个问题在Android 6.0官方的更新说明中有提及:

On previous versions of Android, if your app requested the system to load a shared library with text relocations, the system displayed a warning but still allowed the library to be loaded. Beginning in this release, the system rejects this library if your app’s target SDK version is 23 or higher. To help you detect if a library failed to load, your app should log the dlopen(3) failure, and include the problem description text that the dlerror(3) call returns. To learn more about handling text relocations, see this guide.

这个问题在6.0之前只会产生一个警告,系统还是可以正常加载包含text relocations的共享库的,但从6.0起,即SDK Version>=23时,系统将会拒绝加载包含text relocations的共享库,同时输出错误Log,也就是上文中看到的错误日志。

其实引起该问题的最根本原因,是so动态链接库的代码并非PIC(Position independent code),关于PIC的概念可以参考维基百科的介绍。在Android 6.0更新说明中,也只是简单提了一句处理方案:

To learn more about handling text relocations, see this guide.

该链接是一个解决text relocations (TEXTRELs)的指南。

\\\\\\\\\\\\\\\\\\\\\\\\\\///////////////////////////////////引用结束

  上面的引用内容中大部分的内容都是正确的,其中 红色加粗部分描述的不正确让我浪费了好长时间:

  按照上文红色加粗部分满足两个条件就不会显示错误提示

  1.targetSdkVersion < 23

  2.不使用debug 签名文件


  因为在我自己的项目里面debug 和release 都使用的同一个key 签名,所以我想当然的认为 在我自己的项目中不会出现这个错误弹窗。但是现实还是赤裸裸的打脸。

   最开始为了解决这个问题查了不少的资料,答案基本都一样。没有什么能解决我问题的。 后来自己就不断的尝试,修改targetSdkVersion 版本拉、更改so 引入方式、不直接run 而是打包;结果我发现 打release 包 没有问题 其他的包beta 、dev、pre、都不行 。最后我比对其他环境和release 环境的不同点 发现了原因。debuggable  

这个才是是否出现弹框的关键, 即使你使用debug 签名,只要关闭了debuggable 模式 都不会出现弹框;


      下面来说一下设置debuggable 开启 关闭的方法:

1. 在AndroidMainfest.xml 中设置


  <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme"        android:debuggable="false">


2. 在build.gradle 中设置

 buildTypes {        release {            minifyEnabled false            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'        }        debug {            debuggable false        }    }


debuggable false 就是关闭调试模式,在IDEA中不会显示该进程名字也不能调试;

在build.gradle 中默认release 中debuggable 是false 的 ,debug 中 debuggable 是true 的,如果要不用默认设置需要自己写一下。这样给测试打的包就没有弹窗了,我们发布项目时候 debuggable 一定要关闭 不管什么项目。

       以上是我的解决方案,tagerversion 如果>= 23如何解决,大家可以一起讨论下



原创粉丝点击