1、在Android源代码工程环境中,可以用C/C++语言编写应用程序模块(可执行程序或者动态链接库so)。这些模块的源代码一般位于external目录下,编译结果一般位于/system/bin或者/system/lib目录下。
2、Hello Word程序示例
2.1、代码结构
源代码根--|
|--external--|
|--hello_word--|
|--hello.c
|--Android.mk
2.2 hello.c
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv){
printf("hello word");
return 1;
}
2.3 Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := hello_word_test
LOCAL_SRC_FILES := $(call all-subdir-c-files)
include $(BUILD_EXECUTABLE)
2.4用mmm命令编译,用make snod打包
注意mmm命令不是直接可用的,必须先在 源代码根/build 下执行source envsetup.sh之后再使用。
在 源代码根下执行 mmm ./external/hello_word/
执行完mmm之后,就可以看到 源代码根/out/target/product/generic/system/bin 下有 hello_word_test 可执行文件。
在 源代码根下执行 make snod
在执行完make命令打包之后,这个可执行文件就打包进了 源代码根/out/target/product/generic/system.img 镜像文件中去了。
2.5验证
用模拟器加载打包好的镜像文件,启动模拟器,用adb shell连接上,在/system/bin里面执行 ./hello_word_test即可以看到运行结果。
2.6 C++语言
main.cpp文件
#include<iostream>
using namespace std;
int main(int argc, char *argv[])
{
cout<<"hello,this is my first linux test."<<endl;
return 1;
}
Android.mk文件
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := hello_word_cpp
LOCAL_SRC_FILES := $(call all-subdir-cpp-files)
include $(BUILD_EXECUTABLE)