欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 系统——Android中各类.mk文件的编写

程序员文章站 2022-05-13 22:54:29
android中各类.mk文件的编写 local_path:= $(call my-dir) include $(clear_vars) local_static_java_libraries :=...

android中各类.mk文件的编写

local_path:= $(call my-dir)

include $(clear_vars)
local_static_java_libraries := xsocket jackson-mapper logging jackson-core javatar log4j
local_module_tags := optional
local_src_files := $(call all-subdir-java-files)
local_package_name := test
local_certificate := platform
include $(build_package)
##################################################
include $(clear_vars)
local_prebuilt_static_java_libraries := xsocket:lib/xsocket-2.8.14.jar \
    jackson-mapper:lib/jackson-mapper-asl-1.6.2.jar \
    logging:lib/commons-logging.jar \
    jackson-core:lib/jackson-core-asl-1.6.2.jar \
    javatar:lib/javatar-2.5.jar \
    log4j:lib/log4j-1.2.15.jar
include $(build_multi_prebuilt)
# use the folloing include to make our test apk.
include $(call all-makefiles-under,$(local_path))
以上是我的一个项目中所需要的第三方jar包,主要参考了android中的calculator应用,该应用也引用了一个第三方的jar包arity-2.1.2.jar。
需要注意的是,当你要引用的jar包不止一个时,有两个关键的地方需要注意的。
local_static_java_libraries := xsocket jackson-mapper logging jackson-core javatar log4j
local_prebuilt_static_java_libraries := xsocket:lib/xsocket-2.8.14.jar \
    jackson-mapper:lib/jackson-mapper-asl-1.6.2.jar \
    logging:lib/commons-logging.jar \
    jackson-core:lib/jackson-core-asl-1.6.2.jar \
    javatar:lib/javatar-2.5.jar \
    log4j:lib/log4j-1.2.15.jar
xsocket jackson-mapper logging jackson-core javatar log4j这几个只是名字,可以随便取。local_prebuilt_static_java_libraries后面才是真正其作用的地方。如:
xsocket:lib/xsocket-2.8.14.jar 引用的是lib目录中的xsocket-2.8.14.jar。
还要注意的是 := 不要写成了+=了哦。
一、编译一个简单的apk
local_path := $(call my-dir)
include $(clear_vars)
# build all java files in the java subdirectory
local_src_files := $(call all-subdir-java-files)
# name of the apk to build
local_package_name := localpackage
# tell it to build an apk
include $(build_package)

二、编译一个依赖静态.jar文件的apk
local_path := $(call my-dir)
include $(clear_vars)
# list of static libraries to include in the package
local_static_java_libraries := static-library
# build all java files in the java subdirectory
local_src_files := $(call all-subdir-java-files)
# name of the apk to build
local_package_name := localpackage
# tell it to build an apk
include $(build_package)
注:local_static_java_libraries 后面应是你的apk程序所需要的java库的jar文件名。

三、编译一个需要platform key签名的apk (限制的权限)
local_path := $(call my-dir)
include $(clear_vars)
# build all java files in the java subdirectory
local_src_files := $(call all-subdir-java-files)
# name of the apk to build
local_package_name := localpackage
local_certificate := platform
# tell it to build an apk
include $(build_package)
注:local_certificate 后面应该是签名文件的文件名

四、编译一个需要特殊vendor key签名的apk
local_path := $(call my-dir)
include $(clear_vars)
# build all java files in the java subdirectory
local_src_files := $(call all-subdir-java-files)
# name of the apk to build
local_package_name := localpackage
local_certificate := vendor/example/certs/app
# tell it to build an apk
include $(build_package)

五、装载一个普通的第三方apk
local_path := $(call my-dir)
include $(clear_vars)
# module name should match apk name to be installed.
local_module := localmodulename
local_src_files := $(local_module).apk
local_module_class := apps
local_module_suffix := $(common_android_package_suffix)
local_certificate := platform
include $(build_prebuilt)

六、装载需要.so(动态库)的第三方apk
local_path := $(my-dir)
include $(clear_vars)
local_module := baiduinput_android_v1.1_1000e
local_src_files := $(local_module).apk
local_module_class := apps
local_module_suffix := $(common_android_package_suffix)
local_certificate := platform
include $(build_prebuilt)

#################################################################
####### copy the library to /system/lib #########################
#################################################################
include $(clear_vars)
local_module := libinputcore.so
local_module_class := shared_libraries
local_module_path := $(target_out_shared_libraries)
local_src_files := lib/$(local_module)
override_build_module_path := $(target_out_intermediate_libraries)
include $(build_prebuilt)

七、编译一个静态java库
local_path := $(call my-dir)
include $(clear_vars)
# build all java files in the java subdirectory
local_src_files := $(call all-subdir-java-files)
# any libraries that this library depends on
local_java_libraries := android.test.runner
# the name of the jar file to create
local_module := sample
# build a static jar file.
include $(build_static_java_library)
注:local_java_libraries表示生成的java库的jar文件名。

