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

查看:28
本文介绍了如何为 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 命令添加到我的设备(我的设备具有低于 19 的 Android API),这是不可用的.

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 shell 构建可执行文件的步骤>(过时)和 Android Studio(撰写本文时 4.1+).最后包括 ndk-buildCMake.

The answer provides a sequence of steps for building an executable for Android shell through both Eclipse (outdated) and Android Studio (4.1+ by the time of this writing). The last includes ndk-build and CMake.

mycommand.c 为例:

#include <stdio.h>

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


二.构建可执行

Eclipse(可能已经过时)

假设在Eclipse中设置了NDK位置,创建一个新的Android应用项目并执行以下步骤.


II. BUILD EXECUTABLE

Eclipse (might be outdated)

In assumption that NDK location is set in Eclipse, create a new Android Application Project and do the following steps.

  1. 添加原生支持.在 Project Explorer 中右键单击项目 >Android 工具 >添加原生支持 >完成

  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 下如下:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE     := mycommand
LOCAL_SRC_FILES  := mycommand.c

include $(BUILD_EXECUTABLE)

  • 创建Application.mk * project_root/jni 文件夹下:

    APP_ABI := all
    

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

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

    *所有 支持的 CPU 架构的二进制文件在此处生成.使用adb shell cat/proc/cpuinfo找出CPU架构并按照支持的 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 文件夹.

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

    编辑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"
            }
        }
    }
    

  • Build project并在/app/.externalNativeBuild/ndkBuild/debug/obj/local//mycommand


  • Android Studio 和 CMake

    1. 使用 Native C++ 模板创建项目.

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

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

    cmake_minimum_required(VERSION x.x.x)
    
    add_executable(mycommand
                   mycommand.c )
    

  • 编辑build.gradle:

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

  • Build project并在/app/build/intermediates/cmake/debug/obj//mycommand


    三.将二进制推入设备

    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:

    • 非root用户设备上,您可以将二进制文件推送到/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 (this might not be needed 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)并执行命令.

    • non-rooted 设备上使用命令的绝对路径:

    • 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天全站免登陆