title: How can I merge two Android.mk files when adding a library?
body: <p>I'm new with Android so I don't fully understand how to make Android.mk files. 
  My project was running fine, following a tutorial online where the Android.mk was provided. But later on I needed to add a missing library and the tutorial for that had another Android.mk, now I'm trying to merge the two together.</p>
  
  <p><strong>Here is my Android.mk :</strong></p>
  
  <pre><code>LOCAL_PATH := $(call my-dir)
  
  include $(CLEAR_VARS)
  
  #opencv
  OPENCVROOT:= C:\OpenCV-2.4.10-android-sdk
  OPENCV_CAMERA_MODULES:=on
  OPENCV_INSTALL_MODULES:=on
  OPENCV_LIB_TYPE:=SHARED
  include ${OPENCVROOT}/sdk/native/jni/OpenCV.mk
  
  LOCAL_SRC_FILES := com_example_adrien_ndkopencvtest4_OpencvNativeClass.cpp
  
  
  LOCAL_LDLIBS += -llog
  
  LOCAL_MODULE := MyOpencvLibs
  
  
  include $(BUILD_SHARED_LIBRARY)
  </code></pre>
  
  <p><strong>Here is the Android.mk I need to add :</strong> </p>
  
  <pre><code>LOCAL_PATH  := $(call my-dir)
  OPENCV_PATH := D:/CODE/OpenCV-2.4.10-android-sdk/sdk/native/jni
  
  include $(CLEAR_VARS)
  LOCAL_MODULE    := nonfree
  LOCAL_SRC_FILES := libnonfree.so
  include $(PREBUILT_SHARED_LIBRARY)
  
  include $(CLEAR_VARS)
  OPENCV_INSTALL_MODULES := on
  OPENCV_CAMERA_MODULES  := off
  include $(OPENCV_PATH)/OpenCV.mk
  
  LOCAL_C_INCLUDES :=             \
      $(LOCAL_PATH)               \
      $(OPENCV_PATH)/include
  
  LOCAL_SRC_FILES :=              \
      nonfree_jni.cpp
  
  LOCAL_MODULE := nonfree_demo
  LOCAL_CFLAGS := -Werror -O3 -ffast-math
  LOCAL_LDLIBS := -llog -ldl
  LOCAL_SHARED_LIBRARIES += nonfree
  
  include $(BUILD_SHARED_LIBRARY)
  </code></pre>
  
  <p>What I'm interested in in the last Android.mk is the libnonfree.so only, I don't need the cpp file.</p>
  
  <p><strong>Here is the structure of my project :</strong> </p>
  
  <p><a href="https://i.stack.imgur.com/I60wl.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/I60wl.jpg" alt="enter image description here"></a></p>
  
  <p><strong>EDIT :</strong> </p>
  
  <p>Thanks for your answers. I've done what was suggested and my Android.mk looks like that now : </p>
  
  <pre><code>LOCAL_PATH := $(call my-dir)
  include $(CLEAR_VARS)
  
  #opencv
  OPENCVROOT:= C:\OpenCV-2.4.10-android-sdk
  OPENCV_CAMERA_MODULES:=on
  OPENCV_INSTALL_MODULES:=on
  OPENCV_LIB_TYPE:=SHARED
  include ${OPENCVROOT}/sdk/native/jni/OpenCV.mk
  
  LOCAL_SRC_FILES := com_example_adrien_ndkopencvtest4_OpencvNativeClass.cpp
  LOCAL_LDLIBS += -llog
  LOCAL_SHARED_LIBRARIES += nonfree
  LOCAL_MODULE := MyOpencvLibs
  include $(BUILD_SHARED_LIBRARY)
  
  include $(CLEAR_VARS)
  LOCAL_MODULE    := nonfree
  LOCAL_SRC_FILES := libnonfree.so
  include $(PREBUILT_SHARED_LIBRARY)
  </code></pre>
  
  <p>But the problem remains. I've got "Undefined reference" to methods libnonfree.so should add. I assumed the problem was from the Android.mk but maybe it's from elsewhere then. </p>
  
  <p>I've added <code>System.loadLibrary("nonfree");</code> to my <code>MainACtivity</code> and </p>
  
  <pre><code>#include "opencv2/nonfree/nonfree.hpp"
  #include "opencv2/nonfree/features2d.hpp"
  </code></pre>
  
  <p>I don't know what I'm doing wrong. </p>
  
  <p><strong>EDIT 2 :</strong> </p>
  
  <p><strong>Here is the error :</strong> </p>
  
  <p><a href="https://i.stack.imgur.com/s4Dhw.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/s4Dhw.jpg" alt=""></a></p>
  
  <p><strong>Application.mk :</strong></p>
  
  <pre><code>APP_STL := gnustl_static
  APP_CPPFLAGS := -frtti -fexceptions
  APP_ABI := armeabi-v7a
  APP_PLATFORM := android-16
  </code></pre>
  
  <p>I didn't know where to set <code>arguments="V=1"</code> in my build.gradle</p>
  
  <p><strong>My build.gradle :</strong> </p>
  
  <pre><code>apply plugin: 'com.android.application'
  
  android {
      compileSdkVersion 25
      buildToolsVersion "25.0.2"
      defaultConfig {
          applicationId "com.example.adrien.ndkopencvtest4"
          minSdkVersion 15
          targetSdkVersion 25
          versionCode 1
          versionName "1.0"
          testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
      }
  
      sourceSets.main {
          //jniLibs.srcDirs = ['src/main/jniLibs']
          jni.srcDirs = [] //disable automatic ndk-build call
      }
      task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {
          commandLine "C:/Users/Adrien/AppData/Local/Android/sdk/ndk-bundle/ndk-build.cmd",
                  'NDK_PROJECT_PATH=build/intermediates/ndk',
                  'NDK_LIBS_OUT=src/main/jniLibs',
                  'APP_BUILD_SCRIPT=src/main/jni/Android.mk',
                  'NDK_APPLICATION_MK=src/main/jni/Application.mk'
      }
      tasks.withType(JavaCompile) {
          compileTask -&gt; compileTask.dependsOn ndkBuild
      }
      buildTypes {
          release {
              minifyEnabled false
              proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
          }
      }
  }
  
  dependencies {
      compile fileTree(include: ['*.jar'], dir: 'libs')
      androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
          exclude group: 'com.android.support', module: 'support-annotations'
      })
      compile 'com.github.wseemann:FFmpegMediaMetadataRetriever-armeabi-v7a:1.0.14'
      compile 'com.android.support:appcompat-v7:25.3.1'
      compile 'com.android.support.constraint:constraint-layout:1.0.2'
      testCompile 'junit:junit:4.12'
      compile project(':openCVLibrary2410')
  
  }
  </code></pre>
  
  <p><strong>EDIT 3 :</strong> </p>
  
  <p><strong>Results of "ndk-build V=1" :</strong></p>
  
  <pre><code>C:\Users\Adrien\AndroidStudioProjects\NDKOpencvTest4\app\src\main\jni&gt;ndk-build V=1
  
  [armeabi-v7a] SharedLibrary  : libMyOpencvLibs.so
  C:/Users/Adrien/AppData/Local/Android/sdk/ndk-bundle/build//../toolchains/llvm/preb
  uilt/windows-x86_64/bin/clang++.exe -Wl,-soname,libMyOpencvLibs.so -shared --sysroo
  t=C:/Users/Adrien/AppData/Local/Android/sdk/ndk-bundle/build//../platforms/android-
  16/arch-arm C:/Users/Adrien/AndroidStudioProjects/NDKOpencvTest4/app/src/main/obj/l
  ocal/armeabi-v7a/objs/MyOpencvLibs/com_example_adrien_ndkopencvtest4_OpencvNativeCl
  ass.o C:/Users/Adrien/AppData/Local/Android/sdk/ndk-bundle/build//../sources/cxx-st
  l/gnu-libstdc++/4.9/libs/armeabi-v7a/libgnustl_static.a -lgcc C:/Users/Adrien/Andro
  idStudioProjects/NDKOpencvTest4/app/src/main/obj/local/armeabi-v7a/libopencv_java.s
  o C:/Users/Adrien/AndroidStudioProjects/NDKOpencvTest4/app/src/main/obj/local/armea
  bi-v7a/libnonfree.so  -gcc-toolchain C:/Users/Adrien/AppData/Local/Android/sdk/ndk-
  bundle/build//../toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64 -no-c
  anonical-prefixes -target armv7-none-linux-androideabi -Wl,--fix-cortex-a8  -Wl,--b
  uild-id -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -Wl,--warn-sh
  ared-textrel -Wl,--fatal-warnings   -LC:/Users/Adrien/AppData/Local/Android/sdk/ndk
  -bundle/build//../platforms/android-16/arch-arm/usr/lib -llog -lc -lm -o C:/Users/A
  drien/AndroidStudioProjects/NDKOpencvTest4/app/src/main/obj/local/armeabi-v7a/libMy
  OpencvLibs.so
  C:/Users/Adrien/AndroidStudioProjects/NDKOpencvTest4/app/src/main/jni/com_example_a
  drien_ndkopencvtest4_OpencvNativeClass.cpp:214: error: undefined reference to 'cv::
  SURF::SURF(double, int, int, bool, bool)'
  C:/Users/Adrien/AndroidStudioProjects/NDKOpencvTest4/app/src/main/jni/com_example_a
  drien_ndkopencvtest4_OpencvNativeClass.cpp:222: error: undefined reference to 'cv::
  SURF::SURF()'
  C:/Users/Adrien/AndroidStudioProjects/NDKOpencvTest4/app/src/main/jni/com_example_a
  drien_ndkopencvtest4_OpencvNativeClass.cpp:204: error: undefined reference to 'VTT
  for cv::SURF'
  C:/Users/Adrien/AndroidStudioProjects/NDKOpencvTest4/app/src/main/jni/com_example_a
  drien_ndkopencvtest4_OpencvNativeClass.cpp:204: error: undefined reference to 'VTT
  for cv::SURF'
  C:/Users/Adrien/AndroidStudioProjects/NDKOpencvTest4/app/src/main/jni/com_example_a
  C:/Users/Adrien/AndroidStudioProjects/NDKOpencvTest4/app/src/main/jni/com_example_adrien_ndkopencvtest4_OpencvNativeClass.cpp:204: error: undefined
  reference to 'VTT for cv::SURF'
  C:/Users/Adrien/AndroidStudioProjects/NDKOpencvTest4/app/src/main/jni/com_example_adrien_ndkopencvtest4_OpencvNativeClass.cpp:449: error: undefined
  reference to 'cv::SURF::SURF(double, int, int, bool, bool)'
  C:/Users/Adrien/AndroidStudioProjects/NDKOpencvTest4/app/src/main/jni/com_example_adrien_ndkopencvtest4_OpencvNativeClass.cpp:457: error: undefined
  reference to 'cv::SURF::SURF()'
  clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation)
  make: *** [C:/Users/Adrien/AndroidStudioProjects/NDKOpencvTest4/app/src/main/obj/local/armeabi-v7a/libMyOpencvLibs.so] Error 1
  </code></pre>
  
  <p><strong>SOLUTION :</strong></p>
  
  <p>My libnonfree.so file was not correct, I downloaded another one. I also changed my <code>Application.mk</code> file to : </p>
  
  <pre><code>APP_STL := gnustl_static
  APP_CPPFLAGS := -frtti -fexceptions
  APP_ABI := armeabi-v7a
  APP_PLATFORM := android-16
  </code></pre>