11

Is there a way to run a binary executable file (compile with NDK), on Android?

/* #includes #defines ... */

int main(){
    // Do something when this is executed
    return 0;
}

I want it to run independant from the VM. As in not inside an activity, just a binary that runs directly on the proc

user965369
  • 5,413
  • 13
  • 33
  • 47

2 Answers2

10
adb push exename /data/bin/exename
#next line might be needed if you are developing on Windows
adb shell chmod 777 /data/bin/exename
adb shell /data/bin/exename

But your device has to be rooted. (It also works on emulator.)

Andrey Kamaev
  • 29,582
  • 6
  • 94
  • 88
  • Ok so will this compile into an APK that can be installed directly or is the exe wrapped in a Davlik format just for initialization? – user965369 Oct 22 '11 at 18:29
  • No and no. Pure binary file is just a binary file. You can use `include $(BUILD_EXECUTABLE)` instead of `include $(BUILD_SHARED_LIBRARY)` in your `Android.mk` to build such executable with NDK. – Andrey Kamaev Oct 23 '11 at 13:28
  • is there any way to execute binary from java file(i.e. at run time). – CoDe Mar 07 '12 at 12:54
  • Does this still work on Jelly Bean? The question [Run native ARM executable on Jelly Bean](http://android.stackexchange.com/q/29615/4229) seems to imply this no longer works. – Mark Booth Sep 20 '12 at 08:35
3

I am answering to your doubt in the first answer mentioned by Andrey.

Try the codes given by him.

adb push exename /data/bin/exename

It is used to push the binary file named 'exename' to the execuatable path on Android.

adb shell chmod 777 /data/bin/exename

This line is not required in linux. Its used to change the mode. The first 7 stands for 'user', next for 'group' and the last for 'other'. Changing the numbers would CHange the MODe of each group mentioned above. 7 represents - read, write and execute. 6 represents - read, write and NO execute.

adb shell /data/bin/exename

This code is used to execute the binary. Which inturn means that its being used in the Terminal.

Anoop K. Prabhu
  • 5,417
  • 2
  • 26
  • 43
  • Thanks Anoop! Looks like it all works pretty smoothly via shell commands. Can you not execute in via interaction on the mobile? Is there any way you can build some sort of link to a shell script which will result in an execution of the binary (as in on the actual mobile - suposedly this has to be done via Java)? – user965369 Oct 25 '11 at 11:51
  • Oh, I am afraid I couldnot help you out. I didnot understand your question completely and to whatever I understood, I have no idea about it. Could you make it a bit more clear so that I too could try searching for it. – Anoop K. Prabhu Oct 26 '11 at 06:57
  • Sorry. What i mean is, yuo can execute a binary on Android via shell comands (adb shell chmod 777 is a command run on the kernel shell). Shell scripts are files that basically contain a load of shell commands. Is there any way I can execute a shell script on an Android device, instead of having to type the shell commands myself...? – user965369 Oct 26 '11 at 12:12
  • ok. I dont know whether there is a way to execute a shellscript in android device. There must be a way, will look into it. What I do normally to reduce typing all these commands is to create a text file with all the script commands in the pc and then run the file in terminal. It serves my purpose. Did that help? – Anoop K. Prabhu Oct 27 '11 at 09:23
  • Do mind to upvote the answers that you find yourselves useful. :) – Anoop K. Prabhu Oct 27 '11 at 11:57