Don't extract jni from prebuilt apks.

- We don't need LOCAL_PAGE_ALIGN_JNI_SHARED_LIBRARIES now, for we always
  page-align jni shared libraries and store them umcompressed.
- For prebuilt apks, we don't extract jni any more; Instead we always run
  uncompress-shared-libs on them.
- For apks built from source, we still install the jni separately, because
  that way multiple apks can share the same jni and it saves space.

With this change, for most prebuilt apks, we don't need to specify
LOCAL_PREBUILT_JNI_LIBS ("@lib/<abi>/foo.so") any more, for the build
system automatically replaces the embedded jni with uncompressed files;
But if a prebuilt is a fat apk (i.e. containing jni not needed by the
current product architecture), you still need LOCAL_PREBUILT_JNI_LIBS to
specify what jni to keep. Otherwise all embedded jni will be replaced with
uncompressed files, that wastes space.

Bug: 8076853
Change-Id: Icf07e0998ac3602e6e05e80fed836fbafca33e01
This commit is contained in:
Ying Wang 2015-05-01 14:02:26 -07:00
parent 069f1f8942
commit 3797466fbd
6 changed files with 28 additions and 52 deletions

View File

@ -20,7 +20,6 @@ LOCAL_UNSTRIPPED_PATH:=
LOCAL_MODULE_CLASS:=
LOCAL_MODULE_SUFFIX:=
LOCAL_PACKAGE_NAME:=
LOCAL_PAGE_ALIGN_JNI_SHARED_LIBRARIES:=
LOCAL_OVERRIDES_PACKAGES:=
LOCAL_EXPORT_PACKAGE_RESOURCES:=
LOCAL_MANIFEST_PACKAGE_NAME:=

View File

@ -2097,8 +2097,7 @@ $(hide) mkdir -p $(addprefix $(dir $@)lib/,$(PRIVATE_JNI_SHARED_LIBRARIES_ABI))
$(foreach abi,$(PRIVATE_JNI_SHARED_LIBRARIES_ABI),\
$(call _add-jni-shared-libs-to-package-per-abi,$(abi),\
$(patsubst $(abi):%,%,$(filter $(abi):%,$(PRIVATE_JNI_SHARED_LIBRARIES)))))
$(hide) (cd $(dir $@) && zip -r \
$(if $(filter true, $(PRIVATE_PAGE_ALIGN_JNI_SHARED_LIBRARIES)),-0,) $(notdir $@) lib)
$(hide) (cd $(dir $@) && zip -r -0 $(notdir $@) lib)
$(hide) rm -rf $(dir $@)lib
endef
@ -2144,21 +2143,22 @@ endef
define align-package
$(hide) mv $@ $@.unaligned
$(hide) $(ZIPALIGN) \
-f \
$(if $(filter true, $(PRIVATE_PAGE_ALIGN_JNI_SHARED_LIBRARIES)),-p,) \
-f -p \
4 \
$@.unaligned $@.aligned
$(hide) mv $@.aligned $@
endef
# Uncompress shared libraries embedded in an apk.
#
define uncompress-shared-libs
$(hide) rm -rf $(dir $@)/tmpworkdir
$(hide) mv $@ $@.compressed
$(hide) mkdir $(dir $@)/tmpworkdir
$(hide) unzip $@.compressed 'lib/*.so' -d $(dir $@)/tmpworkdir
$(hide) ( cd $(dir $@)/tmpworkdir && zip -D -r -0 ../$(notdir $@).compressed lib )
$(hide) mv $@.compressed $@
$(hide) rm -rf $(dir $@)/tmpworkdir
$(hide) if unzip -l $@ $(PRIVATE_EMBEDDED_JNI_LIBS) >/dev/null ; then \
rm -rf $(dir $@)uncompressedlibs && mkdir $(dir $@)uncompressedlibs; \
unzip $@ $(PRIVATE_EMBEDDED_JNI_LIBS) -d $(dir $@)uncompressedlibs && \
zip -d $@ 'lib/*.so' && \
( cd $(dir $@)uncompressedlibs && zip -D -r -0 ../$(notdir $@) lib ) && \
rm -rf $(dir $@)uncompressedlibs; \
fi
endef
define install-dex-debug

View File

@ -8,7 +8,7 @@
#
# Output variables:
# jni_shared_libraries, jni_shared_libraries_abi, jni_shared_libraries_with_abis if we are going to embed the libraries into the apk;
# extracted_jni_libs, if we extract jni libs from prebuilt apk.
# embedded_prebuilt_jni_libs, prebuilt jni libs embedded in prebuilt apk.
#
my_embed_jni :=
@ -27,7 +27,7 @@ jni_shared_libraries :=
jni_shared_libraries_abis :=
# jni_shared_libraries_with_abis is a list of <abi>:<path-to-the-built-jni-lib>
jni_shared_libraries_with_abis :=
extracted_jni_libs :=
embedded_prebuilt_jni_libs :=
#######################################
# For TARGET_ARCH
@ -51,7 +51,7 @@ jni_shared_libraries += $(my_jni_shared_libraries)
jni_shared_libraries_abis += $(my_jni_shared_libraries_abi)
jni_shared_libraries_with_abis += $(addprefix $(my_jni_shared_libraries_abi):,\
$(my_jni_shared_libraries))
extracted_jni_libs += $(my_extracted_jni_libs)
embedded_prebuilt_jni_libs += $(my_embedded_prebuilt_jni_libs)
# Include RS dynamically-generated libraries as well
# TODO: Add multilib support once RS supports generating multilib libraries.
@ -83,11 +83,11 @@ jni_shared_libraries += $(my_jni_shared_libraries)
jni_shared_libraries_abis += $(my_jni_shared_libraries_abi)
jni_shared_libraries_with_abis += $(addprefix $(my_jni_shared_libraries_abi):,\
$(my_jni_shared_libraries))
extracted_jni_libs += $(my_extracted_jni_libs)
embedded_prebuilt_jni_libs += $(my_embedded_prebuilt_jni_libs)
endif # my_add_jni
endif # TARGET_2ND_ARCH
jni_shared_libraries := $(strip $(jni_shared_libraries))
jni_shared_libraries_abis := $(sort $(jni_shared_libraries_abis))
jni_shared_libraries_with_abis := $(strip $(jni_shared_libraries_with_abis))
extracted_jni_libs := $(strip $(extracted_jni_libs))
embedded_prebuilt_jni_libs := $(strip $(embedded_prebuilt_jni_libs))

