首页 > 解决方案 > 在 Android 中使用 LD_PRELOAD

问题描述

我正在尝试使用 LD_PRELOAD 在应用程序启动期间动态加载 .so 文件。我使用 Android Studio 构建了本机库(创建一个本机项目,然后从 apk 中提取 .so 文件)。

我按照(https://cedricvb.be/post/intercepting-android-native-library-calls/)中的步骤配置 setprop 但是当我启动应用程序时,出现白屏并且似乎挂起但我的构造函数代码确实不被调用(打印一行到 logcat)。

我从( https://www.gamedev.net/forums/topic/213849-linux-gt-so-files-entry-point/)修改了简单的构造函数代码。

#include <jni.h>
#include <string>
#include <unistd.h>
#include <android/log.h>
#include <fcntl.h>
#include <dirent.h>
#include <dlfcn.h>

void simpleConstructor() __attribute__((constructor));
void simpleConstructor()
{
    __android_log_print(ANDROID_LOG_DEBUG,"constructor","constructor_code");
}

我这样配置了 LD_PRELOAD 变量

setprop wrap.com.x.y.z LD_PRELOAD=/data/local/tmp/libnative-lib.so

当我运行 getprop 时,价值就在那里。

我在 logcat 中看到的只是条目,例如

04-07 18:03:54.850  1659  6548 I ActivityManager: Force stopping com.x.y.z appid=10209 user=0: from pid 9076
04-07 18:03:54.953  1659  6548 I ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.x.y.z/.MainActivity} from uid 2000
04-07 18:03:54.991  9083  9083 I com.x.y.z: Late-enabling -Xcheck:jni
04-07 18:03:55.093  1659  1680 I ActivityManager: Start proc 9083:com.x.y.z/u0a209 for activity com.x.y.z/.MainActivity
04-07 18:04:05.099  1659  1678 W ActivityManager: Process ProcessRecord{b809543 9083:com.x.y.z/u0a209} failed to attach
04-07 18:04:05.101  1659  1678 I ActivityManager: Killing 9083:com.x.y.z/u0a209 (adj -10000): start timeout

感谢任何建议。

标签: androidc++

解决方案


推荐阅读