如何更多命令添加到Android的壳呢? [英] How to add more commands to Android Shell?

查看:176
本文介绍了如何更多命令添加到Android的壳呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候,我需要我的设备上运行一个命令或脚本,但他们不具备或不存在。

Sometimes I need to run a command or script on my device, but they are not available or don't exist.

我们可以添加一些额外的命令的Andr​​oid 设备上的
除了已经可以在上面这些命令?

Can we add some additional commands to Android device's shell, except those commands that are already available on it?

例如,添加 screenrecord 命令我的设备(我的设备具有的Andr​​oid 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?

推荐答案

答案提供了一系列步骤添加额外的可执行文件,的Andr​​oid 设备。作为构建工具的Andr​​oid NDK 是在两个的Eclipse Android的工作室的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.


  1. 添加原生支持。右键单击该项目在项目资源管理器> Android的工具> 添加本机支持> 完成

  1. Add native support. Right click on the project in Project Explorer > Android Tools > Add Native Support > Finish

添加源$ C ​​$ C ,即在 project_root 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.


    1. 使用普通的 *。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 the path_to_ndk/ndk-build script;
    • or calling the ndk-build script in build.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!
    


  • 这篇关于如何更多命令添加到Android的壳呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