Compiling C for Android
Compile and run C programs on Android to debug pieces of code
Android runs on Unix and is very much capable of running programs compiled with C. The only catch is that it uses some specific architectures which means specific compilers should be used to generate the binary. This can be very useful in viewing the output of a C library like compiled JNI functions for example.
Imagine we reverse-engineered a JNI function that uses a seeded random or the filesystem, which we can't easily replicate on a local Linux machine. We could copy Ghidra's decompiled code into a simple C program that prints the output like this one:
In the original code, this was a variable, but I've added the printf
to extract this value and read it instead. Running this locally might give different results than if it actually ran on the mobile device, so we need to compile it with a specific Android compiler for the device to understand.
Some devices don't use ARM and should use regular x86_64
instead. In this case, simply use the x86_64-linux-androidXX-clang
compiler instead
Use these binaries like you would any other gcc
compiler, for example:
Then you can copy this binary over to the device using ADB, and run it:
Last updated