How to create a static library (.a file) in Android Studio 3.2 with CMake

out*_*ast 2 java-native-interface android cmake static-libraries android-ndk

Now i create a new project include c++ support in Android Studio 3.2, it has native-lib.cpp naturally, the CMakeLists.txt looks like this:

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).
    src/main/cpp/native-lib.cpp)
Run Code Online (Sandbox Code Playgroud)

now if i build this project, i can find libnative-lib.so in some "debug" directoies, that's OK, but i want static library ie .a files.

Change SHARED to STATIC won't generate those files, what else should i do?

The CMake docs does not mention other way than add_library().

我搜索的每个文档都只谈论SHARED/STATIC.

我错过了什么?

Ale*_*ohn 5

添加到@DanAlbert 的评论中,问题不在于单独的静态库有多大用处。问题在于,默认情况下,Android Studio 通过其 Gradle 插件不会构建作为STATIC库的CMake目标。

幸运的是,您可以明确指定要生成的目标,例如

android { defaultConfig { externalNativeBuild { cmake {
    targets "native_staticlib"
} } } }
Run Code Online (Sandbox Code Playgroud)

实际上,通过集成的externalNativeBuild构建静态库并没有太多附加值。如果从命令行运行cmake --build,则可以实现相同的目标。