Why modifying an unrelated module in Android.mk rebuilds all libraries? - android

I made a simple project in Android Studio.
It compiles with no errors, the editor throws no warnings and the app works fine.
BUT if I modify the Android.mk in my project and hit the build button it recompiles everything from scratch.
Since it's a small project it takes little time but I can see the wait becoming much longer.
This is my Android.mk:
MY_DIR := $(call my-dir)
LOCAL_PATH := ${MY_DIR}/src/main/cpp
EXTERNAL_PATH := ${MY_DIR}/../..
include $(CLEAR_VARS)
LOCAL_MODULE := cglm
LOCAL_C_INCLUDES := ${EXTERNAL_PATH}/cglm/include
LOCAL_SRC_FILES := $(wildcard ${EXTERNAL_PATH}/cglm/src/*.c)
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := main
LOCAL_SRC_FILES := main.c noop.c renderer.c triangle.c quad.c shaders.c
LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv3
LOCAL_STATIC_LIBRARIES := android_native_app_glue
include $(BUILD_SHARED_LIBRARY)
$(call import-module,android/native_app_glue)
Concrete steps that I took to observe the behaviour I described:
1. With the Android.mk as you see it, I build the app. Everything is compiled from scratch as it should.
2. I change some code inside shaders.c
3. I build the app. Only the shaders.c file is recompiled. Good.
4. I delete shaders.c from the LOCAL_SRC_FILES on Android.mk.
5. I build the app. First thing it does is recompiling the whole cglm module which wasn't even being used by the main module. Then it proceeds to recompile the whole main module as you can see in the Android.mk I showed you.
Why is this happening and can I prevent it from happening without switching to cmake nor using a prebuilt library?

Related

How do I add .cpp included files to the dependencies in android.mk?

In our Android project, we have several tens of C and CPP files which we #include through one container file for the build. The container file is listed in LOCAL_SRC_FILES, since that's what's compiled. However, when I edit one of the included .cpp files, that doesn't seem to trigger a build.
If I were in total control of the make file, I'd just include all those files as dependencies for the relevant compile step. However, android.mk handles making all those compile dependencies in general, so I don't know where to insert them.
I have a three CPP file in this path
..src\main\cpp
mainClass.cpp
native-handler.h
native-handler.cpp
And I include to in android.mk file like this
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libjson-c
LOCAL_SRC_FILES := ../prebuild/libjson/$(TARGET_ARCH_ABI)/libjson-c.a
LOCAL_EXPORT_C_INCLUDES := ../prebuild/include
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := libcurl
LOCAL_SRC_FILES := ../prebuild/libcurl/$(TARGET_ARCH_ABI)/libcurl.a
LOCAL_EXPORT_C_INCLUDES := ../prebuild/include
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := Native
LOCAL_SRC_FILES := ../cpp/mainClass.cpp ../cpp/native-handler.cpp
LOCAL_C_INCLUDES := ../cpp/native-handler.h ../prebuild/include/json-c/json.h ../prebuild/include/curl/curl.h
LOCAL_LDLIBS := -lz -llog -ljnigraphics
LOCAL_STATIC_LIBRARIES := libjson-c libcurl
include $(BUILD_SHARED_LIBRARY)
Android.mk offers some features that Android.bp/Soong deliberately avoids (e.g. include files from parent directories). The goal of Soong is to completely replace the Android.mk based build system eventually. So if you happen to find a solution that works with Android.mk files you might have the same problem again in a future Android release.
The Android build system will detect changes to all files specified (e.g. as LOCAL_SRC_FILES in Android.mk or srcs in Android.bp) and trigger a rebuild of the module. I guess you have to switch to adding all source files that your container file included to the module configuration.

Android - item in LOCAL_STATIC_LIBRARIES not being built

I'm having trouble getting the dependency module to build while building a static library in AOSP. Call this library A, it has a dependency on another static lib B.
A's Android.mk looks like this:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := A
LOCAL_SRC_FILES := <files...>
LOCAL_STATIC_LIBRARIES := B
include $(BUILD_STATIC_LIBRARY)
Individually B builds fine (mma).
Problem is when I build A, B is not being built. Instead I see this at output:
Export includes file: <...>/B/Android.mk -- out/<...>/STATIC_LIBRARIES/B_intermediates/export_includes
Can someone explain what does this line mean and, why is it not trying use B's Android.mk to properly build B?
I understand it's not ideal to package a static lib inside another, but here I'm more courious about why is it the build system not running through B's makefile, when it's clearly a dependency?
Thank you!
The build system ignores lots of irrelevant statements, LOCAL_STATIC_LIBRARIES being one of examples. They don't write every entry in LOCAL_STATIC_LIBRARIES as a dependency for libA.a. Instead, they interpret the Android.mk files to produce make rules for all targets, and if a dependency happens to show up, it will eventually get built, too.
Therefore, the easiest workaround for you would be to add a dummy shared library to your Android.mk, like
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := A
LOCAL_SRC_FILES := <files...>
include $(BUILD_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := dummyA
LOCAL_SRC_FILES := dummy.c
LOCAL_STATIC_LIBRARIES := B
include $(BUILD_SHARED_LIBRARY)
I am not sure if you can drop LOCAL_SRC_FILES completely. In terms of good old plain make files, the above is roughly equivalent to:
all: libA.a libdummyA.so
libdummyA.so: dummy.c libB.a
gcc -o $# dummy.c -lb
Alternatively, you can manually specify the dependency:
$(PRODUCT_OUT)/obj/STATIC_LIBRARIES/libA_intermediates/libA.a: $(PRODUCT_OUT)/obj/STATIC_LIBRARIES/libB_intermediates/libB.a
Re: export_includes message, it is the result of processing LOCAL_EXPORT_C_INCLUDES statement for libB.

Android NDK: Linking x86 shared library

I was given a shared library built on Linux x86, let's call it libA.so, and I want to use the function calls provided by this library SDK.
I am having issues building and have a few questions:
1) I will be able to build for x86, but will I be able to build for arm? I believe the answer is no, meaning I cannot run on a Nexus 5 for example.
2) The ndk-build complains of the #include that should be resolved by my LOCAL_SHARED_LIBRARIES. I am not sure why that is. My Android.mk is as follow:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := B
LOCAL_SRC_FILES := B.cpp
LOCAL_SHARED_LIBRARIES := A
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
Can someone please help me resolve my Android.mk? I don't understand why it is complaining about my include statement in B.cpp. Please let me know if I can run B in an arm environment, although the SDK I am relying on was built on x86.
If your shared library libA.so has been compiled for linux-x86, it will certainly not run on android x86 targets (mainly because it needs to be linked to Bionic C library instead of glibc), and absolutely not on android arm devices.
Then, to solve your second issue, if you can get properly compiled android shared libraries for your android targets, you would include your library this way:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := A
LOCAL_SRC_FILES := ../libA/prebuilts/$(TARGET_ARCH_ABI)/libA.so # path to libA .so file, depending on the target ABI.
LOCAL_EXPORT_C_INCLUDES := ../libA/includes # path to libA headers.
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := B
LOCAL_SRC_FILES := B.cpp
LOCAL_SHARED_LIBRARIES := A
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)

Android NDK/JNI: Building a shared library that depends on other shared libraries

I am writing an android app that wants to make JNI calls into a shared library built in using the NDK. The trick is this shared library calls functions provided by OTHER shared libraries. The other shared libraries are C libraries that have been compiled elsewhere.
Here's what I've tried:
My Environment:
I'm working in Eclipse. I've added native support and have a jni library. In that library I have my code and a \lib directory where I have copied my other .so files.
Attempt #1 Android.mk: Just telling it where the libs are
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := native_lib
LOCAL_SRC_FILES := native_lib.cpp
LOCAL_LDLIBS := -L$(SYSROOT)/../usr/lib -llog
LOCAL_LDLIBS += -L$(LOCAL_PATH)/lib/support_lib1
LOCAL_LDLIBS += -L$(LOCAL_PATH)/lib/support_lib2
include $(BUILD_SHARED_LIBRARY)
This builds just fine, but when I try to run I get errors indicating that dlopen(libnative_lib) failed because it couldn't load libsupport_lib1.
Coming here I found this:
Can shared library call another shared library?
which said that I needed to call load library on all necessary libraries. Great!
Attempt #2 Opening each library first
static {
System.loadLibrary("support_lib1");
System.loadLibrary("support_lib2");
System.loadLibrary("native_lib");
}
Again, this builds just fine, however when I run I get a new error:
couldn't load libsupport_lib1. findLibrary returned null.
Now we're getting somewhere. It must not be loading the libraries over to the target.
Attempt #3 Copying .so files into project/libs/armeabi
Didn't work. When Eclipse builds it deleted the files I dropped in there.
Attempt #4 Creating a new module for each library
So then I found this:
Android NDK: Link using a pre-compiled static library
It's about static libraries, but maybe I am having a similar problem. The gist is that I need to declare a module for each library. So my new Android.mk looks like this:
LOCAL_PATH := $(call my-dir)
#get support_lib1
include $(CLEAR_VARS)
LOCAL_MODULE := support_lib1
LOCAL_SRC_FILES := $(LOCAL_PATH)/lib/support_lib1.so
include $(BUILD_SHARED_LIBRARY)
#get support_lib2
include $(CLEAR_VARS)
LOCAL_MODULE := support_lib2
LOCAL_SRC_FILES := $(LOCAL_PATH)/lib/support_lib2.so
include $(BUILD_SHARED_LIBRARY)
#build native lib
include $(CLEAR_VARS)
LOCAL_MODULE := native_lib
LOCAL_SRC_FILES := native_lib.cpp
LOCAL_LDLIBS := -L$(SYSROOT)/../usr/lib -llog
LOCAL_LDLIBS += -L$(LOCAL_PATH)/lib/support_lib1
LOCAL_LDLIBS += -L$(LOCAL_PATH)/lib/support_lib2
include $(BUILD_SHARED_LIBRARY)
This builds! Even better, armeabi has the sos now! Even BETTER I get the following messages when I try to run it (telling me that support_lib1 and 2 were opened by LoadLibrary:
Trying to load lib /data/app-lib/com.example.tst/libsupport_lib1.so
added shared lib /data/app-lib/com.example.tst/libsupport_lib1.so
no JNI_OnLoad found in /data/app-lib/com.example.tst/libsupport_lib1.so, skipping init
but then...
dlopen failed: Could not locate symbol func_that_exists_in_libsupport_lib.so referenced by libnative_lib.so
Edit: Attempt 5: Use PREBUILT_SHARED_LIBRARY
So I found this:
How can i Link prebuilt shared Library to Android NDK project?
which seems to be exactly what I'm asking. Their answer seems to be 'don't use 'build_shared_library' but instead 'use PREBUILT_SHARED_LIBRARY
Okay, let's try.
LOCAL_PATH := $(call my-dir)
#get support_lib1
include $(CLEAR_VARS)
LOCAL_MODULE := support_lib1
LOCAL_SRC_FILES := $(LOCAL_PATH)/lib/support_lib1.so
include $(PREBUILT_SHARED_LIBRARY)
#get support_lib2
include $(CLEAR_VARS)
LOCAL_MODULE := support_lib2
LOCAL_SRC_FILES := $(LOCAL_PATH)/lib/support_lib2.so
include $(PREBUILT_SHARED_LIBRARY)
#build native lib
include $(CLEAR_VARS)
LOCAL_MODULE := native_lib
LOCAL_SRC_FILES := native_lib.cpp
LOCAL_LDLIBS := -L$(SYSROOT)/../usr/lib -llog
LOCAL_SHARED_LIBRARIES := support_lib1 support_lib2
include $(BUILD_SHARED_LIBRARY)
Build... fails! The build complains about missing symbols now.
Edit: Attempt 6: Flatten everything
So I went back to the prebuilts documentation in the NDK. It says:
Each prebuilt library must be declared as a single independent module to the build system. Here is a trivial example where we assume that the file "libfoo.so" is located in the same directory than the Android.mk below:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := foo-prebuilt
LOCAL_SRC_FILES := libfoo.so
include $(PREBUILT_SHARED_LIBRARY)
Notice that, to declare such a module, you really only need the following:
Give the module a name (here 'foo-prebuilt'). This does not need to correspond to the name of the prebuilt library itself.
Assign to LOCAL_SRC_FILES the path to the prebuilt library you are providing. As usual, the path is relative to your LOCAL_PATH.
Include PREBUILT_SHARED_LIBRARY, instead of BUILD_SHARED_LIBRARY, if you are providing a shared, library. For static ones, use PREBUILT_STATIC_LIBRARY.
A prebuilt module does not build anything. However, a copy of your prebuilt shared library will be copied into $PROJECT/obj/local, and another will be copied and stripped into $PROJECT/libs/.
So let's try flattening everything out to match the trivial example. I copied my libraries out of their cozy /lib folder and put them in the jni root. I then did this:
LOCAL_PATH := $(call my-dir)
#get support_lib1
include $(CLEAR_VARS)
LOCAL_MODULE := support_lib1
LOCAL_SRC_FILES := support_lib1.so
include $(PREBUILT_SHARED_LIBRARY)
#get support_lib2
include $(CLEAR_VARS)
LOCAL_MODULE := support_lib2
LOCAL_SRC_FILES := support_lib2.so
include $(PREBUILT_SHARED_LIBRARY)
#build native lib
include $(CLEAR_VARS)
LOCAL_MODULE := native_lib
LOCAL_SRC_FILES := native_lib.cpp
LOCAL_LDLIBS := -L$(SYSROOT)/../usr/lib -llog
LOCAL_SHARED_LIBRARIES := support_lib1 support_lib2
include $(BUILD_SHARED_LIBRARY)
and... same error. Moreover I'm most definitely NOT seeing library files getting copied to $PROJECT/obj/local.
sooooo.... now what?
Your problem is with the naming convention. NDK and Android insist on the shared library names to always begin with lib. Otherwise, the libraries will not be linked properly, and not copied to the libs/armeabi folder properly, and not installed on the device (copied to /data/data/package/lib directory properly.
If you rename support_lib1.so to libsupport_1.so and support_lib2.so to libsupport_2.so, and put these two files in jni/lib directory, then your Attempt #5 will work with minor change:
LOCAL_PATH := $(call my-dir)
#get support_lib1
include $(CLEAR_VARS)
LOCAL_MODULE := support_lib1
LOCAL_SRC_FILES := lib/libsupport_1.so
include $(PREBUILT_SHARED_LIBRARY)
#get support_lib2
include $(CLEAR_VARS)
LOCAL_MODULE := support_lib2
LOCAL_SRC_FILES := lib/libsupport_2.so
include $(PREBUILT_SHARED_LIBRARY)
#build native lib
include $(CLEAR_VARS)
LOCAL_MODULE := native_lib
LOCAL_SRC_FILES := native_lib.cpp
LOCAL_LDLIBS := -L$(SYSROOT)/../usr/lib -llog
LOCAL_SHARED_LIBRARIES := support_lib1 support_lib2
include $(BUILD_SHARED_LIBRARY)
BTW, I don't think you need this -L$(SYSROOT)/../usr/lib.
PS Don't forget to update the Java side, too:
static {
System.loadLibrary("support_lib1");
System.loadLibrary("support_lib2");
System.loadLibrary("native_lib");
}
Not sure if this is exactly where you are at, but here's what I know about these sorts of things.
Make each prebuilt libary its own separate Makefile. Multiple targets in Android.mk tends to get wonky. Sad.
Include each make file using $(call import-add-path) and $(call import-module)
Export as much as you can from the prebuilt's make files, using the LOCAL_EXPORT_ family of variables.
Prebuilt Shared Library Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := my_module_name
MY_LIBRARY_NAME := shared_library_name
### export include path
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
### path to library
LOCAL_SRC_FILES := libs/$(TARGET_ARCH_ABI)/lib$(MY_LIBRARY_NAME).so
### export dependency on the library
LOCAL_EXPORT_LDLIBS := -L$(LOCAL_PATH)/libs/$(TARGET_ARCH_ABI)/
LOCAL_EXPORT_LDLIBS += -l$(MY_LIBRARY_NAME)
include $(PREBUILT_SHARED_LIBRARY)
This is assuming that the prebuilt libaries live in a dir structure like this
+ SharedProjectFolderName
+--- Android.mk
+--- include/
+-+- libs/$(TARGET_ARCH_ABI)/
|- libshared_library_name.so
If you are not building for multiple ABI, I guess you can leave that bit out
The Project's Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := my_jni_module
## source files here, etc...
### define dependency on the other library
LOCAL_SHARED_LIBRARIES := my_module_name
include $(BUILD_SHARED_LIBRARY)
$(call import-add-path,$(LOCAL_PATH)/path/to/myLibraries/)
$(call import-module,SharedProjectFolderName)
$(call import-module,AnotherSharedProject)
I recommend you put all shared libraries in one folder. When you say $(call import-module,SharedProjectFolderName) it looks for a folder containing an Android.mk along the search path you told it (import-add-path)
By the way, you probably shouldn't specify LOCAL_LDLIBS := -L$(SYSROOT)/../usr/lib. It should be finding the proper libs from NDK by itself. Adding more linker paths will probably confuse it. The proper way is to export the linker paths as flags from the sub-modules.
ALSO, you can use ndk-build V=1 to get a ton of info on why it can't find paths, etc
The -L option gives the linker a directory path in which to look for libraries. The -l option gives the linker a library file name to link in. Library file names must begin with "lib". Your libraries should be named libsupport_lib1.so and libsupport_lib2.so. If you do that, then this is probably what you should do (replacing attempt #1):
LOCAL_LDLIBS := -L$(SYSROOT)/../usr/lib -llog -lsupport_lib1 -lsupport_lib2
LOCAL_LDLIBS += -L$(LOCAL_PATH)/lib
The linker will prefix the library name you specify using -l with "lib" and suffix it with ".so". (Why do you have -L$(SYSROOT)/../usr/lib?)
I believe that attempts #1 and #2 failed because you did not link your libraries into your executable - they are not mentioned in a -l option. By the way, you can verify this yourself. Unzip the .apk file and look in the lib directory and subdirectories. Are your .so files in there?
Looking at the error:
but then... dlopen failed: Could not locate symbol func_that_exists_in_libsupport_lib.so referenced by libnative_lib.so
Can you supply the entire message? dlopen() loads and links libraries into the running process.

How to deal with the LOCAL_STATIC_LIBRARIES in android .mk

I'm trying to port one of the android NDK samples (native-activity) to work with the vs-android plugging that lets me develop in MVS.
To do this I need to translate the Android.mk commands into something MVS can deal with, and I'm having trouble with one of them.
Android.mk :
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := native-activity
LOCAL_SRC_FILES := main.c
LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv1_CM
LOCAL_STATIC_LIBRARIES := android_native_app_glue
include $(BUILD_SHARED_LIBRARY)
$(call import-module,android/native_app_glue)
The lines of LOCAL_STATIC_LIBRARIES and $(call import-module,android/native_app_glue) obviously deal with the native_app_glue that is central to making the native_activity class do it's magic.
Any idea on how to translate this in terms of compile line options in vs-android, linker options of MVS or anything similar? Or at least what those two lines do?
Cheers,
Jaime
Got it to work.
As it seems to be creating a library (native_app_glue) from what the .mk says, I went ahead and compiled the native_app_glue library as another static library (.a) project in my MVS, and then added it to the project dependencies. This worked with no problems

Categories

Resources