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。

还要注意的是 := 不要写成了+=了哦。

 

附:编写各种类型的Android.mk,出处我忘记了,是以前浏览是拷贝下来的。如果侵犯了作者的权益,烦请联系我[email protected]。我及时删除。

 

一、编译一个简单的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文件,资源文件,包的引用目录地址

3.忘了是从哪篇看到了,博主若要本人删除,请联系我。。。

你可能感兴趣的:(android)