【发布时间】:2016-10-02 23:08:32
【问题描述】:
我一直在尝试在 Android Studio 2.2 中构建和调试外部 Java/C++ 源库,但我无法弄清楚如何执行此操作或是否可行。
具体来说,我正在尝试使用 https://github.com/mapbox/mapbox-gl-native 库,但我怀疑任何 Java/C++ 库的解决方案都可能类似。在这种情况下,库包含一个 Makefile 和一个 CMakeLists.txt 文件。
我还没有看到 CMakeLists.txt 是如何使用的,但是我已经使用以下命令从源代码构建了 Mapbox 库:
BUILDTYPE=Debug make android
我使用 Android Studio 向导创建了一个项目,并选择了 C++ 选项,该选项创建了一个成功构建和调试的示例 .cpp:
// 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());
}
它还会创建一个 app/CMakeLists.txt 文件。
无论如何,我都在寻找可以构建和调试第 3 方 C++ 源库的方法。我最好的猜测是有一些方法可以修改向导生成的 app/CMakeLists.txt 文件来执行此操作,但我猜这是要做什么,我不知道该怎么做。
我认为这可能是正确的道路的原因是向导生成的 app/CMakeLists.txt 包含:
# 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
这似乎有效。我在想可能可以引用库 CMakeLists.txt 或在向导生成的 CMakeLists.txt 内的 Mapbox 源中找到的 Makefile。希望它构建的库是可调试的。或者,将其静态链接也是可以接受的,只要我可以调试到库中的 C++ 源代码。
谢谢。
【问题讨论】:
标签: c++ android-studio makefile cmake mapbox-gl