Using LD_PRELOAD in Android


I am trying to use LD_PRELOAD to dynamically load a .so file during application launch. I built the native library using Android Studio (creating a native project and then extracting the .so file from the apk).

I followed the steps in ( to configure setprop but when I launched the application, the white screen appears and seems to hang but my constructor code did not get called (print a line to logcat).

I modified the simple constructor code from (

#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()

I configured the LD_PRELOAD variable as such

setprop LD_PRELOAD=/data/local/tmp/

and the value is there when I ran getprop.

All I see in logcat are entries such as

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

Appreciate any advice.

asked on Stack Overflow Apr 7, 2020 by localacct • edited Apr 7, 2020 by localacct

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0