View File

@ -9,7 +9,7 @@
#
# Output variables:
# my_jni_shared_libraries, my_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.
# my_embedded_prebuilt_jni_libs, prebuilt jni libs embedded in prebuilt apk.
#
my_jni_shared_libraries := \
@ -19,7 +19,7 @@ my_jni_shared_libraries := \
# App-specific lib path.
my_app_lib_path := $(dir $(LOCAL_INSTALLED_MODULE))lib/$(TARGET_$(my_2nd_arch_prefix)ARCH)
my_extracted_jni_libs :=
my_embedded_prebuilt_jni_libs :=
ifdef my_embed_jni
# App explicitly requires the prebuilt NDK stl shared libraies.
@ -76,34 +76,10 @@ endif # $(my_jni_shared_libraries) not empty
endif # my_embed_jni
ifdef my_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 @lib/<abi>/libfoo.so (path inside the apk) are JNI libs embedded prebuilt apk;
# Files like path/to/libfoo.so (path relative to LOCAL_PATH) are prebuilts in the source tree.
my_extracted_jni_libs := $(patsubst @%,%, \
my_embedded_prebuilt_jni_libs := $(patsubst @%,%, \
$(filter @%, $(my_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
ifeq ($(LOCAL_CERTIFICATE),PRESIGNED)
$(warning Extracting files using LOCAL_PREBUILT_JNI_LIBS cannot be done while)
$(warning using LOCAL_CERTIFICATE:=PRESIGNED, as this would corrupt)
$(warning the APK or waste disk space. Instead, you should delete)
$(warning LOCAL_PREBUILT_JNI_LIBS and use LOCAL_PAGE_ALIGN_JNI_SHARED_LIBRARIES:=true)
$(warning This will allow loading of shared libraries directly from the APK,)
$(warning eliminating the need to separately extract them.)
$(error Failed to build: $(LOCAL_MODULE))
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
# prebuilt JNI exsiting as separate source files.
my_prebuilt_jni_libs := $(addprefix $(LOCAL_PATH)/, \

View File

@ -392,7 +392,6 @@ $(LOCAL_BUILT_MODULE): $(AAPT) | $(ZIPALIGN)
$(LOCAL_BUILT_MODULE): PRIVATE_JNI_SHARED_LIBRARIES := $(jni_shared_libraries_with_abis)
# PRIVATE_JNI_SHARED_LIBRARIES_ABI is a list of ABI names.
$(LOCAL_BUILT_MODULE): PRIVATE_JNI_SHARED_LIBRARIES_ABI := $(jni_shared_libraries_abis)
$(LOCAL_BUILT_MODULE): PRIVATE_PAGE_ALIGN_JNI_SHARED_LIBRARIES := $(LOCAL_PAGE_ALIGN_JNI_SHARED_LIBRARIES)
ifneq ($(TARGET_BUILD_APPS),)
# Include all resources for unbundled apps.
LOCAL_AAPT_INCLUDE_ALL_RESOURCES := true

View File

@ -195,14 +195,18 @@ endif
include $(BUILD_SYSTEM)/dex_preopt_odex_install.mk
#######################################
# Sign and align non-presigned .apks.
$(built_module) : PRIVATE_PAGE_ALIGN_JNI_SHARED_LIBRARIES := $(LOCAL_PAGE_ALIGN_JNI_SHARED_LIBRARIES)
# The embedded prebuilt jni to uncompress.
ifndef embedded_prebuilt_jni_libs
# No LOCAL_PREBUILT_JNI_LIBS, uncompress all.
embedded_prebuilt_jni_libs := 'lib/*.so'
endif
$(built_module): PRIVATE_EMBEDDED_JNI_LIBS := $(embedded_prebuilt_jni_libs)
$(built_module) : $(my_prebuilt_src_file) | $(ACP) $(ZIPALIGN) $(SIGNAPK_JAR)
$(transform-prebuilt-to-target)
ifneq ($(LOCAL_CERTIFICATE),PRESIGNED)
@# Only strip out files if we can re-sign the package.
ifdef extracted_jni_libs
$(hide) zip -d $@ 'lib/*.so' # strip embedded JNI libraries.
endif
ifdef LOCAL_DEX_PREOPT
ifneq (nostripping,$(LOCAL_DEX_PREOPT))
$(call dexpreopt-remove-classes.dex,$@)
@ -210,9 +214,7 @@ endif
endif
$(sign-package)
endif
ifeq ($(LOCAL_PAGE_ALIGN_JNI_SHARED_LIBRARIES),true)
$(uncompress-shared-libs)
endif
$(align-package)
###############################