My question is:
I have used ndk to develop some functions, when built, it created a .so file in libs/armeabi. Now I needed to add another .so file from others. I only copied the .so to my dir libs/armeabi, then built it. The .so file was missing and I only had mine.
I do not kown why it happens. Should I need to config my Android.mk file, or any else?
This happens because the libs folder is cleaned when you run the ndk-build!
Edit your Android.mk and add this:
include $(CLEAR_VARS)
LOCAL_MODULE := MYOTHERSO-prebuilt
LOCAL_SRC_FILES := [relative path to your other .so]/lib[change to your lib name].so
LOCAL_EXPORT_C_INCLUDES := [relative path to your other .so include files]
include $(PREBUILT_SHARED_LIBRARY)
You should store the .so file on another path (not on libs/armeabi folder) in order to reference it from your Android.mk. During ndk-build it will be copied to libs/armeabi folder.
Related
I am a android NDK newbie, I am trying to follow the documentation at https://developer.android.com/ndk/guides/prebuilts and I found it is unclear on how to setup the code.
So to declare a prebuilt library, it shows the first step:
Give the module a name. This name does not need to be the same as that
of the prebuilt library, itself.
Question 1: What does this mean by giving the module a name? rename the module folder? is this the folder usually called jni/? does it matter where this folder should be?
Question 2: If I don't find this jni/ folder, should I just add one by creating a new folder or right clicked on my project in android studio to add new module? I did the right click thing and there is no such module for native c++ libs.
the second step:
In the module's Android.mk file, assign to LOCAL_SRC_FILES ...
Question 3: Should I create the 1Android.mk` file by myself? if it is a must, why not have it generated automatically?
I then tried to follow the documentation at https://developer.android.com/studio/projects/add-native-code#new-project but there is no such Android.mk file created...
Anyway, in the Android.mk file it has something like this:
include $(CLEAR_VARS)
LOCAL_MODULE := foo-user
LOCAL_SRC_FILES := foo-user.c
LOCAL_SHARED_LIBRARIES := foo-prebuilt
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)
include $(BUILD_SHARED_LIBRARY)
Question 4: On the documentation page, where variables like PREBUILT_SHARED_LIBRARY BUILD_SHARED_LIBRARY is defined? can these path can be any place in the system as long I define it with full path?
Question 5: for LOCAL_PATH, I understand it is the local path where the Android.mk file locates, does that mean 1) the system will find this file automatically, 2) I can only have one folder with Android.mk if I want to use native libs?
I am importing a .aar file as a dependency in my android project and have added this to Android.mk file.
Now internally this file has dependency on some .so files which its not able to pick up automatically.
So I copied all the dependent .so files under the below structure,
app --> libs --> arm64-v8a --> test.so
|
| --> x86 --> test.so
And now I tried several combination of below config in Android.mk file, but the above test.so file is not getting added to the apk generated after building.
.
.
.
.
**include $(CLEAR_VARS)
ifneq (,$(filter $(TARGET_ARCH_ABI), armeabi-v7a x86 arm64-v8a x86_64))
LOCAL_MODULE := Some Name
LOCAL_SRC_FILES := sources/app/libs/$(TARGET_ARCH)/xyz.so //Path of of my local .so file
LOCAL_MODULE_CLASS := SHARED_LIBRARIES
include $(PREBUILT_SHARED_LIBRARY)**
I tried lot of googling and also referred Android Xref, but unable to figure out what I am missing.
Any leads?
LOCAL_PREBUILT_JNI_LIBS = libs/xxxx.so
See example:
https://github.com/ayufan-pine64/proprietary_vendor_google_atv/blob/master/atv/Android.mk
As the title is self-explanatory, I'd want to replace some files in system/bin/ and system/lib/ right before when the system.img is being generated.
To be more exact, I've got some prebuilt .so files which shuold be copied over the existing ones. For example libart.so. Please note that replacing art/ (source code of art) with the modified art source code is not an option.
To do this, which mk file should I investigate and put my replacing code into?
You may follow the same structure as the one proposed in this other question, but use this macro:
PRODUCT_COPY_FILES += \
device/common/my_bin.bin:cache/my_bin.bin \
device/common/my_so.so:system/art/my_so.so
This works as follows:
The file is copied from left to(:) right.
When the image is generated, in this case cache or system, it would be generated with the files contained in those folders.
As we have copied our files to that destination, when system.img is generated, our file will be contained.
Also, the order in which this files are copied, matters. For instance if you are copying a file that already exists, hence, you want it replaced. (As your libart.so), you would need YOUR call to that copy to be done before the default one.
For this, I recommend doing:
source build/envsetup.sh
lunch <<device>>
mgrep libart.so -> This does a grep through mk files, so you can see where the default copy is done.
Insert $(info whatever) calls in desired mk files to trace where is a good spot to add your PRODUCT_COPY_FILES.
It's not easy to answer where to add it. I have never modified libart.so myself, but I have copied many files this way and it works like a charm.
I hope it works for your case as well.
A quick dirty way to this:
Copy desired files to out(..)/system/lib or wherever after system.img has been built
make snod
Other way is using Android.mk. Create a new dir in device tree eg libart-prebuilt. Put here your lib, then create an Android.mk file with content
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libart-prebuilt
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE_PATH := $(TARGET_OUT)/lib
LOCAL_MODULE_STEM := libart
LOCAL_MODULE_SUFFIX := $(TARGET_SHLIB_SUFFIX)
LOCAL_SRC_FILES := libart.so
LOCAL_MODULE_CLASS := SHARED_LIBRARIES
include $(BUILD_PREBUILT)
Now just add libart-prebuilt to PRODUCT_PACKAGES in your device.mk
Also there's an option to add binary .so library as a product package, using PREBUILT_SHARED_LIBRARY option in .mk file: Include prebuilt shared library in Android AOSP or BUILD_PREBUILT https://stackoverflow.com/a/25682674/1028256
I need to copy a directory tree with XML files recursively to the out directory using Android.mk file
The directory structure is like this:
parent directory has three sub-directories each with an XML file. The parent directory also includes an Android.mk file.
The Android.mk file in parent has the following rules:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := file.xml
LOCAL_MODULE_TAGS := optional debug
LOCAL_MODULE_CLASS := ETC
LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)
LOCAL_SRC_FILES := file.xml
include $(BUILD_PREBUILT)
I need to define LOCAL_SRC_FILES and LOCAL_MODULE_PATH as the source and target directories.
Could someone please let me know how to do it?
Basically the issue here is the "LOCAL_MODULE" needs to be defined for each file in this case. And it needs to be unique. If we try to have the same name for all three subdirectories, make fails with an 'already defined' error.
As a workaround, I defined LOCAL_MODULE for each of the three files with unique filenames. I don't think there is any other option of resolving this.
As an aside, I ought to mention that it is possible to copy files by running cp command directly from make. Please see Copy multiple txt files in /system using Android.mk
However, please note that running shell commands directly from within Android.mk files is deprecated starting from Android N.
I have added ndk plugin in Eclipse.
I have imported a NDK project in Eclipse.
But projects requires external .h file from the system, so I have added that folder where .h files files resides by
right click on project-> c/C++ General->Paths and symbols->then click on
include and then click add and given path of that folder
also checked all configurations and all languages.
still when I build the project from Command prompt by moving to path where my project is , then ndk-build I am getting for .h file no such file or directory error.
How can I resolve this issue??
Please help...
Please refer to documentation of LOCAL_C_INCLUDES variable
what you did to add the reference the external headers was only for eclipse, so it correctly resolves all the symbols and files references.
You also need to properly add a reference to these .h inside your ndk configuration files, ie inside Android.mk.
Use LOCAL_C_INCLUDES := path/to/headers in your module, like in this sample Android.mk file:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := main.c
LOCAL_MODULE := mymodule
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../includes
include $(BUILD_SHARED_LIBRARY)
If your .h files are part of a prebuilt module your own module depends on, use LOCAL_EXPORT_C_INCLUDES inside the prebuilt module instead of LOCAL_C_INCLUDES inside yours.