Android Studio/Gradle 中编译捆绑库的问题

Issue with compiling bundled libs in Android Studio/Gradle

我想编译捆绑在我的项目中的库。我 运行 分为 2 期。

第一个 Cmake 似乎没有detect/include那个目录。

第二个是在捆绑目录之后detected/included而不是android工具链系统的一个用于编译库。

作为第一期的解决方法,我添加了 if(ANDROID) 以添加该目录以便将其包含在内。

if(EXISTS "${CMAKE_SOURCE_DIR}/libs/CMakeLists.txt")
    message(STATUS "Using bundled libraries located at ${CMAKE_SOURCE_DIR}/libs")
    if(ANDROID)
        add_subdirectory(libs)
    else()
        include(libs/CMakeLists.txt)
    endif()
else()

所以对我来说,预期结果应该如下所示,包括 libs/CMakeLists.txt 并使用 NDK

提供的工具链构建库

如果您尝试为本机组件使用非 ndk 预构建库,请将这些库详细信息添加到 CMake 中,如下所述。

add_library( imported-lib
             SHARED
             IMPORTED )
set_target_properties( # Specifies the target library.
                       imported-lib

                       # Specifies the parameter you want to define.
                       PROPERTIES IMPORTED_LOCATION

                       # Provides the path to the library you want to import.
                       imported-lib/src/${ANDROID_ABI}/libimported-lib.so )

https://developer.android.com/studio/projects/configure-cmake#add-other-library

用于使用 cmake 系统构建本机库。

  1. 在您的模块中定义CMakeLists.txt(用于生成库的本地源代码)。

    add_library(xyz 静态 folder-name/xyz.cpp)

    target_include_directories(xyz PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR}/..)
    
  2. 在您的 android 应用程序模块 cpp 文件夹中定义 CMakeLists.txt。

    cmake_minimum_required(版本 3.4.1)

    /* 使用现有的 ndk 库,如果你愿意,你可以删除它 */

    将 native_app_glue 构建为静态库。

    add_library(native_app_glue 静态 ${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c)

    为 glm 库导入 CMakeLists.txt

    add_subdirectory(glm)

    /* 如果你添加头文件位置则需要 */ target_include_directories(游戏私人 ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/数据 ${ANDROID_NDK}/sources/android/native_app_glue)

    添加库依赖项

    target_link_libraries( native_app_glue xyz )

  3. 定义应用模块build.gradle

    android { compileSdkVersion 28

    defaultConfig {
        applicationId 'com.google.sample.tunnel'
        minSdkVersion 14
        targetSdkVersion 28
        versionCode     1
        versionName    '1.0'
    
    }
    
    externalNativeBuild {
        cmake {
            version '3.10.2'
            path 'src/main/cpp/CMakeLists.txt' // location of second(app module) CMakeLists.txt
        }
    }
    

    }

  4. 同时检查 CMake 构建命令以验证构建期间使用的所有参数。 检查这个文件 app.externalNativeBuild\cmake\debug\x86\cmake_build_command.txt

这就是它构建 libcurl 的方式,它首先配置它,然后使用错误的工具链进行编译。我确实在这里 https://developer.android.com/ndk/guides/other_build_systems 找到了一些东西,但它确实为每个构建导出 1 个工具链。我的正在使用 2 个 abi

ndk {
            // Specifies the ABI configurations of your native
            // libraries Gradle should build and package with your APK.
            abiFilters 'armeabi-v7a', 'arm64-v8a'
        }

如您所见,它使用的是 ExternalProject_Add。

#-----------------------------------------------------------------
    # Build bundled cURL library
    #-----------------------------------------------------------------
    if(BUNDLED_CURL AND (BUILD_CLIENT OR BUILD_SERVER))

        ExternalProject_Add(
            bundled_curl
            SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/curl
            CONFIGURE_COMMAND ./configure --prefix=${CMAKE_CURRENT_BINARY_DIR}/libs/curl
            --enable-shared=no --enable-static=yes
            --enable-http --enable-ftp --disable-file
            --disable-ldap --disable-ldaps --disable-rtsp
            --enable-proxy --disable-dict --disable-telnet
            --disable-tftp --disable-pop3 --disable-imap
            --disable-smb --disable-smtp --disable-gopher
            --without-ssl --without-libssh2 --without-nghttp2
            --without-gssapi --with-zlib
            --disable-ares --enable-threaded-resolver
            --enable-ipv6 --enable-unix-sockets
            --without-libidn2 --disable-manual
            --disable-sspi --enable-libgcc
            --without-libmetalink --without-libpsl
            --without-librtmp ${CROSS_COMPILE32_FLAGS}
            PREFIX ${CMAKE_CURRENT_BINARY_DIR}/libs/curl
            BUILD_COMMAND make
            INSTALL_COMMAND make install
            BUILD_IN_SOURCE 1
        )

        set(CURL_BUNDLED_LIBRARY "${CMAKE_CURRENT_BINARY_DIR}/libs/curl/lib/libcurl.a")
        set(CURL_BUNDLED_INCLUDE_DIR "${CMAKE_CURRENT_BINARY_DIR}/libs/curl/include")
    endif()