0

I've been attempting to build and debug an external Java/C++ source library inside Android Studio 2.2 but I am unable to figure out how to do this or if it is possible.

In specific, I am attempting to use the https://github.com/mapbox/mapbox-gl-native library, but I suspect the solution may be similar for any Java/C++ library. In this case the library contains a Makefile and a CMakeLists.txt file.

I have not seen how the CMakeLists.txt is used, but I have built the Mapbox library from source using the command:

BUILDTYPE=Debug make android

I have created a project using the Android Studio wizard and chose the C++ option which creates a sample .cpp that successfully builds and debugs:

// native-lib.cpp

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

extern "C"
jstring
Java_com_example_kea_mapboxtest_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}

It also create an app/CMakeLists.txt file.

I am looking for anyway I can build and debug a 3rd party C++ source library. My best guess is that there is some way to modify the app/CMakeLists.txt file generated by the wizard to do this, but I'm guessing this is what to do and I can't figure out how.

The reason I think this might be the correct road to take is that the wizard generated app/CMakeLists.txt contains:

# 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 it for you.
# Gradle automatically packages shared libraries with your APK.

    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/native-lib.cpp

which seems to work. I was thinking it might be possible to reference the library CMakeLists.txt or the Makefile found in the Mapbox source inside the wizard generated CMakeLists.txt. The hope is the library it builds will be debuggable. Alternatively having it statically linked would also be acceptable, so long as I could debug into the C++ source in the library.

Thanks.

Mitch
  • 1,716
  • 3
  • 25
  • 41

1 Answers1

0

Ordinarily, I'd suggest using add_subdirectory() to first bring map box into your build and then target_link_libraries() to link your native-lib target to it, something like this:

add_subdirectory(path/to/mapbox/src mapbox)
add_library(native-lib SHARED src/main/cop/native-lib.cpp)
target_link_libraries(native-lib PUBLIC mbgl-core)

Unfortunately, the mapbox project's CMakeLists.txt file assumes it is the top level of the build, so that approach won't work without some hackery on your part (the problem is its use of CMAKE_SOURCE_DIR in its top level CMakeLists.txt file). An alternative might be to use ExternalProject instead. This is less convenient but more flexible in that it allows you to bring in builds from just about any sort of external project, building such projects in their own sandbox which your main build then uses for linking, etc. It also has the advantage that it can take care of downloading the source for mapbox for you too, so you don't have to add it directly to your own project's sources. Rather than repeat it all here, I'll refer you to the following links for existing questions and answers which should point you in the right direction for using ExternalProject.

If that's still not clear enough, let me know in the comments or start a chat and I'll try to clarify this answer further for you.

Community
  • 1
  • 1
Craig Scott
  • 9,238
  • 5
  • 56
  • 85
  • Thanks Craig. I'm totally new to CMake. I ordered a book to see if it would help, but that is a few weeks from arriving. I'm not finding much online and even less with respect to debugging in Android Studio. To clarify the OP, I have built the library using Make and created a project using it, but the debugger wouldn't go into the C++, so I'm not convinced that even if I understood your post that it would solve the problem after reading the links you sent. Nice that the library updates, but debugging with AS is really the problem. Let me read more and then I'll take you up on the chat offer. – Mitch Oct 03 '16 at 00:36
  • Okay, I've made a few attempts but everything does nothing or in some cases crashes Android Studio. The more I look at your suggestion, the less I think it will work. The reason is that it appears your "sandbox" will still loose the debug info. Of course being totally new I'm just guessing at this. I'd be happy to give it a go if you think this might allow linking. I would need more assistance in understanding the CMake commands to proceed. Thanks. – Mitch Oct 03 '16 at 02:18
  • Discussion moved to chat [here](http://chat.stackoverflow.com/rooms/124759/cmake-and-mapbox-issue) – Craig Scott Oct 03 '16 at 06:42