27

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?

3

2 Answers 2

23

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

4
  • Thx for your reply. In fact the problem was on the binary generation. It was not being generated correctely! ;) Commented May 27, 2013 at 9: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
    Commented Nov 16, 2015 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
    Commented Mar 8, 2018 at 15:59
  • /data/local requires root access on my device. Use /data/local/tmp instead
    – Chan Tzish
    Commented Feb 4, 2020 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .