Search code examples
androidcandroid-ndkshared

Android NDK and dropping in new shared libraries


I have a question regarding why I cannot simply drop in new shared libraries in Android without recompiling via NDK-build. Heres what i do:

Using Android Studio and the recent release of the Android NDK, I have been able to compile an app that uses a C code, which refers to a shared library. I compile the shared library with:

GCC := /xxx/AndroidStudioProjects/android-ndk-r10d/toolchains/x86-4.6/prebuilt/windows/bin/i686-linux-android-gcc.exe
GPP :=/xxx/AndroidStudioProjects/android-ndk-r10d/toolchains/x86-4.6/prebuilt/windows/bin/i686-linux-android-g++.exe
AR  := /xxx/AndroidStudioProjects/android-ndk-r10d/toolchains/x86-4.6/prebuilt/windows/bin/i686-linux-android-ar.exe

OPTIONS  :=\
 -ffunction-sections \
 -funwind-tables  \
 -DANDROID 

default: all
all: obj
    $(AR) r libmathadd.so mathadd.o

obj:
    $(GCC) $(OPTIONS) -c mathadd.c

This gives me my .so file. I now run my ndk-build with the Android.mk makefile:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE           := mathadd
LOCAL_SRC_FILES        :=./libmathadd.so
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_LDLIBS := -llog
LOCAL_MODULE    := ndk1
 LOCAL_SRC_FILES := native.c
 LOCAL_SHARED_LIBRARIES := mathadd
 include $(BUILD_SHARED_LIBRARY)

Following this, I have my ndk1 .so file and my mathadd .so which i then place in a folder directory:

app -> src -> jniLibs -> x86 -> libmathadd.so

and

app -> src -> jniLibs -> x86 -> libndk1.so

I compile and run on my emulator, and boom everything works!

Now heres my problem. I slightly modify my shared library source (mathadd.c). I recompile it using the first make file and drop it straight into the app tree (without rebuilding the native c code) . I rebuild the app in Studio, and run on my emulator, and notice that my modifications did not occur.

I am not changing my native source, nor the api between native and my shared library.

If I rebuild the native with the ndk-build script, and drag the new native ndk1 in, everything works again, including the new modifcations. It seems I need to rebuild source via ndk everytime I tweak a shared library...??

Any ideas?

UPDATE: I opened up my libndk .so library with a RE tool. Turns out the ndk compiler is statically adding the libmathadd code into the libndk, despite it being listed as a local shared library. Im curious if this is because the optimization is by default set for fastest speed, and the libmathadd code is simplistic math.


Solution

  • So libndk1.so has a dependency on libmath.so ? That would then be logical to rebuild it when you touch to libmath source.