Android7.1 编译踩的坑

1、使用BUILD_PREBUILT遇到的坑

使用BUILD_PREBUILT,一些属性的值必须指定,否则会报错
错误一:

build/core/base_rules.mk:122: *** vendor/icetech/preinstall/app/FEMSYS_Registration: LOCAL_MODULE_CLASS must contain exactly one word, not "".
build/core/ninja.mk:163: recipe for target 'out/build-e6e907c624e25ada6d4d9f66c976c642.ninja' failed
make: *** [out/build-e6e907c624e25ada6d4d9f66c976c642.ninja] Error 1
make: Leaving directory '/mnt/disk/binn/Work/a40i/android'

#### make failed to build some targets (1 seconds) ####

需要指定LOCAL_MODULE_CLASS的值

错误二:

build/core/prebuilt.mk:88: *** missing separator.
build/core/ninja.mk:163: recipe for target 'out/build-e6e907c624e25ada6d4d9f66c976c642.ninja' failed
make: *** [out/build-e6e907c624e25ada6d4d9f66c976c642.ninja] Error 1
make: Leaving directory '/mnt/disk/binn/Work/a40i/android'

#### make failed to build some targets (1 seconds) ####

这个错误提示比较隐蔽,通过和其他一些demo对比,发现必须指定LOCAL_MODULE_PATH的值

成功执行的案例脚本:

LOCAL_PATH := $(call my-dir)

# 把apk中的lib库编译到out目录下
# 该脚本可以再优化,执行该脚本时,自动解压apk,然后把so编译到out目录下
$(foreach p, $(notdir $(wildcard $(LOCAL_PATH)/armeabi-v7a/*.so)), \
	$(eval include $(CLEAR_VARS)) \
	$(eval LOCAL_MODULE_TAGS := optional) \
	$(eval LOCAL_SRC_FILES := armeabi-v7a/$(p))	\
	$(eval LOCAL_MODULE := $(shell basename $(p))) \
	$(eval LOCAL_MODULE_CLASS := SHARED_LIBRARIES) \
	$(eval LOCAL_MODULE_SUFFIX := .so) \
	$(eval LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)) \
	$(eval include $(BUILD_PREBUILT)) \
)

# apk编译到out目录下
include $(CLEAR_VARS)
LOCAL_MODULE := FEMSYS_Registration
LOCAL_DEX_PREOPT := false
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(LOCAL_MODULE).apk
LOCAL_CERTIFICATE := platform
LOCAL_MODULE_CLASS := APPS
LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)
LOCAL_MODULE_PATH := $(TARGET_OUT)/app
LOCAL_MULTILIB :=both
LOCAL_SHARED_LIBRARIES := $(foreach p,$(wildcard $(LOCAL_PATH)/armeabi-v7a/*.so),$(patsubst %.so,%,$(shell basename $(p))))
include $(BUILD_PREBUILT)

优化版本:自动解压apk,更新so文件

LOCAL_PATH := $(call my-dir)

# configure
# ----------------------
APK_MODULE := FEMSYS_Registration
CPU_ARCH := armeabi-v7a
# ----------------------
$(eval APK_EXTRACT_ROOT := $(TARGET_OUT_INTERMEDIATES)/TEMP)
$(eval LIBS_DIR := $(APK_EXTRACT_ROOT)/$(APK_MODULE)/lib/$(CPU_ARCH))
$(shell mkdir -p $(APK_EXTRACT_ROOT))
$(shell rm -fr $(APK_EXTRACT_ROOT)/$(APK_MODULE))
# 下面该命令不要分开执行,每个shell执行都是一个子shell,会出现同步情况
# 可能前一个命令未执行完,后一个命令已开始,出现编译错误
$(shell rm -fr $(LOCAL_PATH)/$(CPU_ARCH); \
		unzip $(LOCAL_PATH)/$(APK_MODULE).apk -d $(APK_EXTRACT_ROOT)/$(APK_MODULE) >> /dev/null; \
		cp -fr $(LIBS_DIR) $(LOCAL_PATH)/$(CPU_ARCH) \
)

$(foreach p, $(notdir $(wildcard $(LOCAL_PATH)/$(CPU_ARCH)/*.so)), \
	$(eval include $(CLEAR_VARS)) \
	$(eval LOCAL_MODULE_TAGS := optional) \
	$(eval LOCAL_SRC_FILES := $(CPU_ARCH)/$(p))	\
	$(eval LOCAL_MODULE := $(shell basename $(p) .so)) \
	$(eval LOCAL_MODULE_CLASS := SHARED_LIBRARIES) \
	$(eval LOCAL_MODULE_SUFFIX := .so) \
	$(eval LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)) \
	$(eval include $(BUILD_PREBUILT)) \
)

include $(CLEAR_VARS)
LOCAL_MODULE := $(APK_MODULE)
LOCAL_DEX_PREOPT := false
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(LOCAL_MODULE).apk
LOCAL_CERTIFICATE := platform
LOCAL_MODULE_CLASS := APPS
LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)
LOCAL_MODULE_PATH := $(TARGET_OUT)/app
LOCAL_MULTILIB :=both
LOCAL_SHARED_LIBRARIES := $(foreach p,$(wildcard $(CPU_ARCH)/*.so),$(patsubst %.so,%,$(shell basename $(p))))
include $(BUILD_PREBUILT)
2、java编译依赖第三方jar包

framework.jar使用如下方式依赖第三方jar

LOCAL_JAVA_LIBRARIES += hitechmwinf

在代码中import包中的类,依然提示找不到,对比其他jar包的使用方式,还需要添加

framework_docs_LOCAL_API_CHECK_JAVA_LIBRARIES += \
	hitechmwinf
3、编译不依赖framework.jar的jar包

当framework.jar编译时依赖jar包A.jar。如果A.jar编译中依赖了framework.java,编译framework.jar时会出现循环依赖的错误。
解决方法:在Android.mk中添加使用如下选项

LOCAL_JAVA_LIBRARIES := core-oj core-libart
LOCAL_NO_STANDARD_LIBRARIES := true
LOCAL_JARJAR_RULES := $(LOCAL_PATH)/jarjar-rules.txt

LOCAL_JARJAR_RULES把编译时引用的android包替换为非Android包,替换的包可以自行决定,例如

rule com.squareup.** com.android.@1 

注意:如果使用的包是java1.7版本的,还需添加

LOCAL_JAVA_LANGUAGE_VERSION := 1.7

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