当前位置: 首页 > 工具软件 > android lib > 使用案例 >

android mk学习-添加lib包

楮杰
2023-12-01

做系统开发,某些系统应用会引用第三方 jar 包,本文演示如何在 makefile 中导入第三方 jar 包。
原应用的 mk 如下

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
	
	# 应用名称
	LOCAL_PACKAGE_NAME := TestApp
	LOCAL_MODULE_TAGS := optional
	
	# 打系统签名
	LOCAL_CERTIFICATE := platform
	# 指定混淆文件
	LOCAL_PROGUARD_FLAG_FILES := proguard.cfg
	
	# 指定 src 目录
	LOCAL_SRC_FILES := \
    	$(call all-java-files-under, src)
	
	# rom 厂商的包,与本例关联不大
	LOCAL_JAVA_LIBRARIES := \
    	com.xxx.android

	LOCAL_PROPRIETARY_MODULE := true

include $(BUILD_PACKAGE)

jar 包放到 libs/ 目录下,修改如下


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

	LOCAL_PACKAGE_NAME := TestApp
	LOCAL_MODULE_TAGS := optional

	LOCAL_CERTIFICATE := platform
	LOCAL_PROGUARD_FLAG_FILES := proguard.cfg
	
	LOCAL_SRC_FILES := \
    	$(call all-java-files-under, src)
		
	# 指定每个 jar 包对应的名称,可以和 jar 包实际名称不同
	LOCAL_STATIC_JAVA_LIBRARIES := \
		mysdk \
		glide_3.7.0 \

	LOCAL_JAVA_LIBRARIES := \
    	com.mstar.android

	LOCAL_PROPRIETARY_MODULE := true

include $(BUILD_PACKAGE)

include $(CLEAR_VARS)

#指定 jar 包路径
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES += \
	mysdk:libs/opensdk.jar \
	glide_3.7.0:libs/glide-3.7.0.jar \

# 编译多个 jar
include $(BUILD_MULTI_PREBUILT)
include $(call all-makefiles-under,$(LOCAL_PATH))

如果要引用系统源码的包,可参考如下添加:

LOCAL_STATIC_JAVA_LIBRARIES := \
    android-support-v7-appcompat \
    android-support-design \
    android-support-v13 \
    android-support-v4 \
    android-support-v7-recyclerview \
	android-support-v7-recyclerview-res \

编译烧录,测试OK。如果碰到 crash ,可能是混淆导致的,可参照 jar 包提供商添加对应的混淆规则,修改指定的混淆文件 LOCAL_PROGUARD_FLAG_FILES := proguard.cfg

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class * extends android.os.Binder

# 不混淆 aidl 相关类
-keep class * implements android.os.IInterface
-keep class * implements android.os.Parcelable {
	public static final android.os.Parcelable$Creator *;
}
# 不混淆 SDK 相关类
-keep class com.xxxxxx.yyyy.open.** { *;}
-keep public class * extends android.app.Service
 类似资料: