利用源码编译Android系统的APK和可执行命令的方法

时间:2022-09-10 00:17:55

编译android系统apk
1、设置环境

?
1
2
3
export android_home=/home/administrator/soft/android
export path=$path:$android_home/out/host/linux-x86/bin
export android_product_out=$android_home/out/target/product/generic

2、创建项目

?
1
2
3
4
5
6
7
8
9
android create project -t 1 -k com.lhw.led -a mainactivity -p ./led
avd id: 1,it's get by command "android list"
 
project name: led
package: com.lhw.led
 
main activity:mainactivity
 
project path: ./

接下来就是自己编写android程序了。

3、编写android.mk
android.mk需要放在项目根路径下,即led下,内容:

?
1
2
3
4
5
6
7
8
9
10
local_path       := $(call my-dir)
 
include $(clear_vars)
local_src_files     := $(call all-subdir-java-files)
local_package_name   := led
 
local_java_libraries  :=
local_static_java_libraries :=
 
include $(build_package)


4、编译
在android源码目录下执行:

?
1
2
./build/envsetup.sh
mmm /home/administrator/workspace/android_test/led/

执行结果:

?
1
install: out/target/product/generic/system/app/led.apk


编译android系统可执行命令

android系统允许开发者,自己编写"ls"这样的命令放到android系统中。

1、编写c文件:mycmd.c

?
1
2
3
4
5
6
7
#include <stdio.h>
 
int main(int argc, char **argv)
{
  printf("this is my command!\n");
  return 0;
}

2、编写android.mk文件

?
1
2
3
4
5
6
7
8
9
local_path   := $(call my-dir)
 
local_src_files := mycmd.c
local_module  := mycmd
local_module_tags := mycmd
local_shared_libraries := libc
local_static_libraries :=
 
include $(build_executable) 


3、编译

?
1
mmm /home/administrator/workspace/android_test/mycmd/

编译完成后信息:

?
1
install: out/target/product/generic/system/bin/mycmd