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

时间:2019-04-17 17:46:10

标签: android gradle cmake android-gradle

我想编译捆绑在我的项目中的库。我遇到了两个问题。

首先,Cmake似乎没有检测到/包含该目录。

第二个是检测到/包含捆绑目录后,而不是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提供的工具链来构建库

3 个答案:

答案 0 :(得分:0)

如果您要为本机组件使用非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

答案 1 :(得分:0)

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

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

    add_library(xyz STATIC         文件夹名称/xyz.cpp)

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

    cmake_minimum_required(版本3.4.1)

    / *使用现有的ndk lib,如果需要,可以删除此* /

    将native_app_glue构建为静态库。

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

    将CMakeLists.txt导入glm库

    add_subdirectory(glm)

    / *要求是否添加头文件位置* / target_include_directories(游戏PRIVATE      $ {CMAKE_CURRENT_SOURCE_DIR}      $ {CMAKE_CURRENT_SOURCE_DIR} /数据      $ {ANDROID_NDK} / sources / android / native_app_glue)

    添加lib依赖项

    target_link_libraries(      native_app_glue      y    )

  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

答案 2 :(得分:0)

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

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()