如何为Android Shell构建可执行文件 [英] How to build an executable for Android shell

查看:440
本文介绍了如何为Android Shell构建可执行文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时我需要在设备上运行命令或脚本,但是它们不可用或不存在.

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

我们可以在Android设备的shell中添加一些其他命令吗? 除了那些已经可用的命令以外?

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.

我知道如何使用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?

推荐答案

答案提供了一系列步骤,可通过EclipseAndroid Studio 2.2+为Android shell构建可执行文件.最后一个包括ndk-buildCMake.

The answer provides a sequence of steps for building an executable for Android shell through both Eclipse and Android Studio 2.2+. The last includes ndk-build and CMake.

例如,考虑mycommand.c:

#include <stdio.h>

int main()
{
    printf("My Command!\n");
    return 0;
}


II.可以执行

Eclipse

假设在Eclipse中设置了NDK位置,请创建一个新的Android Application Project并执行以下步骤.


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. 添加本机支持.右键单击Project Explorer> Android Tools> Add Native Support> Finish

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

添加源代码,即将mycommand.c放在project_root/jni文件夹下.

Add source code, i.e. put mycommand.c under project_root/jni folder.

编辑 Android.mk project_root/jni下,如下所示:

Edit Android.mk under project_root/jni as follows:

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
    

  • 构建可执行文件,并在 project_root/libs/<abi>/mycommand 下找到它.

  • Build executable and find it under project_root/libs/<abi>/mycommand.

    在此生成

    * all支持的CPU体系结构的二进制文件.使用adb shell cat /proc/cpuinfo找出CPU体系结构,并根据支持的ABI设置APP_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.

    步骤如下.

    1. mycommand.cAndroid.mk (与上面的Eclipse部分相同)添加到/app/src/main/cpp文件夹.
    2. 编辑build.gradle :

    1. Add mycommand.c, Android.mk (same as in the Eclipse section above) to the /app/src/main/cpp folder.
    2. Edit build.gradle:

    android {
        ...
        defaultConfig {
            ...
            externalNativeBuild {
                ndkBuild {
                    targets "mycommand"
                    // use a specific ABI filter if needed
                    // abiFilters "armeabi-v7a"
                }
            }
        }
        externalNativeBuild {
            ndkBuild {
                path "src/main/cpp/Android.mk"
            }
        }
    }
    

  • 构建项目,并在/app/.externalNativeBuild/ndkBuild/debug/obj/local/<abi>/mycommand

  • Build project and find the executable under /app/.externalNativeBuild/ndkBuild/debug/obj/local/<abi>/mycommand


    Android Studio和CMake


    Android Studio and CMake

    1. mycommand.c 添加到/app/src/main/cpp文件夹并编辑CMakeLists.txt :

    1. Add mycommand.c to the /app/src/main/cpp folder and edit CMakeLists.txt:

    cmake_minimum_required(VERSION x.x.x)
    
    add_executable(mycommand
                   src/main/cpp/mycommand.c )
    

  • 编辑build.gradle :

  • Edit build.gradle:

    android {
        ...
        defaultConfig {
            ...
            externalNativeBuild {
                cmake {
                    targets "mycommand"
                    // use a specific ABI filter if needed
                    // abiFilters "armeabi-v7a"
                }
            }
        }
        externalNativeBuild {
            cmake {
                path "CMakeLists.txt"
            }
        }
    }
    

  • 构建项目,并在/app/.externalNativeBuild/cmake/debug/<abi>/mycommand

  • Build project and find the executable under /app/.externalNativeBuild/cmake/debug/<abi>/mycommand


    III.将二进制推入设备

    mycommand二进制文件从其所在位置推送到设备中.请记住,默认情况下SD卡上的文件不可执行,因此应将二进制文件推送到设备的内部存储中.根据设备是否已植根,您可以使用以下选项:


    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:

    • 非根目录设备上,您可以将二进制文件推送到 /data/local/tmp :

    • On non-rooted device you can push the binary to /data/local/tmp:

    adb push mycommand /data/local/tmp
    

  • rooted 设备上,您可以将二进制文件推送到SD卡,然后将其与其他可执行文件一起复制到/system/bin(在以读写模式重新安装分区之后):

  • 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
    adb shell
    su
    mount -o rw,remount /system
    cp /path/to/sdcard/mycommand /system/bin
    

  • 将二进制文件的权限设置为可执行(/data/local/tmp 情况下无需执行此操作).在chmod 555(r-xr-xr-x)以下使用:

    Set the permission of the binary to be executable (no need to do this in case of /data/local/tmp). Below chmod 555(r-xr-xr-x) is used:

    adb shell chmod 555 /path/to/mycommand
    


    V.运行命令

    现在,您可以使用adb shell封装到设备中并执行命令.


    V. RUN COMMAND

    Now you can shell into your device (with adb shell) and execute the command.

    • 非root用户设备上,使用命令的绝对路径:

    • On non-rooted device use the absolute path to the command:

    $ /data/local/tmp/mycommand
    My Command!
    

  • rooted 设备上,如果二进制文件已复制到/system/bin,则可以通过文件名来调用它:

  • 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 Shell构建可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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