Using LD_PRELOAD in Android

0

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 (https://cedricvb.be/post/intercepting-android-native-library-calls/) 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 (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");
}

I configured the LD_PRELOAD variable as such

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

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.

android
c++
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