简体   繁体   English

想编译原生安卓二进制我可以在手机终端运行

[英]Want to compile native Android binary I can run in terminal on the phone

I've been trying for a couple days to compile a native ARM Android binary that will execute on my phone using a terminal application.几天来,我一直在尝试编译一个本机 ARM Android 二进制文件,该二进制文件将使用终端应用程序在我的手机上执行。 I want to generate the same type of binary as the standard Posix binaries installed on the phone like ls, mkdir etc. I've downloaded the Android NDK under Mac OS X and have been able to compile simple ELF binaries without errors.我想生成与安装在手机上的标准 Posix 二进制文件(如 ls、mkdir 等)相同类型的二进制文件。我已经在 Mac OS X 下下载了 Android NDK,并且能够编译简单的 ELF 二进制文件而不会出错。 However, when I transfer them to the phone, they always segfault.但是,当我将它们转移到手机时,它们总是出现段错误。 That is, they segfault when compiled with -static in GCC.也就是说,在 GCC 中使用 -static 编译时会出现段错误。 If I don't use -static, they complain about not being linked, etc. Put simply, they don't work.如果我不使用 -static,他们会抱怨没有被链接,等等。简单地说,他们不工作。

My hypothesis is that they are not linking to the Android standard C library properly.我的假设是它们没有正确链接到 Android 标准 C 库。 Even though I am linking my binaries with the libc provided by the NDK, they still don't work.即使我将我的二进制文件与 NDK 提供的 libc 链接起来,它们仍然不起作用。 I read that Android uses the Bionic C library, and tried to download source for it but I'm not sure how to build a library from it (it's all ARM assembly, it seems).我读到 Android 使用 Bionic C 库,并尝试下载它的源代码,但我不确定如何从中构建一个库(似乎都是 ARM 程序集)。

Is it true that the Android C library on the phone is different from the one provided with the Android NDK?手机上的Android C库和Android NDK提供的库是不是真的不一样? Will the one included with the NDK not allow me to compile native binaries I can execute through a terminal? NDK 中包含的那个是否不允许我编译可以通过终端执行的本机二进制文件? Any guidance here is greatly appreciated!非常感谢这里的任何指导!

Update:更新:

I finally got this to work using GCC 4.7.0 on Mac OS X. I downloaded the Bionic headers and then compiled a dynamically linked binary using the C library that comes with the Android NDK.我终于在 Mac OS X 上使用 GCC 4.7.0 让它工作了。我下载了 Bionic 头文件,然后使用 Android NDK 附带的 C 库编译了一个动态链接的二进制文件。 I was able to get a test app to work on the phone using the phone's C lib (the binary was 33K).我能够使用手机的 C 库(二进制文件为 33K)让测试应用程序在手机上运行。 I also tried to statically link against the NDK's C library, and that also worked.我还尝试静态链接到 NDK 的 C 库,这也有效。

In order to get this all working I had to pass -nostdlib to GCC and then manually add crtbegin_dynamic.o and crtend_android.o to GCC's command line.为了让这一切正常工作,我必须将 -nostdlib 传递给 GCC,然后手动将 crtbegin_dynamic.o 和 crtend_android.o 添加到 GCC 的命令行。 It works something like this:它的工作原理是这样的:

$CC \
$NDK_PATH/usr/lib/crtbegin_dynamic.o \
hello.c -o hello \
$CFLAGS \
$NDK_PATH/usr/lib/crtend_android.o

For static binaries, use "crtbegin_static.o."对于静态二进制文件,使用“crtbegin_static.o”。 This is explained in the crtbegin_dynamic.S/crtbegin_static.S source.这在 crtbegin_dynamic.S/crtbegin_static.S 源中进行了解释。

For this experiment, I only used plain 'ol GCC 4.7.0 and Binutils 2.22.对于这个实验,我只使用了普通的 'ol GCC 4.7.0 和 Binutils 2.22。 I also compiled GCC with newlib, but I am not actually linking my ARM binaries with newlib at all.我还用 newlib 编译了 GCC,但我实际上并没有将我的 ARM 二进制文件与 newlib 链接。 I am forcing GCC/ld to link directly to the libc provided with the Android NDK, or in the case of dynamic binaries, to the libc on the phone.我强制 GCC/ld 直接链接到 Android NDK 提供的 libc,或者在动态二进制文件的情况下,链接到手机上的 libc。

Just use the android-ndk.只需使用 android-ndk。 And build a Android.mk like so.并像这样构建一个Android.mk。 include $(BUILD_EXECUTABLE) is what tells it build a executable instead of a JNI .lib include $(BUILD_EXECUTABLE)告诉它构建可执行文件而不是 JNI .lib

Android.mk安卓.mk

ifneq ($(TARGET_SIMULATOR),true)

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

LOCAL_CFLAGS += -Wall


LOCAL_LDLIBS := -L$(LOCAL_PATH)/lib -llog -g

LOCAL_C_INCLUDES := bionic
LOCAL_C_INCLUDES += $(LOCAL_PATH)/include

LOCAL_SRC_FILES:= main.cpp

LOCAL_MODULE := mycmd

include $(BUILD_EXECUTABLE)

endif  # TARGET_SIMULATOR != true

First, make sure you have the NDK:首先,确保您有 NDK:

http://developer.android.com/tools/sdk/ndk/index.html http://developer.android.com/tools/sdk/ndk/index.html

Here is the easiest way to compile a C binary for your phone:这是为您的手机编译 C 二进制文件的最简单方法:

http://developer.android.com/tools/sdk/ndk/index.html http://developer.android.com/tools/sdk/ndk/index.html

http://www.kandroid.org/ndk/docs/STANDALONE-TOOLCHAIN.html http://www.kandroid.org/ndk/docs/STANDALONE-TOOLCHAIN.html

Usually $NDK(may be different) =通常$NDK(可能不同)=

Linux: Linux:

/home/ <user> /android-ndk /home/ <user> /android-ndk

Mac OS X: Mac OS X:

/Users/ <user> /android-ndk /Users/ <user> /android-ndk

In Terminal:在终端:

# create tool-chain - one line
# New method in ndk 12.
$NDK/build/tools/make_standalone_toolchain.py --arch arm --install-dir=/tmp/my-android-toolchain
# Old method.
#$NDK/build/tools/make-standalone-toolchain.sh --platform=android-3 --install-dir=/tmp/my-android-toolchain

# add to terminal PATH variable
export PATH=/tmp/my-android-toolchain/bin:$PATH

# make alias CC be the new gcc binary
export CC=arm-linux-androideabi-gcc

# compile your C code(I tried hello world)
$CC -o foo.o -c foo.c

# push binary to phone
adb push foo.o /data/local/tmp

# execute binary
adb /data/local/tmp/foo.o

Using CMake with the Android NDK is a nice way to compile Android console applications.将 CMake 与 Android NDK 一起使用是编译 Android 控制台应用程序的好方法。

Download CMake and android-cmake (set it up like this ).下载CMakeandroid-cmake像这样设置)。 If your program is called main.c, then write the following in file CMakeLists.txt :如果您的程序名为 main.c,则在文件CMakeLists.txt中写入以下内容:

