Android 7.1预编译编译第三方so

在项目开发中有时候会用到第三方库,但是在Android编译系统中不能像Linux那样直接在Makefile链接某个so,Android所有的库都需要在out/target/product/项目/obj/lib/生成,所有我们可以采用Android 的预编译BUILD_PREBUILT将第三方库编译到out/target/product/项目/obj/lib/中,于是有以下的Android.mk

#1.预编译第三方库liba.so

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

 #要生成的库
LOCAL_MODULE := liba 

#默认选项
LOCAL_MODULE_TAGS := optional

#所属的类是动态库
LOCAL_MODULE_CLASS := SHARED_LIBRARIES 

#这个是生成的后缀,我在hisi上面是可以不用加这个的,但是高通上面必须要加这个,不然编译的出来的库不带so
LOCAL_MODULE_SUFFIX := .so 

#这个是第三方的库
LOCAL_SRC_FILES := armeabi-v7a/liba.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(BUILD_PREBUILT)

#2.使用该库

include $(CLEAR_VARS)
LOCAL_MODULE := libmaintest
LOCAL_SRC_FILES := main.c
LOCAL_C_INCLUDES := $(LOCAL_PATH)

#链接上面预编译的共享库
LOCAL_SHARED_LIBRARIES := liba
include $(BUILD_SHARED_LIBRARY)

 

上面的Android.mk程序在hisi3798MV200上是可以编译通过的,因为他编译出来的so全部都是32位的,但是在高通msm8953上死活都编译不过

ninja: Entering directory `.'
ninja: error: 'out/target/product/msm8953_64/obj_arm/SHARED_LIBRARIES/liba_intermediates/export_includes', needed by 'out/target/product/msm8953_64/obj_arm/SHARED_LIBRARIES/libmaintest_intermediates/import_includes', missing and no known rule to make it
build/core/ninja.mk:148: recipe for target 'ninja_wrapper' failed
 

这就尴尬了,看上面的log,预编译第三方库liba.so根本就没有编译,于是我把include $(BUILD_SHARED_LIBRARY)注释掉,发现预编译的第三方库又能编译,这就奇怪了,网上找了好多资料,找到好多关于预编译编译第三方库Android.mk,依旧还是无效,于是在高通的SDK中mgrep -nR "LOCAL_MODULE_CLASS := SHARED_LIBRARIES "找到了高通关于编译第三方so的Android.mk

ifeq ($(TARGET_BUILD_PDK),true)
ifeq ($(TARGET_BOARD_PLATFORM),msm8996)
#----------------------------------------------------------------------
# Fixup libandroid dependency
#----------------------------------------------------------------------
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libandroid
LOCAL_MODULE_SUFFIX := .so 
LOCAL_MODULE_CLASS := SHARED_LIBRARIES                                                                                                                                                       
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES_arm := ../../../../$(PRODUCT_OUT)/obj/PACKAGING/pdk_fusion_intermediates/system/lib/libandroid.so
LOCAL_SRC_FILES_arm64 := ../../../../$(PRODUCT_OUT)/obj/PACKAGING/pdk_fusion_intermediates/system/lib64/libandroid.so
LOCAL_MULTILIB := both
include $(BUILD_PREBUILT)

$(LOCAL_PATH)/../../../../out/target/product/msm8996/obj/PACKAGING/pdk_fusion_intermediates/system/lib/libandroid.so : $(PRODUCT_OUT)/obj/PACKAGING/pdk_fusion_intermediates/pdk_fusion.stamp$(LOCAL_PATH)/../../../../out/target/product/msm8996/obj/PACKAGING/pdk_fusion_intermediates/system/lib64/libandroid.so : $(PRODUCT_OUT)/obj/PACKAGING/pdk_fusion_intermediates/pdk_fusion.stampendif
endif

于是有以下修改

​
#1.预编译第三方库liba.so

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

 #要生成的库
LOCAL_MODULE := liba 

#默认选项
LOCAL_MODULE_TAGS := optional

#所属的类是动态库
LOCAL_MODULE_CLASS := SHARED_LIBRARIES 

#这个是生成的后缀,我在hisi上面是可以不用加这个的,但是高通上面必须要加这个,不然编译的出来的库不带so
LOCAL_MODULE_SUFFIX := .so 

#这个是第三方的库,我这里是添加了一个32位的库和64位的库
LOCAL_SRC_FILES_arm:= arm64-v8a/liba.so
LOCAL_SRC_FILES_arm64:= armeabi-v7a/liba.so

#32位、64位两者都编译
LOCAL_MULTILIB := both 
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(BUILD_PREBUILT)

#2.使用该库

include $(CLEAR_VARS)
LOCAL_MODULE := libmaintest
LOCAL_SRC_FILES := main.c
LOCAL_C_INCLUDES := $(LOCAL_PATH)

#链接上面预编译的共享库
LOCAL_SHARED_LIBRARIES := liba
include $(BUILD_SHARED_LIBRARY)

​

这么一修改之后就可以编译通过了,我猜测是我再高通的平台是64位的,但是也会用到32位的,所以就需要声明

LOCAL_SRC_FILES_arm:= arm64-v8a/liba.so
LOCAL_SRC_FILES_arm64:= armeabi-v7a/liba.so,并且声明LOCAL_MULTILIB := both 两个都编译

海思3798MV200是单32位,所以就按照第一个版本的Android.mk也能编译通过

你可能感兴趣的:(Android,编译)