Sometimes I need to run a command or script on my device, but they are not available or don't exist.
Can we add some additional commands to Android
device's shell
,
except those commands that are already available on it?
For example, add screenrecord
command to my device (my device has Android
API lower than 19), which this is unavailable on it.
I know how to get the list of available commands on the device with adb shell
adb shell ls /system/bin
but I want to add more custom commands and scripts, to do some special and work.
Is there any way to do it? Or it's impossible?
If in case you are getting this error:
error: only position independent executables (PIE) are supported when you created the executable using CMake,
add these lines to app gradle
The answer provides a sequence of steps for building an executable for
Android shell
through bothEclipse
andAndroid Studio
2.2+. The last includesndk-build
andCMake
.I. PREPARE SOURCE CODE
As an example consider
mycommand.c
:II. BUILD EXECUTABLE
Eclipse
In assumption that
NDK
location is set inEclipse
, create a newAndroid Application Project
and do the following steps.Add native support. Right click on the project in
Project Explorer
>Android Tools
>Add Native Support
>Finish
Add source code, i.e. put
mycommand.c
underproject_root
/jni
folder.Edit
Android.mk
underproject_root
/jni
as follows:Create
Application.mk
* under theproject_root
/jni
folder:Build executable and find it under
project_root/libs/<abi>/mycommand
.*Binaries for
all
supported CPU architectures are generated here. Useadb shell cat /proc/cpuinfo
to find out the CPU architecture and setAPP_ABI
as per Supported ABIs.Android Studio and ndk-build
The steps are as follows.
mycommand.c
,Android.mk
(same as in theEclipse
section above) to the/app/src/main/cpp
folder.Edit
build.gradle
:Build project and find the executable under
/app/.externalNativeBuild/ndkBuild/debug/obj/local/<abi>/mycommand
Android Studio and CMake
Add
mycommand.c
to the/app/src/main/cpp
folder and editCMakeLists.txt
:Edit
build.gradle
:Build project and find the executable under
/app/.externalNativeBuild/cmake/debug/<abi>/mycommand
III. PUSH BINARY INTO DEVICE
Push
mycommand
binary from where it is located into your device. Keep in mind that files on SD card aren't executable by default, so the binary should be pushed into the device's internal storage. Depending of whether device is rooted or not you have the following options:On non-rooted device you can push the binary to
/data/local/tmp
:On rooted device you can push the binary to SD card and then copy it to
/system/bin
(after remounting the partition in read-write mode) along with the other executable files:IV. SET EXECUTABLE PERMISSION (optional)
Set the permission of the binary to be executable (no need to do this in case of
/data/local/tmp
). Belowchmod 555
(r-xr-xr-x) is used:V. RUN COMMAND
Now you can shell into your device (with
adb shell
) and execute the command.On non-rooted device use the absolute path to the command:
On rooted device, in case the binary has been copied to
/system/bin
, you can call it by the file name: