android 修改新增jar 路径

#平台
CPU: RK3288
OS: android 5.11

#需求
新增xxx.jar 并在系统启动后自动加载到启动环境中, 等级等同于framework.jar,参考 BOOTCLASSPATH 变量的设置:
cat /init.environ.rc

export BOOTCLASSPATH /system/my/path/my.jar:/system/framework/core-libart.jar:/system/framework/conscrypt.jar:/system/framework/okhttp.jar:/system/framework/core-junit.jar:/system/framework/bouncycastle.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/telephony-common.jar:/system/framework/voip-common.jar:/system/framework/ims-common.jar:/system/framework/mms-common.jar:/system/framework/android.policy.jar:/system/framework/apache-xml.jar

其中**/system/my/path/my.jar**为新增加的jar.

#实现思路
新增模块名为: my

###让系统能编译新增加的JAR
这部分较为简单:

git diff build/target/product/base.mk
diff --git a/build/target/product/base.mk b/build/target/product/base.mk
old mode 100644
new mode 100755
index 14200f2..67eeae7
--- a/build/target/product/base.mk
+++ b/build/target/product/base.mk
@@ -21,6 +21,7 @@ PRODUCT_PACKAGES += \
     appwidget \
     appops \
     am \
+    my \
     android.policy \
     android.test.runner \
     app_process \

###使用JAR包输出到指定目录

git diff frameworks/base/Android.mk
diff --git a/frameworks/base/Android.mk b/frameworks/base/Android.mk
index 5105ad5..e8a60b0 100755
--- a/frameworks/base/Android.mk
+++ b/frameworks/base/Android.mk
@@ -1055,6 +1055,19 @@ LOCAL_DX_FLAGS := --core-library
 
 include $(BUILD_JAVA_LIBRARY)
 
+
+java_dirs := java/my
+include $(CLEAR_VARS)
+LOCAL_MODULE_TAGS := optional
+LOCAL_NO_STANDARD_LIBRARIES := true
+LOCAL_JAVA_LIBRARIES := framework
+LOCAL_SRC_FILES := $(call all-java-files-under, $(java_dirs))
+LOCAL_MODULE := my
+LOCAL_DX_FLAGS := --core-library
+LOCAL_OUT_DIR :=/system/my/path
+include $(BUILD_JAVA_LIBRARY)
 
 # Include subdirectory makefiles
 # ============================================================

上面的步骤中, 添加了一个自定义的编译变量: LOCAL_OUT_DIR用于声明指定输出的目录

git diff build/core/base_rules.mk
diff --git a/build/core/base_rules.mk b/build/core/base_rules.mk
old mode 100644
new mode 100755
index 8c25897..7d8750a
--- a/build/core/base_rules.mk
+++ b/build/core/base_rules.mk
@@ -136,7 +136,6 @@ ifneq ($(my_module_relative_path),)
   my_module_path := $(my_module_path)/$(my_module_relative_path)
 endif
 endif # not LOCAL_UNINSTALLABLE_MODULE
 ifneq ($(strip $(LOCAL_BUILT_MODULE)$(LOCAL_INSTALLED_MODULE)),)
   $(error $(LOCAL_PATH): LOCAL_BUILT_MODULE and LOCAL_INSTALLED_MODULE must not be defined by component makefiles)
 endif
@@ -186,12 +185,14 @@ ifneq (true,$(LOCAL_UNINSTALLABLE_MODULE))
   endif
   endif
   LOCAL_INSTALLED_MODULE := $(my_module_path)/$(my_installed_module_stem)
+#AnsonCode LOCAL_OUT_DIR to make file output to special dir
+  ifdef LOCAL_OUT_DIR
+       LOCAL_INSTALLED_MODULE := $(PRODUCT_OUT)/$(LOCAL_OUT_DIR)/$(my_installed_module_stem)
+  endif
 endif

 # Assemble the list of targets to create PRIVATE_ variables for.
 LOCAL_INTERMEDIATE_TARGETS += $(LOCAL_BUILT_MODULE)

