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

查看:630
本文介绍了如何使用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文件(当然,没有<在该文件中添加了strong> 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 :

我们已经有一个添加SHARED库,然后添加一个使用相同源文件的STATIC库.在您的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-static"可以用"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\.externalNativeBuild\cmake\debug(release)\<abi>\libnative-lib-static.a

此路径在app\build.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天全站免登陆