如何构建在 Android 上运行的本机(命令行)可执行文件?
- 2024-11-01 08:41:00
- admin 原创
- 35
问题描述:
我已经成功构建了一个使用本机(JNI)库的 Android 应用程序(GUI)。
但是,现在我想创建一个从命令行(root 权限)运行的可执行文件,并且根本不使用 GUI。我该如何构建这样的程序?
解决方案 1:
从 NDK r8d 开始,这个问题可以用一种更简单的方式解决。
创建具有以下目录层次结构的项目:
project/
jni/
Android.mk
Application.mk
*.c, *.cpp, *.h, etc.
在Android.mk中填写以下内容。最重要的是最后一行。其他变量的含义请查看NDK文档。
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := name-of-your-executable
LOCAL_SRC_FILES := a.cpp b.cpp c.cpp etc.cpp
LOCAL_CPPFLAGS := -std=gnu++0x -Wall -fPIE # whatever g++ flags you like
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog -fPIE -pie # whatever ld flags you like
include $(BUILD_EXECUTABLE) # <-- Use this to build an executable.
转到
project/
目录,然后输入
ndk-build
结果将被放置在 中project/libs/<arch>/name-of-your-executable
。
解决方案 2:
http://www.bekatul.info/content/native-c-application-android [已损坏(2015 年 11 月 9 日)]
Wayback Machine 链接
总结一下这篇文章...
测试代码是:
#include <stdio.h>//for printf
#include <stdlib.h>//for exit
int main(int argc, char **argv)
{
int i = 1;
i+=2;
printf("Hello, world (i=%d)!
", i);
return 0;
exit(0);
}
Makefile 为:
APP := test
ROOT := /home/dd/android
INSTALL_DIR := /data/tmp
NDK_PLATFORM_VER := 8
ANDROID_NDK_ROOT := $(ROOT)/android-ndk-r5
ANDROID_NDK_HOST := linux-x86
ANDROID_SDK_ROOT := $(ROOT)/android-sdk-linux_86
PREBUILD := $(ANDROID_NDK_ROOT)/toolchains/arm-eabi-4.4.0/prebuilt/$(ANDROID_NDK_HOST)
BIN := $(PREBUILD)/bin/
LIB := $(ANDROID_NDK_ROOT)/platforms/android-$(NDK_PLATFORM_VER)/arch-arm/usr/lib
INCLUDE := $(ANDROID_NDK_ROOT)/platforms/android-$(NDK_PLATFORM_VER)/arch-arm/usr/include
CC := $(BIN)/arm-eabi-gcc
GDB_CLIENT := $(BIN)/arm-eabi-gdb
LIBCRT := $(LIB)/crtbegin_dynamic.o
LINKER := /system/bin/linker
DEBUG := -g
CFLAGS := $(DEBUG) -fno-short-enums -I$(INCLUDE)
CFLAGS += -Wl,-rpath-link=$(LIB),-dynamic-linker=$(LINKER) -L$(LIB)
CFLAGS += -nostdlib -lc
all: $(APP)
$(APP): $(APP).c
$(CC) -o $@ $< $(CFLAGS) $(LIBCRT)
install: $(APP)
$(ANDROID_SDK_ROOT)/platform-tools/adb push $(APP) $(INSTALL_DIR)/$(APP)
$(ANDROID_SDK_ROOT)/platform-tools/adb shell chmod 777 $(INSTALL_DIR)/$(APP)
shell:
$(ANDROID_SDK_ROOT)/platform-tools/adb shell
run:
$(ANDROID_SDK_ROOT)/platform-tools/adb shell $(INSTALL_DIR)/$(APP)
debug-install:
$(ANDROID_SDK_ROOT)/platform-tools/adb push $(PREBUILD)/../gdbserver $(INSTALL_DIR)/gdbserver
$(ANDROID_SDK_ROOT)/platform-tools/adb shell chmod 777 $(INSTALL_DIR)/gdbserver
debug-go:
$(ANDROID_SDK_ROOT)/platform-tools/adb forward tcp:1234: tcp:1234
$(ANDROID_SDK_ROOT)/platform-tools/adb shell $(INSTALL_DIR)/gdbserver :1234 $(INSTALL_DIR)/$(APP)
debug:
$(GDB_CLIENT) $(APP)
clean:
@rm -f $(APP).o $(APP)
作者将这些文件存储在他/她的本地 Linux 计算机上:
/home/dd/android/dev/native/test.c
/home/dd/android/dev/native/Makefile
然后作者对其进行了编译和测试:
dd@abil:~/android/dev/native$ make clean; make; make install; make run
/home/dd/android/android-ndk-r5/toolchains/arm-eabi-4.4.0/prebuilt/linux-x86/bin//arm-eabi-gcc -c -fno-short-enums -I/home/dd/android/android-ndk-r5/platforms/android-9/arch-arm/usr/include test.c -o test.o
/home/dd/android/android-ndk-r5/toolchains/arm-eabi-4.4.0/prebuilt/linux-x86/bin//arm-eabi-g++ -Wl,--entry=main,-dynamic-linker=/system/bin/linker,-rpath-link=/home/dd/android/android-ndk-r5/platforms/android-9/arch-arm/usr/lib -L/home/dd/android/android-ndk-r5/platforms/android-9/arch-arm/usr/lib -nostdlib -lc -o test test.o
/home/dd/android/android-sdk-linux_86/platform-tools/adb push test /data/tmp/test
45 KB/s (2545 bytes in 0.054s)
/home/dd/android/android-sdk-linux_86/platform-tools/adb shell chmod 777 /data/tmp/test
/home/dd/android/android-sdk-linux_86/platform-tools/adb shell /data/tmp/test
Hello, world (i=3)!
使用的 SDK 和 NDK 是:
source code: /home/dd/android/dev/native
android ndk: /home/dd/android/android-ndk-r5
android sdk: /home/dd/android/android-sdk-linux_86
但是,调试指南才是真正好的部分!复制并粘贴...
设置编译以启用调试:
DEBUG = -g
CFLAGS := $(DEBUG) -fno-short-enums -I$(ANDROID_NDK_ROOT)/platforms/android-$(NDK_PLATFORM_VER)/arch-arm/usr/include
将 gdbserver 文件 ($(PREBUILD)/../gdbserver) 复制到模拟器,在 Makefile 中添加目标,以使其变得简单:
debug-install:
$(ANDROID_SDK_ROOT)/platform-tools/adb push $(PREBUILD)/../gdbserver $(INSTALL_DIR)/gdbserver
$(ANDROID_SDK_ROOT)/platform-tools/adb shell chmod 777 $(INSTALL_DIR)/gdbserver
现在我们将在端口 1234 进行调试:
debug-go:
$(ANDROID_SDK_ROOT)/platform-tools/adb forward tcp:1234: tcp:1234
$(ANDROID_SDK_ROOT)/platform-tools/adb shell $(INSTALL_DIR)/gdbserver :1234 $(INSTALL_DIR)/$(APP)
然后执行:
dd@abil:~/android/dev/native$ make clean; make; make install; make debug-install; make debug-go
/home/dd/android/android-ndk-r5/toolchains/arm-eabi-4.4.0/prebuilt/linux-x86/bin//arm-eabi-gcc -c -g -fno-short-enums -I/home/dd/android/android-ndk-r5/platforms/android-9/arch-arm/usr/include test.c -o test.o
/home/dd/android/android-ndk-r5/toolchains/arm-eabi-4.4.0/prebuilt/linux-x86/bin//arm-eabi-g++ -Wl,--entry=main,-dynamic-linker=/system/bin/linker,-rpath-link=/home/dd/android/android-ndk-r5/platforms/android-9/arch-arm/usr/lib -L/home/dd/android/android-ndk-r5/platforms/android-9/arch-arm/usr/lib -nostdlib -lc -o test test.o
/home/dd/android/android-sdk-linux_86/platform-tools/adb push test /data/tmp/test
71 KB/s (3761 bytes in 0.051s)
/home/dd/android/android-sdk-linux_86/platform-tools/adb shell chmod 777 /data/tmp/test
/home/dd/android/android-sdk-linux_86/platform-tools/adb push /home/dd/android/android-ndk-r5/toolchains/arm-eabi-4.4.0/prebuilt/linux-x86/../gdbserver /data/tmp/gdbserver
895 KB/s (118600 bytes in 0.129s)
/home/dd/android/android-sdk-linux_86/platform-tools/adb shell chmod 777 /data/tmp/gdbserver
/home/dd/android/android-sdk-linux_86/platform-tools/adb forward tcp:1234: tcp:1234
/home/dd/android/android-sdk-linux_86/platform-tools/adb shell /data/tmp/gdbserver :1234 /data/tmp/test
Process /data/tmp/test created; pid = 472
Listening on port 1234
现在打开其他控制台并执行调试器:
dd@abil:~/android/dev/native$ make debug
/home/dd/android/android-ndk-r5/toolchains/arm-eabi-4.4.0/prebuilt/linux-x86/bin//arm-eabi-gdb test
GNU gdb 6.6
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "--host=x86_64-linux-gnu --target=arm-elf-linux"...
(gdb) target remote :1234
Remote debugging using :1234
warning: Unable to find dynamic linker breakpoint function.
GDB will be unable to debug shared library initializers
and track explicitly loaded dynamic code.
0xb0001000 in ?? ()
(gdb) b main
Breakpoint 1 at 0x82fc: file test.c, line 6.
(gdb) c
Continuing.
Error while mapping shared library sections:
/system/bin/linker: No such file or directory.
Error while mapping shared library sections:
libc.so: Success.
Breakpoint 1, main (argc=33512, argv=0x0) at test.c:6
6 int i = 1;
(gdb) n
7 i+=2;
(gdb) p i
$1 = 1
(gdb) n
9 printf("Hello, world (i=%d)!
", i);
(gdb) p i
$2 = 3
(gdb) c
Continuing.
Program exited normally.
(gdb) quit
嗯,没问题。另一个控制台将提供额外的输出,如下所示:
Remote debugging from host 127.0.0.1
gdb: Unable to get location for thread creation breakpoint: requested event is not supported
Hello, world (i=3)!
Child exited with retcode = 0
Child exited with status 0
GDBserver exiting
解决方案 3:
这是遵循 KennyTM 答案的示例项目。您可以从头开始创建它,也可以修改另一个项目,例如hello-jni
在 NDK 示例中。
jni/main.c:
#include <stdio.h>
int main() {
printf("hello
");
return 0;
}
jni/应用程序.mk:
#APP_ABI := all
APP_ABI := armeabi-v7a
jni/Android.mk:
LOCAL_PATH := $(call my-dir)
# first target: the hello-jni example
# it shows how to build multiple targets
# {{ you may comment it out
include $(CLEAR_VARS)
LOCAL_MODULE := hello-jni
LOCAL_SRC_FILES := hello-jni.c
LOCAL_LDLIBS := -llog -L$(LOCAL_PATH)/lib -lmystuff # link to libmystuff.so
include $(BUILD_SHARED_LIBRARY)
#}} you may comment it out
# second target
include $(CLEAR_VARS)
LOCAL_MODULE := hello
LOCAL_SRC_FILES := main.c
include $(BUILD_EXECUTABLE) # <-- Use this to build an executable.
我必须指出,您不会在 stdout 输出中看到任何日志,您必须使用adb logcat
才能看到它。
因此,如果您想要记录:
jni/main.c:
#include <stdio.h>
#include <android/log.h>
int main() {
printf("hello
");
__android_log_print(ANDROID_LOG_DEBUG , "~~~~~~", "log %i", 0); // the 3rd arg is a printf-style format string
return 0;
}
jni/Android.mk中的相应部分变为:
LOCAL_PATH := $(call my-dir)
#...
include $(CLEAR_VARS)
LOCAL_MODULE := hello
LOCAL_SRC_FILES := main.c
LOCAL_LDLIBS := -llog # no need to specify path for liblog.so
include $(BUILD_EXECUTABLE) # <-- Use this to build an executable.
解决方案 4:
某人某处的回答让我朝着正确的方向前进,但它包含错误/不精确之处。
就 gdbserver 而言,adb 命令
$(ANDROID_SDK_ROOT)/platform-tools/adb push $(PREBUILD)/../gdbserver $(INSTALL_DIR)/gdbserver
永远无法工作,原因很明显,因为在目录 $(PREBUILD) 和 gdbserver 之间有一个目录 android-arm。最好设置
PREBUILDDEBUG=$(ANDROID_NDK_ROOT)/prebuilt/android-arm
并将前一个命令替换为
$(ANDROID_SDK_ROOT)/platform-tools/adb push $(PREBUILDDEBUG)/gdbserver $(INSTALL_DIR)/gdbserver
有了它,我就可以正常使用 Android 虚拟设备了。(显然没有分段错误。)在我的真实设备上,我确实遇到了分段错误。这就是
make run
部分。关于调试部分,无论是在模拟器还是在真实设备上,当我执行
b main
在 gdb 模式下。我不知道为什么。
- 2024年20款好用的项目管理软件推荐,项目管理提效的20个工具和技巧
- 2024年开源项目管理软件有哪些?推荐5款好用的项目管理工具
- 项目管理软件有哪些?推荐7款超好用的项目管理工具
- 项目管理软件哪个最好用?盘点推荐5款好用的项目管理工具
- 项目管理软件有哪些最好用?推荐6款好用的项目管理工具
- 项目管理软件有哪些,盘点推荐国内外超好用的7款项目管理工具
- 2024项目管理软件排行榜(10类常用的项目管理工具全推荐)
- 项目管理软件排行榜:2024年项目经理必备5款开源项目管理软件汇总
- 2024年常用的项目管理软件有哪些?推荐这10款国内外好用的项目管理工具
- 项目管理必备:盘点2024年13款好用的项目管理软件