Run native executable in Android App

來源:互聯網
上載者:User

標籤:

Run native executable in Android AppDemo †

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

  • run local UNIX commands
  • run native executable downloaded from the Web

Package: NativeExe-0.2.apk
Source code: on Github (ADT project)

To install the package,

  • Go to Settings→Application and check "Unknown sources"
  • Open the package link above using Android Browser

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

↑How can I run UNIX commands in Android App? †

You can use Runtime.exec() in standard Java. Here‘s sample code to run /system/bin/ls /sdcard in Android App:

try {    // Executes the command.    Process process = Runtime.getRuntime().exec("/system/bin/ls /sdcard");        // Reads stdout.    // NOTE: You can write to stdin of the command using    //       process.getOutputStream().    BufferedReader reader = new BufferedReader(            new InputStreamReader(process.getInputStream()));    int read;    char[] buffer = new char[4096];    StringBuffer output = new StringBuffer();    while ((read = reader.read(buffer)) > 0) {        output.append(buffer, 0, read);    }    reader.close();        // Waits for the command to finish.    process.waitFor();        return output.toString();} catch (IOException e) {    throw new RuntimeException(e);} catch (InterruptedException e) {    throw new RuntimeException(e);}

This code is based on this article. Thanks yussi to let me know this by comment.

↑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 yourappyourapp: ELF 32-bit LSB executable, ARM, version 1 (SYSV), for GNU/Linux 2.6.14, statically linked, not stripped

You have three ways to put the binary to the phone:

  • From Android Java app, using assets folder (by fnerg in comment below)
    • Include the binary in the assets folder.
    • Use getAssets().open("YourBinaryHere") to get an InputStream.
    • Write it to /data/data/app-package-name (e.g. /data/data/net.gimite.nativeexe), where your application has access to write files and make it executable.
    • Run "/system/bin/chmod 744 /data/data/app-package-name/yourapp" using the code above.
    • Run your executable using the code above.
  • From Android Java app, downloading via HTTP (which I use in my demo application above)
    • Dowload the executable using HTTP and put it to /data/data/app-package-name (e.g. /data/data/net.gimite.nativeexe), where your application has access to write files and make it executable. You can use standard Java FileOutputStream to write files there.
    • Run "/system/bin/chmod 744 /data/data/app-package-name/yourapp" using the code above.
    • Run your executable using the code above.
  • By adb (needs SDK and root)
    • If you want to put the executable to YOUR phone connected with adb command in Android SDK and you have root, you can put the executable by:
% 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.

 

Run native executable in Android App

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.