Nederealm
Nederealm

Reputation: 447

Running Android NDK binary on Linux desktop

I downloaded an APK from Play Store that contains native code binaries. In the APK file there is an lib/x86 folder that supposedly contains a library file containing native procedures, normally a .so extension. Since the code is in x86, is it possible to write a Java program to invoke the library on the desktop? Even if you dont have the source code for that library. The NDK function just has to accept parameters and return a value. For example, can we write

class AppNativeLoader
{
    public static native void generateRand(int seed);

    static
    {
        System.loadLibrary( "AndroidNDKLib" );
    }
}

public class WCallTest
{
    public static void main( String[ ] args )
    {
        long seed = System.currentTimeMillis();

        if(args.length > 0) {
            seed = Long.valueOf(args[0]);
        }

        long rand = AppNativeLoader.generateRand(seed);

        System.out.println(rand);
    }
}

NOTE: This is just an example. The actual environment differs. Using JRE 7 on RHEL, I extracted the x86 .so and placed it in the same directory as the .class file. I still get an UnSatisfiedLinkerError. Anything amiss? Assuming there are no callbacks and the function doesn't utilize and Android APIs, is this possible?

EDIT: I opened the lib in IDA Pro and I saw the following dependencies

.plt:0000B100 ; Needed Library 'liblog.so'
.plt:0000B100 ; Needed Library 'libz.so'
.plt:0000B100 ; Needed Library 'libc.so'
.plt:0000B100 ; Needed Library 'libm.so'
.plt:0000B100 ; Needed Library 'libstdc++.so'
.plt:0000B100 ; Needed Library 'libdl.so'

These should be available in my desktop environment, no?

Upvotes: 0

Views: 2011

Answers (2)

Danogentili
Danogentili

Reputation: 759

You can try downloading the needed shared libraries from here (make sure to choose the correct API version, and an architecture matching the architecture of the NDK shared library, to find out which shared libraries you need you can simply use ldd).

Then, to easily access the methods exposed by the shared lib, you can decompile the java code of the app using jadx, and then write your own code around the JNI classes.

Then, to compile your java code, you can use any version of the JDK.

Then, to execute it, you'll have to use a version of JRE matching the architecture of the NDK shared library (in your case, you'll have to download the 32-bit JRE).

However, this is not guaranteed to work: I am currently getting segfaults in the NDK shared library I'm trying to use on my PC, and since most NDK binaries are stripped, debugging is going to be a nightmare.

Upvotes: 2

Dan Albert
Dan Albert

Reputation: 10499

Not all Linux environments are identical (even crossing distribution boundaries is not guaranteed to work). NDK binaries are built against Bionic and a handful of other Android specific libraries, whereas your RedHat system uses glibc and a bunch of other things available from the RedHat repositories.

tl;dr you can't run Android binaries on desktop Linux.

Upvotes: 3

Related Questions