在~/workspace目录下编译好安卓源码,在安卓模拟器中跑一个最简单的C程序,下面来说一下跑C程序的过程。
史上最简单的C程序:
#include<stdio.h>
int main()
{
printf("Hello,world!\n");
return 0;
}
我们需要一个Android.mk文件来指明如何编译C代码:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES:=HelloWorld.c
LOCAL_MODULE:=HelloWorld
LOCAL_MODULE_TAGS:=optional
include $(BUILD_EXECUTABLE)
然后使用mm命令来编译,如果找不到mm命令那么运行如下两行:
1.source build/envsetup.sh
2.lunch(选择源码编译时对应的选项就好)
然后cd到C程序和Android存放的目录运行mm,就会编译产生HelloWorld二进制程序,mm执行结果如下:
============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=4.4.2
TARGET_PRODUCT=aosp_x86
TARGET_BUILD_VARIANT=eng
TARGET_BUILD_TYPE=release
TARGET_BUILD_APPS=
TARGET_ARCH=x86
TARGET_ARCH_VARIANT=x86
TARGET_CPU_VARIANT=
HOST_ARCH=x86
HOST_OS=linux
HOST_OS_EXTRA=Linux-3.2.0-24-generic-x86_64-with-Ubuntu-12.04-precise
HOST_BUILD_TYPE=release
BUILD_ID=KOT49H
OUT_DIR=out
============================================
make: Entering directory `/home/debai/workspace'
target C: HelloWorld <= HelloWorld/HelloWorld.c
target Executable: HelloWorld (out/target/product/generic_x86/obj/EXECUTABLES/HelloWorld_intermediates/LINKED/HelloWorld)
target Symbolic: HelloWorld (out/target/product/generic_x86/symbols/system/bin/HelloWorld)
Export includes file: HelloWorld/Android.mk -- out/target/product/generic_x86/obj/EXECUTABLES/HelloWorld_intermediates/export_includes
target Strip: HelloWorld (out/target/product/generic_x86/obj/EXECUTABLES/HelloWorld_intermediates/HelloWorld)
Install: out/target/product/generic_x86/system/bin/HelloWorld
如何让编译好的HelloWorld在安卓的模拟器上跑起来呢?只要使用adb将HelloWorld push到模拟器就好了!
1.首先使用emulator启动模拟器
2.adb push命令:
<span style="font-size:14px;">adb push ~/workspace/out/target/product/generic_x86/system/bin/HelloWorld /data</span>
注:我的安卓源码是在workspace目录下面编译的
3.adb shell,此时发现模拟器的/data目录下面就有一个HelloWorld程序,直接执行它就好了
参考:参考