简体   繁体   English

在Android Studio中创建本机库

[英]Create native library in android studio

I have created android studio project with c++ support and installed native development kit and other required tools. 我创建了具有c ++支持的android studio项目,并安装了本机开发套件和其他必需的工具。 How to import another prebuilt native library to include in c++ code. 如何导入另一个预构建的本机库以包含在c ++代码中。 I want to do something like this 我想做这样的事情 我想做这样的事情

I have edited cmakelist.txt as given below and now it show shared library under cpp folder. 我已经如下所示编辑了cmakelist.txt,现在它显示了cpp文件夹下的共享库。 在此处输入图片说明

cmake_minimum_required(VERSION 3.4.1)
set(distribution_DIR ${CMAKE_SOURCE_DIR}/../../../../distribution)
add_library(lib_gmath STATIC IMPORTED)
set_target_properties(lib_gmath PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/gmath/lib/${ANDROID_ABI}/libgmath.a)
add_library(lib_gperf SHARED IMPORTED)
set_target_properties(lib_gperf PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/gperf/lib/${ANDROID_ABI}/libgperf.so)
add_library(lib_ocr SHARED IMPORTED)
set_target_properties(lib_ocr PROPERTIES IMPORTED_LOCATION
${distribution_DIR}/ocr/lib/${ANDROID_ABI}/libLPROCR.so)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
add_library(hello-libs SHARED
        hello-libs.cpp)
target_include_directories(hello-libs PRIVATE
                       ${distribution_DIR}/gmath/include
                       ${distribution_DIR}/gperf/include)
target_link_libraries(hello-libs
                  android
                  lib_gmath
                  lib_gperf
                  lib_ocr
                  log)

But i am not able to include classes from that prebuilt library in my c++ code. 但是我无法在我的C ++代码中包含该预建库中的类。

There are few things that I should mention here. 我在这里应该提到的几件事。

  1. I suggest to switch to gradle+clang for building your code. 我建议切换到gradle+clang来构建代码。 Android.mk is somehow getting old and it is better to use newer build systems if you are starting new project. Android.mk已经过时了,如果要开始新项目,最好使用更新的构建系统。
  2. Make sure that your prebuilt library can be used in android. 确保您的预建库可以在android中使用。 If it is not, you need to build it first. 如果不是,则需要先构建它。 For example I built openssl myself because I could not find a suitable prebuilt library. 例如,我自己构建了openssl,因为找不到合适的预构建库。

In case your are using gradle+cmake , here is a simple CMakeLists.txt file for your prebuilt libraries: 如果您使用gradle+cmake ,这是预构建库的一个简单CMakeLists.txt文件:

cmake_minimum_required(VERSION 3.4.1)

add_library( core-api
             SHARED
             src/main/cpp/mysource.c )

# prebuilt libraries

add_library( crypto-lib
             STATIC
             IMPORTED )

set_target_properties( crypto-lib
                       PROPERTIES IMPORTED_LOCATION
                       ${CMAKE_CURRENT_SOURCE_DIR}/../prebuilt/crypto/${ANDROID_ABI}/lib/libcrypto.a )


include_directories( src/main/cpp/
                     ../prebuilt/crypto/common/include/
                     ../prebuilt/crypto/${ANDROID_ABI}/include/ )

# other libraries

find_library( log-lib
              log )

target_link_libraries( core-api
                       crypto-lib
                       ${log-lib} )

And this is a gradle file that uses this CMakeLists.txt : 这是使用此CMakeLists.txtgradle文件:

apply plugin: 'com.android.library'

android {
    compileSdkVersion rootProject.ext.sdkVersion

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 27

        externalNativeBuild {
            cmake {
                arguments "-DANDROID_TOOLCHAIN=clang", "-DANDROID_STL=c++_static"
                cFlags "-D__STDC_FORMAT_MACROS", "-fno-integrated-as"
            }
        }

        ndk {
            abiFilters "armeabi-v7a", "arm64-v8a", "x86", "x86_64"
        }
    }

    buildTypes {
        release {
            minifyEnabled true
            shrinkResources false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

        debug {
            externalNativeBuild {
                cmake {
                    cFlags "-Wall","-DDEBUG"
                }
            }

            debuggable true
        }
    }

    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM