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

2018-09-09 00:25:51

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

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


问题表现形式

错误或警告日志


当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 中设置


<applicationandroid: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 falseproguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'}debug {debuggable false}}



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

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

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


  • 2018-01-04 15:16:05

    201712月如何买到阿里云免费SSL证书(0元SSL证书)

    2017年12月,阿里云搞了个安全月活动,安全活动有折扣。 但是,免费SSL证书却在此时进行过调整,调整后的操作中存在缺陷,不容易找到免费SSL证书。目前尚不清除这是bug还是刻意为之,阿里云作为国内云计算厂商第一,想必不会刻意为之。晚些时候我会提交阿里云聆听平台,反馈此问题,帮助大家后方便从阿里云购买免费SSL证书。

  • 2018-01-04 16:37:25

    怎么让自己的网站支持https 访问 - SLL证书服务

    让自己的网站能够通过 https://你的域名 进行访问。 实现这个需要给你服务器安装安全证书。安全证书是有偿的,你要去申请,当然如果你的服务器在阿里云,你可以向阿里云免费申请SSL证书(每个帐号最多申请20个)。

  • 2018-01-08 22:12:20

    加快gradle的编译速度总结-亲身经历

    这两题集成mob的maven,这个编译速度是慢的可怜啊,经过百度得出了这样一个方案,配置好以后那可是神速啊,妈妈再也不用担心我的编译速度看。嗷嗷的。