From 7cf9f28b5f064f309a5023eb73234cecb5cf3de9 Mon Sep 17 00:00:00 2001 From: Ying Wang Date: Fri, 18 Apr 2014 20:13:41 -0700 Subject: [PATCH] Support to extract JNI libs from prebuilt APK Use LOCAL_PREBUILT_JNI_LIBS to install prebuilt JNI libraries extracted from the prebuilt apk, or prebuilts as source, to the app specific lib path. LOCAL_PREBUILT_JNI_LIBS accepts 2 kinds of files: - Files like @path/to/libfoo.so (path inside the apk) are JNI libs extracted from the prebuilt apk. In this case, all embedded JNI libs inside the prebuilt apk are stripped. - Files like path/to/libfoo.so (path relative to LOCAL_PATH) are prebuilts in the source tree. Those prebuilt JNI libs are not defined as modules in the build system, so this works around possible module name conflict. Bug: 13170859 Change-Id: I91bb844cc11b3621a85733bc7e8910f168957ef0 --- core/clear_vars.mk | 1 + core/install_jni_libs.mk | 42 ++++++++++++++++++++++++++++++++++++--- core/prebuilt_internal.mk | 3 +++ 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/core/clear_vars.mk b/core/clear_vars.mk index 1dd5cfe56..be1def1aa 100644 --- a/core/clear_vars.mk +++ b/core/clear_vars.mk @@ -88,6 +88,7 @@ LOCAL_COMPRESS_MODULE_SYMBOLS:= LOCAL_STRIP_MODULE:= LOCAL_JNI_SHARED_LIBRARIES:= LOCAL_JNI_SHARED_LIBRARIES_ABI:= +LOCAL_PREBUILT_JNI_LIBS:= LOCAL_JAR_MANIFEST:= LOCAL_INSTRUMENTATION_FOR:= LOCAL_APK_LIBRARIES:= diff --git a/core/install_jni_libs.mk b/core/install_jni_libs.mk index 464a2a20e..b9e70b140 100644 --- a/core/install_jni_libs.mk +++ b/core/install_jni_libs.mk @@ -5,9 +5,11 @@ # rs_compatibility_jni_libs (from java.mk) # my_module_path (from base_rules.mk) # partition_tag (from base_rules.mk) +# my_prebuilt_src_file (from prebuilt_internal.mk) # # Output variables: -# jni_shared_libraries, jni_shared_libraries_abi, if we are going to embed the libraries into the apk. +# jni_shared_libraries, jni_shared_libraries_abi, if we are going to embed the libraries into the apk; +# my_extracted_jni_libs, if we extract jni libs from prebuilt apk. # jni_shared_libraries := \ @@ -33,6 +35,10 @@ ifeq ($(filter $(TARGET_OUT)/% $(TARGET_OUT_VENDOR)/% $(TARGET_OUT_OEM)/%, $(my_ my_embed_jni := true endif +# App-specific lib path. +my_app_lib_path := $($(LOCAL_2ND_ARCH_VAR_PREFIX)TARGET$(partition_tag)_OUT_SHARED_LIBRARIES)/$(basename $(LOCAL_INSTALLED_MODULE_STEM)) +my_extracted_jni_libs := + ifdef my_embed_jni # App explicitly requires the prebuilt NDK stl shared libraies. # The NDK stl shared libraries should never go to the system image. @@ -74,12 +80,42 @@ my_leading_separator := ; else my_leading_separator := endif -my_app_lib_path := $($(LOCAL_2ND_ARCH_VAR_PREFIX)TARGET$(partition_tag)_OUT_SHARED_LIBRARIES)/$(basename $(LOCAL_INSTALLED_MODULE_STEM)) $(LOCAL_INSTALLED_MODULE): PRIVATE_POST_INSTALL_CMD += \ $(my_leading_separator)mkdir -p $(my_app_lib_path) \ $(foreach lib, $(my_jni_filenames), ;ln -sf ../$(lib) $(my_app_lib_path)/$(lib)) # Clear jni_shared_libraries to not embed it into the apk. jni_shared_libraries := -endif # $(jni_shared_libraries) not empty +endif # $(jni_shared_libraries) not empty endif # my_embed_jni + +ifdef LOCAL_PREBUILT_JNI_LIBS +# Install prebuilt JNI libs to the app specific lib path. +# Files like @path/to/libfoo.so (path inside the apk) are JNI libs extracted from the prebuilt apk; +# Files like path/to/libfoo.so (path relative to LOCAL_PATH) are prebuilts in the source tree. +my_extracted_jni_libs := $(patsubst @%,%, \ + $(filter @%, $(LOCAL_PREBUILT_JNI_LIBS))) +ifdef my_extracted_jni_libs +ifndef my_prebuilt_src_file +$(error No prebuilt apk to extract prebuilt jni libraries $(my_extracted_jni_libs)) +endif +# We use the first jni lib file as dependency. +my_installed_prebuilt_jni := $(my_app_lib_path)/$(notdir $(firstword $(my_extracted_jni_libs))) +$(my_installed_prebuilt_jni): PRIVATE_JNI_LIBS := $(my_extracted_jni_libs) +$(my_installed_prebuilt_jni): $(my_prebuilt_src_file) + @echo "Extract JNI libs ($@ <- $<)" + @mkdir -p $(dir $@) + $(hide) unzip -j -o -d $(dir $@) $< $(PRIVATE_JNI_LIBS) && touch $@ + +$(LOCAL_INSTALLED_MODULE) : | $(my_installed_prebuilt_jni) +endif + +my_prebulit_jni_libs := $(addprefix $(LOCAL_PATH)/, \ + $(filter-out @%, $(LOCAL_PREBUILT_JNI_LIBS))) +ifdef my_prebulit_jni_libs +$(foreach lib, $(my_prebulit_jni_libs), \ + $(eval $(call copy-one-file, $(lib), $(my_app_lib_path)/$(notdir $(lib))))) + +$(LOCAL_INSTALLED_MODULE) : | $(addprefix $(my_app_lib_path)/, $(notdir $(my_prebulit_jni_libs))) +endif +endif # LOCAL_PREBULT_JNI_LIBS diff --git a/core/prebuilt_internal.mk b/core/prebuilt_internal.mk index fcd3e55b1..6fe9818a2 100644 --- a/core/prebuilt_internal.mk +++ b/core/prebuilt_internal.mk @@ -170,6 +170,9 @@ endif # LOCAL_DEX_PREOPT # Sign and align non-presigned .apks. $(built_module) : $(my_prebuilt_src_file) | $(ACP) $(ZIPALIGN) $(SIGNAPK_JAR) $(transform-prebuilt-to-target) +ifdef my_extracted_jni_libs + $(hide) zip -d $@ 'lib/*.so' # strip embedded JNI libraries. +endif ifneq ($(LOCAL_CERTIFICATE),PRESIGNED) $(sign-package) endif