title: Linking external libraries using NDK, Gradle & CMake in Android Studio
body: <p>I've come back to Android development after a gap and my old ANT based build chain no longer seem to function (that's a separate issue) with the latest SDK, so I'm trying to do things the new way, which is based around gradle and CMake.</p>
  
  <p>I have a number of pre-built static and dynamic 3rd party libraries that my project needs but I've been unable to link these successfully.<br>
  From what I can gather, these need to be specified in the '<em>CMakeLists.txt</em>' file rather than the '<em>build.gradle</em>' files but I'm new to both systems, so please correct me if I'm wrong.</p>
  
  <p>Here's what I've tried so far:</p>
  
  <ul>
  <li><p>first, I try to resolve the location of bullet. I've tried variants where I omit the 'lib' the '.a' and the 'armeabi-v7a' (hoping it'd pick up the right CPU variant) but none have worked</p>
  
  <pre><code> find_library(bullet_lib libBullet.a HINTS ${LIBBASE}bullet3/build3/Android/obj/local/armeabi-v7a)
  </code></pre>
  
  <p>I'm not sure how this differs from the libraries included in <code>target_link_libraries</code></p>
  
  <pre><code>add_library(bullet_lib STATIC IMPORTED)
  </code></pre>
  
  <p>this finds the built-in libraries fine but I get linker errors for 'bullet' and other 3rd party libs</p>
  
  <pre><code>target_link_libraries(my_project_name android log EGL GLESv2 bullet_lib) 
  </code></pre></li>
  </ul>
  
  <p><code>${LIBBASE}</code> is the base dir for the 3rd party libraries and seems to be OK but, here's another thing: CMake errors don't seem to appear in android studio! I can sort of work out where it's having problems by running <code>cmake .</code> in the folder that contains the '<em>CMakeLists.txt</em>' file but I'm not quite sure I'm calling that with the correct parameters: whatever Android Studio does with cmake and that file are a black box to me at this time.</p>
  
  <p>The only messages I can see in android studio are linker errors (it doesn't mention CMake errors about not finding the libraries, which is the cause of these linker errors:)</p>
  
  <pre><code>Build command failed.
  Error while executing process E:\prog\Android\cmake\3.6.4111459\bin\cmake.exe with arguments {--build E:\prog\anthracite\gradle\AnthracitePlayerAPI21\app\.externalNativeBuild\cmake\debug\x86_64 --target anthracite-lib}
  [1/1] Linking CXX shared library ..\..\..\..\build\intermediates\cmake\debug\obj\x86_64\libanthracite-lib.so
  FAILED: cmd.exe /C "cd . &amp;&amp; E:\prog\Android\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe  --target=x86_64-none-linux-android --gcc-toolchain=E:/prog/Android/ndk-bundle/toolchains/x86_64-4.9/prebuilt/windows-x86_64 --sysroot=E:/prog/Android/ndk-bundle/sysroot -fPIC -isystem E:/prog/Android/ndk-bundle/sysroot/usr/include/x86_64-linux-android -D__ANDROID_API__=21 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security  -std=c++11 -fexceptions -O0 -fno-limit-debug-info  -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a --sysroot E:/prog/Android/ndk-bundle/platforms/android-21/arch-x86_64 -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libanthracite-lib.so -o ..\..\..\..\build\intermediates\cmake\debug\obj\x86_64\libanthracite-lib.so @CMakeFiles/anthracite-lib.rsp  &amp;&amp; cd ."
  
  E:\prog\anthracite\src/CCmpPhysicsScene3D.cpp:237: error: undefined reference to 'btDbvtBroadphase::btDbvtBroadphase(btOverlappingPairCache*)'
  E:\prog\anthracite\src/CCmpPhysicsScene3D.cpp:240: error: undefined reference to 'btDefaultCollisionConfiguration::btDefaultCollisionConfiguration(btDefaultCollisionConstructionInfo const&amp;)'
  E:\prog\anthracite\src/CCmpPhysicsScene3D.cpp:242: error: undefined reference to 'btCollisionDispatcher::btCollisionDispatcher(btCollisionConfiguration*)'
  E:\prog\anthracite\src/CCmpPhysicsScene3D.cpp:243: error: undefined reference to 'btSequentialImpulseConstraintSolver::btSequentialImpulseConstraintSolver()'
  E:\prog\anthracite\src/CCmpPhysicsScene3D.cpp:246: error: undefined reference to 'btDefaultSoftBodySolver::btDefaultSoftBodySolver()'
  E:\prog\anthracite\src/CCmpPhysicsScene3D.cpp:247: error: undefined reference to 'btSoftRigidDynamicsWorld::btSoftRigidDynamicsWorld(btDispatcher*, btBroadphaseInterface*, btConstraintSolver*, btCollisionConfiguration*, btSoftBodySolver*)'
  E:\prog\anthracite\src/CCmpPhysicsScene3D.cpp:250: error: undefined reference to 'btDiscreteDynamicsWorld::btDiscreteDynamicsWorld(btDispatcher*, btBroadphaseInterface*, btConstraintSolver*, btCollisionConfiguration*)'
  E:\prog\anthracite\src/CCmpPhysJointHinge.cpp:117: error: undefined reference to 'btHingeConstraint::btHingeConstraint(btRigidBody&amp;, btVector3 const&amp;, btVector3 const&amp;, bool)'
  
  clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation)
  ninja: build stopped: subcommand failed.
  </code></pre>
  
  <p>And running 'cmake .' from the command line gives:</p>
  
  <pre><code>-- Selecting Windows SDK version 10.0.16299.0 to target Windows 10.0.17133.
  CMake Warning at CMakeLists.txt:447 (message):
    resolved libraries:
  
  
  CMake Warning at CMakeLists.txt:448 (message):
    e:/prog/libs/bullet3/build3/Android/obj/local/armeabi-v7a
  
  
  CMake Warning at CMakeLists.txt:449 (message):
    bullet_lib-NOTFOUND
  
  
  
  -- Configuring done
  -- Generating done
  -- Build files have been written to: E:/prog/anthracite/gradle/AnthracitePlayerAPI21/app
  </code></pre>
  
  <p>although, as I mentioned above, I'm not sure of the veracity of that output as the calling parameters to 'cmake' are likely to be quite different from inside Android Studio. (e.g it's defaulting to a windows build, so I'm not sure if it'd look for '.lib' libraries rather than '.a' or '.so')</p>
  
  <p>Also, I'm using these lines in my '<em>CMakeLists.txt</em>' file to report the status of the build:</p>
  
  <pre><code>message(WARNING "resolved libraries:")
  message(WARNING ${LIBBASE}bullet3/build3/Android/obj/local/armeabi-v7a)
  message(WARNING ${bullet_lib})
  </code></pre>
  
  <p>Anyway, I'm clearly missing something and I've not been able to find any clear guides regarding this. It seems a really simple and obvious thing to do (link a library) but it seems to be a massive pain. 
  I'd be grateful for any pointers.</p>