以上修改后, JAR包将会输出到system/my/path/my.jar

最好清空变量:

diff --git a/build/core/clear_vars.mk b/build/core/clear_vars.mk
old mode 100644
new mode 100755
index 52bca70..c4bfc52
--- a/build/core/clear_vars.mk
+++ b/build/core/clear_vars.mk
@@ -250,6 +250,7 @@ LOCAL_MODULE_STEM_32:=
 LOCAL_MODULE_STEM_64:=
 LOCAL_CLANG_32:=
 LOCAL_CLANG_64:=
+LOCAL_OUT_DIR:=
 
 # Trim MAKEFILE_LIST so that $(call my-dir) doesn't need to
 # iterate over thousands of entries every time.

###将JAR包添加到BOOTCLASSPATH变量中.

若默认输出到/system/framework 可以增加定义到以下变量:
生成BOOTCLASSPATH的源码在:
build/core/dex_preopt.mk 中的 PRODUCT_BOOTCLASSPATH

ifneq ($(DALVIK_VM_LIB),)

# list of boot classpath jars for dexpreopt
DEXPREOPT_BOOT_JARS := $(subst $(space),:,$(PRODUCT_BOOT_JARS))
DEXPREOPT_BOOT_JARS_MODULES := $(PRODUCT_BOOT_JARS)
PRODUCT_BOOTCLASSPATH := $(subst $(space),:,$(foreach m,$(DEXPREOPT_BOOT_JARS_MODULES),/system/framework/$(m).jar))

PRODUCT_BOOT_JARS 的定义在以下两个文件.
build/target/product/core_tiny.mk
build/target/product/core_minimal.mk

# The order matters
PRODUCT_BOOT_JARS := \
    core-libart \
    conscrypt \
    okhttp \
    core-junit \
    bouncycastle \
    ext \
    framework \
+   my \
    telephony-common \
    voip-common \
    ims-common \
    mms-common \
    android.policy \
    apache-xml \
    nullwebview \

即是说, 若需要像framework.jar一样加到BOOTCLASSPATH, 只需像上面代码, 加入my即可.
这样加出来的结果会是:

export BOOTCLASSPATH /system/framework/my.jar:/system/framework/core-libart.jar:/system/framework/conscrypt.jar:/system/framework/okhttp.jar:/system/framework/core-junit.jar:/system/framework/bouncycastle.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/telephony-common.jar:/system/framework/voip-common.jar:/system/framework/ims-common.jar:/system/framework/mms-common.jar:/system/framework/android.policy.jar:/system/framework/apache-xml.jar

PS:以下为非标准做法, 请小心使用

diff --git a/build/core/dex_preopt.mk b/build/core/dex_preopt.mk
old mode 100644
new mode 100755
index 8a19b66..cc72e68
--- a/build/core/dex_preopt.mk
+++ b/build/core/dex_preopt.mk
@@ -8,7 +8,14 @@ ifneq ($(DALVIK_VM_LIB),)
 # list of boot classpath jars for dexpreopt
 DEXPREOPT_BOOT_JARS := $(subst $(space),:,$(PRODUCT_BOOT_JARS))
 DEXPREOPT_BOOT_JARS_MODULES := $(PRODUCT_BOOT_JARS)
-PRODUCT_BOOTCLASSPATH := $(subst $(space),:,$(foreach m,$(DEXPREOPT_BOOT_JARS_MODULES),/system/framework/$(m).jar))
+PRODUCT_BOOTCLASSPATH := /system/my/path/my.jar:$(subst $(space),:,$(foreach m,$(DEXPREOPT_BOOT_JARS_MODULES),/system/framework/$(m).jar))
+
+DEXPREOPT_BOOT_JARS := my:$(subst $(space),:,$(PRODUCT_BOOT_JARS))
+
+DEXPREOPT_BOOT_JARS_MODULES := $(PRODUCT_BOOT_JARS) \
+                               my

