25

I need a specific command line tool and I have made a C program in my Linux shell. I have compiled the program with an ARM cross-compiler. I have then moved the program into the Android file system and tried to run it.

The output is permission denied.

What do I have to do, in order to run my own compiled programs in Android file system?

Andrew T.
  • 15,988
  • 10
  • 74
  • 123
Nuno Santos
  • 351
  • 1
  • 3
  • 4

2 Answers2

22

I assume that you used adb push for uploading your executable to the sd-card. Unfortunately the sd-card is always mounted with "noexec" which means that you can't execute anything from here.

Therefore you have to copy the executable to the local filesystem, e.g. to /data/local. In case the device is not rooted or you don't have BusyBox installed there will be no "cp" command. You can simply use cat: cat /sdcard/myprog > /data/local/myprog.

Then you have to set the executable permission on the executable. Chmod on android usually does not support the "u+x" syntax. Therefore you have to call chmod 555 /data/local/myprog.

Afterwards you can execute your executable: /data/local/myprog.

Alternatively the directory /data/local/tmp can be used. Via adb shell you have full access in this directory. On modern devices (Android 11+) apps can't list files from this directory, but they are still able to execute executables from there if you provide the full path of the executable.

Update: On Android 10+, apps that has a targetSDK of 29 or higher can no longer execute anything that is located in their app private directory: https://developer.android.com/about/versions/10/behavior-changes-10#execute-permission

Robert
  • 20,025
  • 6
  • 47
  • 66
  • Thx for your reply. In fact the problem was on the binary generation. It was not being generated correctely! ;) – Nuno Santos May 27 '13 at 09:43
  • I have busybox on my device, but this command: shell@n200C:/ $ busybox cp /storage/external_storage/sda1/ffmpeg /data/ ------ show error= cp: can't create '/data/ffmpeg': Permission denied – Dr.jacky Nov 16 '15 at 13:58
  • tmp-mksh: /data/local/workspace/clang+llvm-5.0.0-aarch64-linux-gnu/bin/clang++: No such file or directory though the file is present – Necktwi Mar 08 '18 at 15:59
  • /data/local requires root access on my device. Use /data/local/tmp instead – Chan Tzish Feb 04 '20 at 13:26
18

First, you have to push it into a directory, such as /data/local/tmp. Then, you have to set permission for that using chmod 755 executable. After that, you can run it as ./executable.

Complete steps are as follows:

adb push executable /data/local/tmp
adb shell
cd /data/local/tmp
chmod 755 executable
./executable

Alternatively, if you want to run it from your asset folder, you have to copy the file to your data folder /data/data/packagename/. Then using File class, set the setExecutable flag to true for the file and run it by the Process class or third party packages like Root Tools .

UPDATE
if you are targeting sdk 29 or higher you CANNOT use the binary from your asset folder ! you have to copy your binary to jnilib folder then run it from native library directory ! context.getApplicationInfo().nativeLibraryDir

Koorosh
  • 291
  • 2
  • 3