'Cmake : Building .dylib to test Android JUnit tests

I am working on Android native project using C++ code built using CMake. Currently my app is running fine and native lib(.so) is successfully loaded using System.loadLibrary().

But I want to test JUnit tests on my mac in Android Studio. When running tests on mac, It throws error

myapp.dylib not found.

Here is my CMakeLists.txt :

cmake_minimum_required(VERSION 3.18.1)

# Declares and names the project.

project("myapplication")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin)
    
add_library( # Sets the name of the library.
        myapplication

        # Sets the library as a shared library.
        SHARED

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



find_library( # Sets the name of the path variable.
        log-lib

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

Here is my cpp file:

#include <jni.h>
#include <string>
#include "header.h"
#include<android/log.h>

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_myapplication_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {
    return env->NewStringUTF(hello.c_str());
}

To build .dylib when running cmake from command line, I get other errors like jni not found and liblog(android log library) not found.

Is there a way that I can build .dylib for mac OS in such a way that it can resolve android specific dependencies like jni and log library?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source