大功告成:
cat /init.environ.rc

export BOOTCLASSPATH /system/my/path/my.jar:/system/framework/core-libart.jar:/system/framework/conscrypt.jar:/system/framework/okhttp.jar:/system/framework/core-junit.jar:/system/framework/bouncycastle.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/telephony-common.jar:/system/framework/voip-common.jar:/system/framework/ims-common.jar:/system/framework/mms-common.jar:/system/framework/android.policy.jar:/system/framework/apache-xml.jar

后记

android 7.1上新增jar包问题记录

  • 代码路径 frameworks/base/openapi/
  • Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_NO_STANDARD_LIBRARIES := true
LOCAL_JAVA_LIBRARIES := core-oj core-libart framework
LOCAL_SRC_FILES := $(call all-java-files-under, java)
LOCAL_MODULE := openapi
LOCAL_DX_FLAGS := --core-library
include $(BUILD_JAVA_LIBRARY)
  • build/target/product/base.mk
old mode 100644
new mode 100755
index 4d70664..b8fa3da
--- a/build/target/product/base.mk
+++ b/build/target/product/base.mk
@@ -36,6 +36,7 @@ PRODUCT_PACKAGES += \
     dnsmasq \
     dpm \
     framework \
+    openapi \
     fsck_msdos \
     hid \
  • 修改PRODUCT_BOOT_JARS, 让jar包写入BOOTCLASSPATH
    • build/target/product/core_minimal.mk
@@ -95,6 +95,7 @@ PRODUCT_BOOT_JARS := \
     bouncycastle \
     ext \
     framework \
+       openapi \
     telephony-common \
     voip-common \
     ims-common \
  • build/target/product/core_tiny.mk
@@ -92,6 +92,7 @@ PRODUCT_BOOT_JARS := \
     bouncycastle \
     ext \
     framework \
+       openapi \
     telephony-common \
     voip-common \
     ims-common \
  • build/core/pdk_config.mk
@@ -24,6 +24,7 @@ PDK_PLATFORM_JAVA_ZIP_JAVA_TARGET_LIB_DIR += \
   target/common/obj/JAVA_LIBRARIES/okhttp_intermediates \
   target/common/obj/JAVA_LIBRARIES/telephony-common_intermediates \
   target/common/obj/JAVA_LIBRARIES/voip-common_intermediates \
+  target/common/obj/JAVA_LIBRARIES/openapi_intermediates \
  • 编译后, 并没有产生可供第三方调用的jar包
rk3288_n712$ ls out/target/common/obj/JAVA_LIBRARIES/openapi_intermediates/
./
../
classes.dex
classes.jack
jack_res_jar_flags
jack-rsc/
jack-rsc.java-source-list
javalib.jar
with-local/

解决方法: make一遍

rk3288_n712$ ls out/target/common/obj/JAVA_LIBRARIES/openapi_intermediates/
./
../
classes/
classes.dex
classes-full-debug.jar
classes.jack
classes.jar
classes-jarjar.jar
jack_res_jar_flags
jack-rsc/
jack-rsc.java-source-list
javalib.jar
with-local/

classes.jar改下名, 就可提供给第三方开发商使用.

  • 验证是否写入 out/target/product/rk3288/root/init.environ.rc
export BOOTCLASSPATH /system/framework/core-oj.jar:/system/framework/core-libart.jar:/system/framework/conscrypt.jar:/system/framework/okhttp.jar:/system/framework/core-junit.jar:/system/framework/bouncycastle.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/openapi.jar:/system/framework/telephony-common.jar:/system/framework/voip-common.jar:/system/framework/ims-common.jar:/system/framework/apache-xml.jar:/system/framework/org.apache.http.legacy.boot.jar

你可能感兴趣的:(android 修改新增jar 路径)