FrontPage

Demo

Here's demo application called "Run Native Exe" to:

Package: NativeExe-0.1.apk
Source code: NativeExe-0.1.zip (ADT project)

To install the package,

or type "adb install NativeExe?-*.apk" in your PC if you have Android SDK.

How can I run UNIX commands in Android App?

Here's sample code to run /system/bin/ls /sdcard in Android App:

try {
    // android.os.Exec is not included in android.jar so we need to use reflection.
    Class<?> execClass = Class.forName("android.os.Exec");
    Method createSubprocess = execClass.getMethod("createSubprocess",
            String.class, String.class, String.class, int[].class);
    Method waitFor = execClass.getMethod("waitFor", int.class);
    
    // Executes the command.
    // NOTE: createSubprocess() is asynchronous.
    int[] pid = new int[1];
    FileDescriptor fd = (FileDescriptor)createSubprocess.invoke(
            null, "/system/bin/ls", "/sdcard", null, pid);
    
    // Reads stdout.
    // NOTE: You can write to stdin of the command using new FileOutputStream(fd).
    FileInputStream in = new FileInputStream(fd);
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    String output = "";
    try {
        String line;
        while ((line = reader.readLine()) != null) {
            output += line + "\n";
        }
    } catch (IOException e) {
        // It seems IOException is thrown when it reaches EOF.
    }
    
    // Waits for the command to finish.
    waitFor.invoke(null, pid[0]);
    
    return output;
} catch (ClassNotFoundException e) {
    throw new RuntimeException(e.getMessage());
} catch (SecurityException e) {
    throw new RuntimeException(e.getMessage());
} catch (NoSuchMethodException e) {
    throw new RuntimeException(e.getMessage());
} catch (IllegalArgumentException e) {
    throw new RuntimeException(e.getMessage());
} catch (IllegalAccessException e) {
    throw new RuntimeException(e.getMessage());
} catch (InvocationTargetException e) {
    throw new RuntimeException(e.getMessage());
}

You can run UNIX commands using android.os.Exec, but it's not documented and not even in android.jar, so you cannot use it in usual way. But you can use it using reflection.

This code is based on source code of Android Terminal Emulator.

OK, but how can I put my own native executable in Android App?

First, you need to cross-compile your native executable for ARM. Here's a way (dynamic link version). Or you can use Scratchbox (Japanese). If you get a file with a format like this, it's probably OK:

% file yourapp
yourapp: ELF 32-bit LSB executable, ARM, version 1 (SYSV), for GNU/Linux 2.6.14, statically linked, not stripped

If you want to put the executable to YOUR phone (which you can connect with adb command in Android SDK), you can do it with e.g.

% adb shell
$ su
# mkdir /data/tmp
# chmod 777 /data/tmp
# exit
$ exit
% adb push yourapp /data/tmp
% adb shell
$ chmod 744 /data/tmp/yourapp
$ /data/tmp/yourapp

Note that you cannot make files executable in /sdcard.

If you want to put the executable to the phone of users who installed your application, here's a way (which I use in my demo application above):

Some notes

Comments



トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS