如何使用 CMake 在 Android Studio 3.2 中创建静态库(.a 文件) [英] How to create a static library (.a file) in Android Studio 3.2 with CMake

查看:96
本文介绍了如何使用 CMake 在 Android Studio 3.2 中创建静态库(.a 文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我在 Android Studio 3.2 中创建了一个包含 c++ 支持的新项目,它自然具有 native-lib.cppCMakeLists.txt 如下所示:

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)

现在如果我构建这个项目,我可以在一些调试"目录中找到 libnative-lib.so,没关系,但我想要静态库,即 .a文件.

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.

SHARED 改成 STATIC 不会生成这些文件,我应该怎么做?

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

CMake 文档没有提及 add_library() 以外的其他方式.

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

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

Every docs i searched only talk about SHARED/STATIC.

我错过了什么?

推荐答案

首先要感谢楼上各位,没有你们的启发,我无法得到这个答案.

First, i want to thank all guys above, without your inspiration, i cannot get this answer.

我把这个答案写下来不是因为这些答案是错误的,我只是想帮助像我这样的新手,一步一步来,所有细节都应该知道.

I write this answer down NOT becase those answers are wrong, i just want to help newbies like me, setp by step, with all details should know.

这个答案在 Android Studio 3.2.1 中用 CMake 3.4.1 证明,我三重检查了.

This answer was proved in Android Studio 3.2.1 with CMake 3.4.1, i triple checked.

现在,如果您创建一个支持 C++ 的新项目,请单击下一步";一路走来,你会得到一个CMakeLists.txt,应该是这样的:

Now if you create a new project with C++ support, click "next" all the way, you will get a CMakeLists.txt, whick should looks like :

cmake_minimum_required(VERSION 3.4.1)

add_library( 
        native-lib
        SHARED
        src/main/cpp/native-lib.cpp)

find_library( 
        log-lib
        log)

target_link_libraries( 
        native-lib
        ${log-lib})

SHARED改为STATIC不会输出任何.a文件,你什么也得不到,甚至没有.so 文件(当然,这个文件中没有添加 SHARED 库).

Change SHARED to STATIC won't output any .a file, you will get nothing, even no .so file( of course, there is no SHARED library be added in this file).

这是Gradle/Android Studio的错",上面的答案已经提到了,如果你单独使用CMake,你肯定会得到一个<代码>.a 文件.

This is Gradle/Android Studio 's "fault", the answers above already mentioned it, if you use CMake alone, you'll definetely get a .a file.

好的,现在是我的TRICK:

我们已经有一个添加共享库,然后我们添加一个静态库,它使用相同的源文件.在您的 CMakeLists.txt 中添加以下内容:

We already have a add SHARED library, then we add a STATIC library, which uses the same source file. Add below in your CMakeLists.txt:

add_library(
        native-lib-static
        STATIC
        src/main/cpp/native-lib.cpp
)

本地库静态"可以替换为除native-lib"之外的任何名称.因为它用于共享版本.

"native-lib-static" can be replaced with any name but "native-lib" since it is used for the SHARED version.

像这样更改您的 target_link_libraries:

target_link_libraries(
        native-lib
        native-lib-static
        ${log-lib})

Gradle->app->build->assembleDebug/assembleRelease

Gradle->app->build->assembleDebug/assembleRelease

然后你会得到libnative-lib-static.a,在

app.externalNativeBuildcmakedebug(release)<abi>libnative-lib-static.a

这个路径是在appuild.gradle中设置的:

This path is set in appuild.gradle:

android{
    defaultConfig{
         externalNativeBuild{
             CMake{

ATM 我不确定 Google 将来是否会更改它,但您始终可以在项目文件夹中搜索 *.a 文件.

ATM i'm not sure whether Google will change it in the future, but you can always search the project folder for *.a files.

这次我想我什么都不会错过.

I don't think i miss anything this time.

感谢@Michael @Dan Albert @Alex Cohn @shizhen

All credit to @Michael @Dan Albert @Alex Cohn @shizhen

这篇关于如何使用 CMake 在 Android Studio 3.2 中创建静态库(.a 文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