project(test)
cmake_minimum_required(VERSION 2.8)
add_executable(test ./main.c)

and run cmake -DCMAKE_TOOLCHAIN_FILE=$ANDTOOLCHAIN .并运行cmake -DCMAKE_TOOLCHAIN_FILE=$ANDTOOLCHAIN .

You will then have a Makefile for your program, you can run make to have your test executable.然后,您将拥有一个程序的 Makefile,您可以运行make来获得您的test可执行文件。

In CMake, you can cross build using toolchain files.在 CMake 中,您可以使用工具链文件进行交叉构建。

From google developers :来自谷歌开发者

cmake \
    -DCMAKE_TOOLCHAIN_FILE=$NDK/build/cmake/android.toolchain.cmake \
    -DANDROID_ABI=$ABI \
    -DANDROID_PLATFORM=android-$MINSDKVERSION \
    $OTHER_ARGS

CMake has its own built-in NDK support. CMake 有自己的内置 NDK 支持。 Before CMake 3.21, this workflow is not supported by Android and is often broken with new NDK releases.在 CMake 3.21 之前,Android 不支持此工作流程,并且经常会因新的 NDK 版本而中断。 Starting from CMake 3.21, the implementations are merged.从 CMake 3.21 开始,实现被合并。

Starting from cmake 3.21 you can:从 cmake 3.21 开始,您可以:

mkdir build
cd build
cmake ..
  -DCMAKE_SYSTEM_NAME=Android
  -DCMAKE_SYSTEM_VERSION=23 # API level. optional, recommanded
  -DCMAKE_ANDROID_NDK=path/to/ndk
  -DCMAKE_ANDROID_ARCH=arm # optional, recommanded
  -DCMAKE_ANDROID_ARCH_ABI=armeabi # optional, recommanded

Note: in the command above, line endings ( <line feed> ) are not escaped, please don't copy-paste this command directly in your shell注意:在上面的命令中,行尾( <line feed> )没有被转义,请不要直接在你的 shell 中复制粘贴这个命令

See Cross Compiling for Android with the NDK for more information about variables, possible values, and determenation algorithms.有关变量、可能值和确定算法的更多信息,请参阅使用 NDK 为 Android 进行交叉编译

Try if if the agcc wrapper can help you as referenced in the Android-tricks blog.尝试如果agcc 包装器可以帮助您,如Android-tricks博客中所引用的那样。 According to the blog post you want to use the bionic library, but the one already installed on the phone , not some separately compiled version.根据博客文章你想使用仿生库,但是手机上已经安装了一个,而不是一些单独编译的版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以在非 root 的 android 手机上运行本机 arm 二进制文件? - Is it possible to run a native arm binary on a non-rooted android phone? 假设我想在桌面上运行android模拟器*作为手机* - suppose I want to just run the android emulator *as a phone* on my desktop 如何在Android终端中编译和运行Java程序 - How to compile and run java programmes in Android terminal 我可以开发可在没有手机的情况下运行的Android手表应用吗? - Can I develop Android watch app to run without mobile phone? 编译和运行项目时唤醒和解锁Android手机屏幕? - Wake and unlock Android phone screen when compile and run project? 如何在我的三星 S9 实体手机上运行我的 react-native 应用程序? - How can I run my react-native app on my physical Samsung S9 phone? 希望开发分支与实时版本(android)一起在手机上运行 - want a dev branch to run on phone along with live version (android) 我想知道我们如何在Android手机上发送和接收文件? - i want to know how can we send and recieve files on android phone? Android Studio apk 可以在苹果手机上运行吗? - Can Android Studio apk run in apple phone? 如何通过react-native控制android手机的电信状态? - How can I control telecom status of android cell phone by react-native?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM