How do I link static .a libraries in an Android Studio project?

asked 2018-09-21 10:44:21 -0500

axjensen gravatar image


I am having troubles linking the prebuild static libraries in an Android Studio project. I am not even sure if it is possible. Is it?

What have I tried? * I create a simple new android studio project with C++ support. (Using Android Studio 3.1.4, Android SDK 21, OpenCV 3.4.3 and NDK 18.0.5) * I import the OpenCV Java SDK. This seems to work fine. I can use this SDK * I copy the OpenSDK native static libs to the projects jniLibs folder * I try to, via the android projects native c++ configuration by cmake, to create a library and that library I try to link a opencv static lib file to. In CMakeLists.txt this looks like this:

# For more information about using CMake with Android Studio, read the
# documentation:

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.

             # Sets the library as a shared library.

             # Provides a relative path to your source file(s).
             src/main/cpp/native-lib.cpp )

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.

              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )

SET("OpenCV_DIR" "/home/aej/Downloads/OpenCV/OpenCV4Android/OpenCV-android-sdk/sdk/native/jni")

find_package(OpenCV REQUIRED)
message(STATUS "OpenCV libraries: ${OpenCV_LIBS}")
include_directories(${OpenCV_INCLUDE_DIRS}) # Not needed for CMake >= 2.8.11

add_library(static_lib_opencv_core STATIC IMPORTED)
set_target_properties(static_lib_opencv_core PROPERTIES IMPORTED_LOCATION /home/aej/projects/Android/NativePanorama/app/src/main/jniLibs/x86_64/libopencv_core.a)

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.

                       # Links the target library to the log library
                       # included in the NDK.


I just want to test, that I can use simple functionality from libopencv_core.a in a cpp file like this:

#include <jni.h>
#include <string>

#include <opencv2/core.hpp>

extern "C" JNIEXPORT jstring

        JNIEnv *env,
        jobject /* this */) {
    std::string hello = "Hello from C++";

    cv::Mat my_mat = cv::Mat();
    return env->NewStringUTF(hello.c_str());

Later I would like to use a different static library, libopencv_stitching.a, but for now I am just trying to set up with libopencv_core.a

When I try to compile the native library I get linking errors, like:

FAILED: : && /home/aej/Android/Sdk/ndk-bundle ...

edit retag flag offensive close merge delete