Sylfo
Sylfo

Reputation: 667

How to dependency to Android native code using CMake?

I am currently writing JNI code for an Android application to use some legacy native code. I chose to use CMake to give it a try (this is my first time using it).

I would like to serialize a quite complex C structure to JSON format using Jansson library. This would to make it more simple to expose it to Java.

Here is my question: How is it possible to import Jansson in my project as a dependency for my own code ?

I tried to import Jansson in my own sources and use a add_subdirectory clause to make it build. As a result, I can see some intermediates CMake files in the output but no actual compiled file.

Here is what my CMake file looks like :

cmake_minimum_required(VERSION 3.4.1)

include_directories(
             src/main/cpp/xxx/Include
             )

add_subdirectory("src/main/cpp/jansson")

add_library( # Sets the name of the library.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             # Associated headers in the same location as their source
             # file are automatically included.
             src/main/cpp/xxx/file1.c
             src/main/cpp/xxx/file2.c
             src/main/cpp/xxx/file3.c
             src/main/cpp/xxx/file4.c
             src/main/cpp/native-lib.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 )

target_link_libraries( # Specifies the target library.
                       native-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

Any help will be welcomed !!

Upvotes: 1

Views: 2018

Answers (1)

Sylfo
Sylfo

Reputation: 667

Okay, so I was just missing to declare the dependency of my library on Jansson. This is made by adding its project name to the existing target_link_library clause.

target_link_libraries( # Specifies the target library.
                       native-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       jansson
                       ${log-lib} )

In case someone else (well maybe my future self) stumbles on the same problem, here are all the necessary steps to add a native dependency in an Android CMake file:

  • Find a library already supporting CMake
  • Import library code in your project (I used git submodule)
  • Add add_subdirectory clause that points to the folder containing the library CMake file
  • Add include files to include_directories (hopefully, the imported library set a variable for that, such as JANSSON_INCLUDE_DIRS in my case
  • Add dependency by adding the library project name to the target_link_libraries clause

Once this is done: poof ! magic ! everything works fine !

Upvotes: 2

Related Questions