Search code examples
javaandroidandroid-ndkjava-native-interfacesmartphone

Testing Android NDK from command line terminal on Smartphone


I have a C library that I am trying to utilise on an Android smartphone

I have had some initial success wrapping the libraries API using SWIG such that I can call the native C functions from Java (SWIG is auto-generating all the C and Java JNI code for me).

So I am in a position where I can write a Java application on my x86 laptop and have it call a native shared library.

My next step is to use the Android NDK to make a native shared library for my target smartphone platform (proabably ARM)

Ultimately I will be developing an Android GUI to interface with the library.

But as an interim measure, is it possible to somehow run a Java application from a terminal on a smartphone?

This would allow me to use the same Java 'harness' application I have used on my laptop on the smartphone to see if it works before taking the next step of writing an Android GUI.

UPDATE

I forgot that although android uses the java language it doesn't use java bytecode - its dalvik bundled up in an .apk so my thought of being able to open a terminal and run a java harness, e.g.

$ java harness

doesn't make any sense

I did find that there is a Android Terminal Emulator (Jack Palevich) availble form the google app store but not sure how you could get that on to an avd emualtor (I don't currently have a smartphone)


Solution

  • I think you can test android.os.Exec.createSubprocess() api. demo code:

    Class<?> execClass = Class.forName("android.os.Exec");
        Method createSubprocess = execClass.getMethod("createSubprocess", String.class, String.class, String.class, int[].class);
        int[] pid = new int[1];
        FileDescriptor fd = (FileDescriptor)createSubprocess.invoke(null, "/system/bin/yourexecfile", "/", null, pid);
    
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fd)));
        String output = "";
        try {
            String line;
            while ((line = reader.readLine()) != null) {
                output += line + "\n";
            }
        }
        catch (IOException e) {}
    

    The path "/system/bin" can be used.But I'm not sure about other path,but u can test. Good luck! ^-^