如何更多命令添加到Android的壳呢?
有时候,我需要我的设备上运行一个命令或脚本,但他们不具备或不存在。
Sometimes I need to run a command or script on my device, but they are not available or don't exist.
我们可以添加一些额外的命令的Android
设备上的壳
,
除了已经可以在上面这些命令?
Can we add some additional commands to Android
device's shell
,
except those commands that are already available on it?
例如,添加 screenrecord
命令我的设备(我的设备具有的Android
API超过19下),这这是它不可用。
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?
的答案提供了一系列步骤添加额外的可执行文件,的Android
设备。作为构建工具的Android NDK
是在两个的Eclipse
和 Android的工作室$ C $使用C>的IDE。
的
The answer provides a sequence of steps for adding an extra executable file to Android
device. As building tool Android NDK
is used within both Eclipse
and Android Studio
IDEs.
例如,考虑 mycommand.c
:
#include <stdio.h>
int main()
{
printf("My Command!\n");
return 0;
}
二。生成可执行
的Eclipse
在假设 NDK
位置在 Eclipse中设置
,创建一个新的 Android应用程序项目
,然后执行以下步骤。
II. BUILD EXECUTABLE
Eclipse
In assumption that NDK
location is set in Eclipse
, create a new Android Application Project
and do the following steps.
-
添加原生支持。右键单击该项目在
项目资源管理器
>Android的工具
>添加本机支持
>完成
Add native support. Right click on the project in
Project Explorer
>Android Tools
>Add Native Support
>Finish
添加源$ C $ C ,即在 project_root $ c将
mycommand.c
$ C> / JNI
文件夹中。
Add source code, i.e. put mycommand.c
under project_root
/jni
folder.
修改 Android.mk
在 project_root
/ JNI
如下:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := mycommand
LOCAL_SRC_FILES := mycommand.c
include $(BUILD_EXECUTABLE)
Application.mk
*下的 project_root
/ JNI
文件夹:
Create Application.mk
* under the project_root
/jni
folder:
APP_ABI := all
构建可执行。 项目
> 生成项目
。
*的为所有二进制文件
支持CPU架构都在这里产生。使用亚行的shell执行cat / proc内/ cpuinfo
来找出CPU的架构和设置 APP_ABI
按支持的ABI 。的
*Binaries for all
supported CPU architectures are generated here. Use adb shell cat /proc/cpuinfo
to find out the CPU architecture and set APP_ABI
as per Supported ABIs.
下面给出的建筑物的可执行的实施例是基于 HelloJNI
样本。我建议使用它以在摇篮
构建系统( 摇篮2.8
的preserve一致性)/插件版本( gradle这个实验性:0.4.0
)的
The example of building executable shown below is based on HelloJNI
sample. I recommend to use it in order to preserve consistency of the Gradle
build system (Gradle 2.8
) / plugin version (gradle-experimental:0.4.0
).
据我所知,通过这个写作 android.ndk
模块的时间的build.gradle
不具备选项建立可执行文件呢。在这种情况下,操作步骤如下。
AFAIK, by the time of this writing android.ndk
module of build.gradle
doesn't have the option to build executables yet. In this case the steps are as follows.
-
使用普通的
*。MK
文件按源文件的位置设置为不存在的文件夹和jniLibs
位置库
文件夹的build.gradle
:
android.sources{
main {
jni {
source { srcDir "src/main/none" }
}
jniLibs {
source { srcDir "src/main/libs" }
}
}
}
添加 mycommand.c
, Android.mk
, Application.mk
(等同于的Eclipse上面
部分)下的 project_app_root
/ 的src
/ 主
/ JNI
文件夹中。
Add mycommand.c
, Android.mk
, Application.mk
(same as in Eclipse
section above) under the project_app_root
/src
/main
/jni
folder.
构建可执行或者
- 手动命令行从
project_app_root
/的src
/执行主要
目录的path_to_ndk / NDK的构建
脚本; -
或调用
的build.gradle
的NDK的构建
脚本**(构建
>使项目
)
- manually in command line by executing from
project_app_root
/src
/main
directory thepath_to_ndk/ndk-build
script; or calling the
ndk-build
script inbuild.gradle
** (Build
>Make Project
):
import org.apache.tools.ant.taskdefs.condition.Os
model {
...
}
task ndkBuild(type: Exec) {
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
commandLine 'ndk-build.cmd', '-C', file('src/main').absolutePath
} else {
commandLine 'ndk-build', '-C', file('src/main').absolutePath
}
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
**的如果您在错误:执行失败的任务:应用程序:ndkBuild。开始出现一个问题进程的命令'NDK的构建(.CMD)''
,尽量完整路径设置为 NDK的构建(.CMD)
命令。的
**If you run into Error:Execution failed for task ':app:ndkBuild'. A problem occurred starting process 'command 'ndk-build(.cmd)''
, try setting the full path to ndk-build(.cmd)
command.
按 mycommand
二进制project_root /库/&LT; ABI&GT; (的Eclipse
)或 project_app_root / src目录/主/库/&LT; ABI&GT;
( Android的工作室
)到您的设备。请记住,SD卡上的文件不是可执行的,所以应该二进制推入设备的内部存储。根据的设备是否是植根与否您有以下选择:
Push mycommand
binary from project_root/libs/<abi>
(Eclipse
) or project_app_root/src/main/libs/<abi>
(Android Studio
) into your device. Keep in mind that files on SD card aren't executable, 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:
-
在无根设备,你可以把二进制为
/数据/本地/ tmp目录
On non-rooted device you can push the binary to
/data/local/tmp
:
adb push mycommand /data/local/tmp
在扎根设备,你可以把二进制到SD卡上,然后将其复制到 /系统/斌
(重新安装后在读写模式下的分区)与其他可执行文件一起:
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:
adb push mycommand /path/to/sdcard
su
mount -o rw,remount /system
cp /path/to/sdcard/mycommand /system/bin
设置的二进制文件的权限是可执行的。在搭配chmod 555
(R-XR-XR-X)时:
Set the permission of the binary to be executable. Below chmod 555
(r-xr-xr-x) is used:
adb shell chmod 555 /data/local/tmp/mycommand
诉运行命令
现在你可以将外壳设备(与亚行外壳
),并执行命令。
-
在无根设备使用绝对路径命令:
On non-rooted device use the absolute path to the command:
$ /data/local/mycommand
My Command!
在扎根设备,以防二进制文件已被复制到 /系统/斌
,您可以通过该文件调用它名称:
On rooted device, in case the binary has been copied to /system/bin
, you can call it by the file name:
$ mycommand
My Command!