title: Android NDK - how to link a C++ library statically with another C++library
body: <p>I have a C++ lib called <strong>mylib</strong>, compiled with ndk-build of Android NDK r6b under CYGWIN. The resulting library file is called <strong>libmylib.so</strong>.</p>
  
  <p>I have a simple app called <strong>helloworld</strong> that includes the .hpp files of mylib and have to be linked with libmylib.so.</p>
  
  <p>I am able to compile helloworld, telling the linker to look for libmylib.so. The "strange" facts are: </p>
  
  <ol>
  <li>libmylib.so is not merged with libhelloworld.so, so i have to copy libmylib.so into the directory helloworld/libs/armeabi manually</li>
  <li>in the java code, i have to load explicitly mylib with System.loadLibrary and i have to load mylib BEFORE i load helloworld library (that is: <code>System.loadLibrary("mylib");
    System.loadLibrary("ndkfoo");</code>), otherwise the app crashes.</li>
  </ol>
  
  <p>If i do 1 and 2, the app works perfectly (tested both on the emulator and on a real device). I would like to know how can to force the linker to produce a libhelloworld.so merged with libmylib.so, so to avoid 1,2 and 3 as mentioned above.</p>
  
  <p>Thanks.</p>
  
  <p>Ps.</p>
  
  <p>The Android.mk of mylib is:</p>
  
  <pre><code>LOCAL_PATH := $(call my-dir) 
  include $(CLEAR_VARS)
  LOCAL_MODULE    := mylib
  LOCAL_SRC_FILES := mylib.cpp 
  include $(BUILD_SHARED_LIBRARY)
  #include $(BUILD_STATIC_LIBRARY)
  </code></pre>
  
  <p>If i use BUILD_SHARED_LIBRARY, i got libmylib.so as usual. If i use, instead, BUILD_STATIC_LIBRARY, no .so or .a is generated by ndk-build. I really would like to see a simple tutorial which explains how to build a JNI app (with JAVA + c++ code) that uses an external library written in C++ and placed in another directory (not the jni directory of the app).</p>
  
  <p>Pss.
  When i compile with BUILD_STATIC_LIBRARY mylib, i obtain the following (basically, the only file created are under the dir /mylib/libs/armeabi/ and are called gdb.setup and gdbserver):</p>
  
  <pre><code>$ /cygdrive/d/android/android-ndk-r6b/ndk-build NDK_LOG=1 V=1 -B
  Android NDK: NDK installation path auto-detected: '/cygdrive/d/android/android-n
  dk-r6b'
  Android NDK: GNU Make version 3.81 detected
  Android NDK: Host OS was auto-detected: windows
  Android NDK: Host CPU was auto-detected: x86
  Android NDK: HOST_TAG set to windows
  Android NDK: Host awk tool was auto-detected: awk
  Android NDK: Host awk test returned: Pass
  Android NDK:  Probing for 'cygpath' program
  Android NDK:  'cygpath' found as: /usr/bin/cygpath
  Using cygwin substitution rules:
  /cygdrive/d =&gt; D:
  /cygdrive/c =&gt; C:
  /cygdrive/D =&gt; D:
  /cygdrive/C =&gt; C:
  /usr/lib =&gt; C:/cygwin/lib
  /usr/bin =&gt; C:/cygwin/bin
  / =&gt; C:/cygwin
  Android NDK:  This NDK supports the following toolchains and target ABIs:
  Android NDK:    arm-linux-androideabi-4.4.3:  armeabi armeabi-v7a
  Android NDK:    x86-4.4.3:  x86
  Android NDK: Found platform root directory: /cygdrive/d/android/android-ndk-r6b/
  platforms
  Android NDK: Found supported platforms: android-3 android-4 android-5 android-8
  android-9
  Android NDK: PLATFORM android-3 supports: arm x86
  Android NDK:   ABI arm sysroot is: /cygdrive/d/android/android-ndk-r6b/platforms
  /android-3/arch-arm
  Android NDK:   ABI x86 sysroot is: /cygdrive/d/android/android-ndk-r6b/platforms
  /android-3/arch-x86
  Android NDK: PLATFORM android-4 supports: arm x86
  Android NDK:   ABI arm sysroot is: /cygdrive/d/android/android-ndk-r6b/platforms
  /android-4/arch-arm
  Android NDK:   ABI x86 sysroot is: /cygdrive/d/android/android-ndk-r6b/platforms
  /android-4/arch-x86
  Android NDK: PLATFORM android-5 supports: arm x86
  Android NDK:   ABI arm sysroot is: /cygdrive/d/android/android-ndk-r6b/platforms
  /android-5/arch-arm
  Android NDK:   ABI x86 sysroot is: /cygdrive/d/android/android-ndk-r6b/platforms
  /android-5/arch-x86
  Android NDK: PLATFORM android-8 supports: arm x86
  Android NDK:   ABI arm sysroot is: /cygdrive/d/android/android-ndk-r6b/platforms
  /android-8/arch-arm
  Android NDK:   ABI x86 sysroot is: /cygdrive/d/android/android-ndk-r6b/platforms
  /android-8/arch-x86
  Android NDK: PLATFORM android-9 supports: arm x86
  Android NDK:   ABI arm sysroot is: /cygdrive/d/android/android-ndk-r6b/platforms
  /android-9/arch-arm
  Android NDK:   ABI x86 sysroot is: /cygdrive/d/android/android-ndk-r6b/platforms
  /android-9/arch-x86
  Android NDK: Found stable platform levels: 3 4 5 8 9
  Android NDK: Found max platform level: 9
  Android NDK: Looking for AndroidManifest.xml in /cygdrive/d/android/workspace/my
  lib/jni
  Android NDK: Looking for AndroidManifest.xml in /cygdrive/d/android/workspace/my
  lib
  Android NDK:     Found it !
  Android NDK: Found project path: /cygdrive/d/android/workspace/mylib
  Android NDK: Parsing /cygdrive/d/android/workspace/mylib/jni/Application.mk
  Android NDK:   Found APP_PLATFORM=android-9 in /cygdrive/d/android/workspace/myl
  ib/default.properties
  Android NDK:   Defaulted to APP_BUILD_SCRIPT=/cygdrive/d/android/workspace/mylib
  /jni/Android.mk
  Android NDK: Application 'local' *is* debuggable
  Android NDK: Selecting debug optimization mode (app is debuggable)
  Android NDK: Adding import directory: /cygdrive/d/android/android-ndk-r6b/source
  s
  Android NDK: Building application 'local' for ABI 'armeabi'
  Android NDK: Using target toolchain 'arm-linux-androideabi-4.4.3' for 'armeabi'
  ABI
  Android NDK: Looking for imported module with tag 'cxx-stl/system'
  Android NDK:   Probing /cygdrive/d/android/android-ndk-r6b/sources/cxx-stl/syste
  m/Android.mk
  Android NDK:     Found in /cygdrive/d/android/android-ndk-r6b/sources/cxx-stl/sy
  stem
  Android NDK: Module 'mylib' has C++ sources
  rm -f /cygdrive/d/android/workspace/mylib/libs/armeabi/lib*.so /cygdrive/d/andro
  id/workspace/mylib/libs/armeabi-v7a/lib*.so /cygdrive/d/android/workspace/mylib/
  libs/x86/lib*.so
  rm -f /cygdrive/d/android/workspace/mylib/libs/armeabi/gdbserver /cygdrive/d/and
  roid/workspace/mylib/libs/armeabi-v7a/gdbserver /cygdrive/d/android/workspace/my
  lib/libs/x86/gdbserver
  rm -f /cygdrive/d/android/workspace/mylib/libs/armeabi/gdb.setup /cygdrive/d/and
  roid/workspace/mylib/libs/armeabi-v7a/gdb.setup /cygdrive/d/android/workspace/my
  lib/libs/x86/gdb.setup
  Gdbserver      : [arm-linux-androideabi-4.4.3] libs/armeabi/gdbserver
  mkdir -p /cygdrive/d/android/workspace/mylib/libs/armeabi
  install -p /cygdrive/d/android/android-ndk-r6b/toolchains/arm-linux-androideabi-
  4.4.3/prebuilt/gdbserver /cygdrive/d/android/workspace/mylib/libs/armeabi/gdbser
  ver
  Gdbsetup       : libs/armeabi/gdb.setup
  mkdir -p /cygdrive/d/android/workspace/mylib/libs/armeabi
  echo "set solib-search-path D:/android/workspace/mylib/obj/local/armeabi" &gt; /cyg
  drive/d/android/workspace/mylib/libs/armeabi/gdb.setup
  echo "directory D:/android/android-ndk-r6b/platforms/android-9/arch-arm/usr/incl
  ude D:/android/android-ndk-r6b/sources/cxx-stl/system/include D:/android/workspa
  ce/mylib/jni" &gt;&gt; /cygdrive/d/android/workspace/mylib/libs/armeabi/gdb.setup
  </code></pre>