10

I tried to add a prebuilt APK to my Android build. The APK contains several shared libraries (*.so files). It compiles without problem, but I still get an error from the app indicating that the libraries cannot be found.

Why could this be?

Here is the android.mk code:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := apkwithso
LOCAL_SRC_FILES := $(LOCAL_MODULE).apk
LOCAL_REQUIRED_MODULES := libx liby libz
LOCAL_MODULE_CLASS := APPS
LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)
LOCAL_CERTIFICATE := PRESIGNED
include $(BUILD_PREBUILT)

libx, y, z are my libx.so, liby.so, and libz.so

I also tried to copy the .so manually from the APK to the out lib directories but it didn't work.

I am compiling with Android 4.1.2 for Galaxy Nexus Maguro.

CJBS
  • 15,147
  • 6
  • 86
  • 135
Cattivo
  • 742
  • 2
  • 7
  • 15

5 Answers5

18

I've had this problem myself, the reason is that when APK files are included in a build they are essentially bypassing the installation process. This installation process is the point at which any shared libraries are extracted from the apk and put in the appropriate place, ergo if that never happens the libraries will not be available. This is because packages that are built from source during the AOSP build either have their prebuilt shared libraries included during that build process or their libraries are also built from source, and in either case those libraries are put in the appropriate place.

For this reason in addition to the apk module itself you should add the following in the same .mk file:

include $(CLEAR_VARS)
LOCAL_MODULE := libapkwithso
LOCAL_SRC_FILES := lib/libapkwithso.so # or wherever your so is
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE_SUFFIX := .so
LOCAL_MODULE_CLASS := SHARED_LIBRARIES
include $(BUILD_PREBUILT)

Then add:

LOCAL_REQUIRED_MODULES := libapkwithso
Justin Buser
  • 2,813
  • 25
  • 32
  • Hi Justin, How to add more .so files, if the apk contains more than one library. – Vikash Kumar Aug 29 '13 at 10:19
  • @VikashKumar You must to create one BUILD_PREBUILT block like that for each .so. Just remember to call `include $(CLEAR_VARS)` at the beginning of each block. – Plinio.Santos Oct 14 '13 at 13:34
  • Our build uses a prebuilt APK with a native module. Lets call this foo.apk which includes local module libbar.so. Our APK is sourced from a "system_apps" directory in the source tree. We check in APK's rather than source code. This logic requires me to check in a native library and an apk, but the apk already has the native library. So this solution didn't work for me. Or am I doing something wrong? I want the library to be pulled from the APK and put into the right spot at build time. It doesn't seem to do that. – Cookster Jan 23 '14 at 22:18
  • Upon further reading it looks like this is correct. The native modules must be included in addition to the APK. Bummer, now I have 2+ items to track, rather than 1 apk. – Cookster Jan 23 '14 at 22:44
  • Please note that on doing `mm` with this change, the apk that is built with not contain the `libapkwithso.so` files. It will be instead be in the `/system` directory of target based on your configuration. So you cannot use the apk directly and instead have to flash a full build. – zeitgeist May 12 '22 at 14:53
8

BUILD_PREBUILT calls prebuild.mk script. The description of this script is the following:

Standard rules for copying files that are prebuilt

It does not tell that the application will be installed. So I think that is why your application cannot find the libraries.

The solution is to extract libraries from the package and copy them separately from the application. The details how to do this you can find here.

Yury
  • 20,618
  • 7
  • 58
  • 86
  • Not at all! I'm glad that I could help you! ) – Yury Feb 05 '13 at 09:41
  • 1
    When you use `BUILD_PREBUILT` you should explicitly specify either `LOCAL_MODULE_CLASS` or `LOCAL_MODULE_PATH` (cite from Embedded Android). – pevik May 26 '14 at 10:35
2

My Instructions is not about your error! But it may be helpful for similar error

If you try to copy prebuilt apk using:

PRODUCT_COPY_FILES += $(LOCAL_PATH)/prebuilt/filename.apk

and you getting this error:

Prebuilt apk found in PRODUCT_COPY_FILES: ... , use BUILD_PREBUILT instead!. Stop.

You can remove this check my modify the file build/core/MakeFile and comment this lines:

define check-product-copy-files
$(if $(filter %.apk, $(call word-colon, 2, $(1))),$(error \
    Prebuilt apk found in PRODUCT_COPY_FILES: $(1), use BUILD_PREBUILT instead!))
endef

by inserting # before each line

Benny
  • 2,233
  • 1
  • 22
  • 27
1

Files can also be be specified in aospSource:destinationOnImage format like this in the device-partial.mk when adding a custom vendor directory:

PRODUCT_COPY_FILES += \
    vendor/your-custom-vendor/your-device/proprietary/libx.so:system/lib/libx.so

You can add pretty much anything you like here (other than .apk files), and it will be copied to your image.

pevik
  • 4,523
  • 3
  • 33
  • 44
Justin Buser
  • 2,813
  • 25
  • 32
0

Too add multiple libraries to one apk one additional section (as in answer of Justin Buser) should be added for each separate library.

Michael
  • 1,505
  • 14
  • 26
  • This answer to a question of [Vikash Kumar](http://stackoverflow.com/users/835757/vikash-kumar) – Michael Oct 08 '13 at 12:54