1. 发现问题
在Android工程开发中难免会有引入第三方库的需求,例如GSON,Okhttp,Glide等等,但是在源码中用makefile编译如何让编译器知道你引入的库呢?下面我举一个例子来说明
- 例如我们工程需要引入GSON库,没引入的时候编译报错如下图。
-
引入库制定编译规则
依赖库问题.png
2. 寻找原因
原因的话编译器已经报出来了,就是找不到对应的类,也就是找不到对应的库文件。
3. 解决办法(引入第三方库方法)
在解决问题之前首先介绍一下这两个编译参数:BUILD_JAVA_LIBRARY 与 BUILD_STATIC_JAVA_LIBRARY
在Android.mk中可通过调用include $(BUILD_JAVA_LIBRARY)和include $(BUILD_STATIC_JAVA_LIBRARY)来分别生成目标设备上的共享JAVA库与静态JAVA库。
二者的区别在于**静态JAVA库是由.class文件打包而成JAR包,它在任何一个JAVA虚拟机上都可以运行**;而**共享JAVA库则是在静态库的基础上进一步打包成的.dex文件**,众所周知,dex是在android系统上所使用的文件格式。
由以上结论可做出进一步的推论:即Android.mk中变量LOCAL_JAVA_LIBRARIES所指定的为android系统使用的dex类库;而LOCAL_STATIC_JAVA_LIBRARIES变量所指定的则是.class文件打包而成的JAR文件:即静态JAVA库。
**BUILD_STATIC_JAVA_LIBRARY**会生成out/target/common/obj/JAVA_LIBRARIES/lib名_intermediates目录及其下的JAR文件;而**BUILD_JAVA_LIBRARY**生成此目录的同时会将其中的JAR包复制到out/target/product/<product-name>/system/framework/中;除此之外,还存在**BUILD_HOST_JAVA_LIBRARY**命令则是在out/host/linux-x86/framework目录下生成相应的JAR包。分别使用此三条命令执行的结果如下所示:注意其中带有红色下划线的LOG信息。
#编译第三方库基本用法
#在Android.mk目录下建立libs,然后将jar包放入libs,先预编译到out/target/product/generic/obj/JAVA_LIBRARIES下,然后本项目就会找到依赖的者jar包了
include $(CLEAR_VARS)
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := gson:libs/gson-2.8.2.jar
include $(BUILD_MULTI_PREBUILT)
#################以下是顺带的说明########################
#################完整的mk文件###########################
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_PACKAGE_NAME := Permission
LOCAL_MODULE_TAGS := optional
LOCAL_CERTIFICATE := platform
#LOCAL_SDK_VERSION 和LOCAL_PRIVATE_PLATFORM_APIS在Android.mk中必须用一个
#LOCAL_SDK_VERSION := current 意思是编译时忽略系统隐藏类(@hide)
#LOCAL_PRIVATE_PLATFORM_APIS := true 表示可以应用系统的API(@hide或者@SystemApi)
LOCAL_PRIVATE_PLATFORM_APIS := true
#关闭混淆优化器,没有这个配置,会报:
#there were 194 unresolved references to program class members.Your input classes appear to be inconsistent.
#there were 2494 unresolved references to library class members. You probably need to update the library versions. Alternatively, you may have to specify the option
LOCAL_PROGUARD_ENABLED := disabled
LOCAL_SRC_FILES := $(call all-java-files-under, src)
#编译aidl文件
LOCAL_SRC_FILES += $(call all-Iaidl-files-under,src/com/xh/permission) \
src/com/xh/permission/ICarPermissionManager.aidl
LOCAL_AIDL_INCLUDES := $(LOCAL_PATH)/src/com/xh/permission
#应用第三方库
LOCAL_STATIC_JAVA_LIBRARIES := gson \
android-support-v7-appcompat \
xhframework
LOCAL_STATIC_ANDROID_LIBRARIES := android-support-annotations
include $(BUILD_PACKAGE)
################我是分割线#####################
LOCAL_STATIC_JAVA_LIBRARIES := gson
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := gson:libs/gson-2.8.2.jar
include $(BUILD_MULTI_PREBUILT)
4.扩展点
如果在Android FrameWork集成第三方SDK的jar包和so库,首先在framework/opt/建立XXXSDK的文件夹讲jar包和so库拷贝进去。
framework依赖库编译.png###################### mk文件如下 ################################
# Copyright (C) 2009 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
LOCAL_PATH := $(my-dir)
include $(CLEAR_VARS)
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := okhttp-3.12.0.jar \
rxjava-2.1.12.jar \
sqlite.jar
include $(BUILD_MULTI_PREBUILT)
##################################################
include $(CLEAR_VARS)
LOCAL_MODULE := libcarlife
LOCAL_SRC_FILES := libs/libcarlife.so
LOCAL_MODULE_SUFFIX := .so
LOCAL_MODULE_CLASS := SHARED_LIBRARIES
include $(BUILD_PREBUILT)
include $(call all-makefiles-under,$(LOCAL_PATH))
##################################################
include $(CLEAR_VARS)
LOCAL_MODULE := libcarlifeserver
LOCAL_SRC_FILES := libs/libcarlifeserver.so
LOCAL_MODULE_SUFFIX := .so
LOCAL_MODULE_CLASS := SHARED_LIBRARIES
include $(BUILD_PREBUILT)
include $(call all-makefiles-under,$(LOCAL_PATH))
##################################################
include $(CLEAR_VARS)
LOCAL_MODULE := libcarlifeservicejni
LOCAL_SRC_FILES := libs/libcarlifeservicejni.so
LOCAL_MODULE_SUFFIX := .so
LOCAL_MODULE_CLASS := SHARED_LIBRARIES
include $(BUILD_PREBUILT)
include $(call all-makefiles-under,$(LOCAL_PATH))
##################################################
include $(CLEAR_VARS)
LOCAL_MODULE := libcarlifevehicle
LOCAL_SRC_FILES := libs/libcarlifevehicle.so
LOCAL_MODULE_SUFFIX := .so
LOCAL_MODULE_CLASS := SHARED_LIBRARIES
include $(BUILD_PREBUILT)
include $(call all-makefiles-under,$(LOCAL_PATH))
网友评论