在android源码开发过程中,需要整合进第三方app到系统中,作为内置应用。如果是第三方app是以apk形式整合进项目中,那么根据平台和公司的要求,只需修改编译脚本就可以实现整合,这里不再赘述;本文讲解拿到的第三方app是源码,需要自己写android.mk文件实现编译apk功能。
现在android app ide主要是两个工具eclipse和android studio。eclipse已经从google的android开发者网站中移除了下载链接,现在主流的android app开发工具就是android studio。
android 源码中package/app目录下的应用大部分还是eclipse的代码结构,第三方app如果是eclipse编写的,那么去源码中package/app目录下找一个应用的android.mk 类比改一下就可以。这里主要讲一下android studio 项目的android.mk怎么写。
android studio编写的app,app源码代码结构就跟eclipse不一样,实际上有用的app目录下的内容。我的android.mk就放在app/src/main目录下(一般android.mk都放在应用目录下,但实际上android源码在编译时会遍历package/app目录下的android.mk文件,不放心可以在项目应用根目录下写android.mk)
首先查看app/src/main目录下的build.gradle文件,在这个文件中会写明在开发中引用了哪些包,如果不是android源码中带有的(如android-support-v4,android-support-v7-xxx等),那么要去android studio的安装目录下找到这些jar包,放到app/src/main/libs/目录下(没有就自己创建一个),因为android studio在使用时会自动下载需要的包到安装目录下,所以在拿到源码时实际上这些包是没有的,在本地用android studio导入项目,它会自动联网同步这些包。
local_path := $(call my-dir)
include $(clear_vars) (经典开头两句)
local_package_name := xxx(apk的名字,如果这个值写的是test,那么编译出来的就是test.apk)
local_certificate := platform (系统签名)
local_resource_dir += $(local_path)/res
src_dirs := java/
local_src_files := $(call all-java-files-under, $(src_dirs))
##声明使用的java文件和资源文件地址,如果使用到aidl,需要特别声明,常见写法如下
#local_src_files += \
# src/xx/xx/xx/aidl/xxxone.aidl \
# src/xx/xx/xx//aidl/xxxtwo.aidl
local_multilib := 32(添加这句是为了防止在编译后使用apk时,提示so是32位而不是64位导致应用fc)
local_prebuilt_jni_libs_x86 := jnilibs/x86/libxxx.so
local_proguard_enabled := disabled (添加这句是为了防止在编译后使用apk时,提示找不到so库中对应的方法导致fc)
#如果使用了jni就要这么写
local_static_java_libraries += design \
android-support-v4 \
android-support-v7-appcompat \
android-support-v7-recyclerview \
recyclerview-v7 \
itextpdf(第三方包名)

local_prebuilt_static_java_libraries += \
itextpdf:libs/itextpdf-5.5.9.jar
#第三方包需要通过local_prebuilt_static_java_libraries声明
local_proguard_flag_files := proguard.flags
##在编译时如果提示包中有warning时,需要按照要求在文件中写dontwarn,proguard.flags需要自行创建,android studio不会创建这个文件
下面是我写的android.mk,以供参考:
local_path := $(call my-dir)
include $(clear_vars)
local_package_name := 应用名
local_certificate := platform
#如果使用的系统的包,需要引入他们使用的资源文件,否则会提示编译资源找不到的错误
local_resource_dir += frameworks/support/v7/appcompat/res
local_resource_dir += frameworks/support/v7/gridlayout/res
local_resource_dir += frameworks/support/v7/recyclerview/res
local_resource_dir += frameworks/support/design/res
local_resource_dir += $(local_path)/res
src_dirs := java/
local_src_files := $(call all-java-files-under, $(src_dirs))
#local_src_files += \
# src/xx/xx/xx/xxxone.aidl \
# src/xx/xx/xx/xxxtwo.aidl
local_static_java_libraries += design \
android-support-v4 \
itextpdf \
android-support-v7-appcompat \
android-support-v7-recyclerview \
recyclerview-v7
local_prebuilt_jni_libs := jnilibs/xx/libxxx.so
local_multilib := 32
local_proguard_flag_files := proguard.flags
local_aapt_flags := --auto-add-overlay
local_aapt_flags += --extra-packages android.support.v7.appcompat
local_aapt_flags += --extra-packages android.support.v7.gridlayout
local_aapt_flags += --extra-packages com.android.datetimepicker
local_aapt_flags += --extra-packages android.support.v7.recyclerview
local_aapt_flags += --extra-packages android.support.design
local_proguard_enabled := disabled
include $(build_package)
include $(clear_vars)
local_prebuilt_static_java_libraries += \
itextpdf:libs/itextpdf-5.5.9.jar
include $(build_multi_prebuilt)
include $(call all-makefiles-under,$(local_path))

特别补充:
1.第三方app如果使用到了design-xx.jar包中的类,编译可以通过,但是在使用时app会出现crash问题,log反应是design中类找不到。design是android提供的jar包,出现这个问题是,要去framework/support/design/目录下执行mm编译出design对应的jar包,再编译app,这时编译出的apk就没有找不到类的异常了
2.可以把android.mk放到应用的跟根目录下,这时候需要修改java文件,资源文件,包的引用目录地址