美文网首页c++ ndk
androidstudio cmakelist编译.cpp并且引

androidstudio cmakelist编译.cpp并且引

作者: miniminiming | 来源:发表于2018-01-08 14:54 被阅读0次

    一、项目结构

    1. 项目配置
      主要是里面cmake的配置,ndk配置,设置cMakeLists.txt


      image.png
    2. 目录结构
      .cpp文件和它需要的.h文件都放在cpp文件夹下
      .cpp文件依赖的.so文件则放在jniLibs文件夹下,jniLibs文件夹在Gradle脚本中是默认存放.so文件的位置,如果你想改它则需要在build.gradle中指定它的位置,如下设置:
    sourceSets {
            main {
                jniLibs.srcDirs = ['libs']
            }
    }
    

    build.gradle设置

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 26
        defaultConfig {
            applicationId "com.example.dell.dualcampreview"
            minSdkVersion 15
            targetSdkVersion 26
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    
            externalNativeBuild {
                cmake {
                    cppFlags "-frtti -fexceptions"
                }
            }
    
            ndk {
                abiFilters 'armeabi-v7a'
            }
        }
    
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
        externalNativeBuild {
            cmake {
                path "CMakeLists.txt"
            }
        }
    }
    
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation 'com.android.support:appcompat-v7:26.1.0'
        implementation 'com.android.support.constraint:constraint-layout:1.0.2'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.1'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    }
    
    

    二、CMakeLists.txt中的设置

    整体都是系统自动生成 的,只看关键的几个地方

    1. add_library()总共有三个
      第一个是根据.cpp文件编译.so,如果有include的头文件的话,在后面加上include_directories()
      第二个和第三个则是前面.cpp文件所需要的依赖的库文件,在后面需要加上set_target_properties()来它依赖的.so文件
    2. 末尾的 target_link_libraries,把前面的引入的库名都写上即可
    # For more information about using CMake with Android Studio, read the
    # documentation: https://d.android.com/studio/projects/add-native-code.html
    
    # Sets the minimum version of CMake required to build the native library.
    
    cmake_minimum_required(VERSION 3.4.1)
    
    # Creates and names a library, sets it as either STATIC
    # or SHARED, and provides the relative paths to its source code.
    # You can define multiple libraries, and CMake builds them for you.
    # Gradle automatically packages shared libraries with your APK.
    
    
    #此处可以通过多加几个addlibrary来针对多个cpp文件来生成多个.so文件
    #它的用法跟Android.mk里的定义差不多
    #需要留意,编译出来的文件放在\build\intermediates\cmake中,但是不用自己复制了,它会自动复制到
    #jniLibs文件夹下
    add_library(
        jniSTDualCamPreview
        SHARED
        src/main/cpp/main.cpp
    )
    #include的文件夹
    include_directories(jniSTDualCamPreview
                        ${CMAKE_SOURCE_DIR}/src/main/cpp/include)
    
    #两个预编译好的库
    add_library(STdisp  SHARED  IMPORTED)
    set_target_properties(STdisp PROPERTIES IMPORTED_LOCATION
                          ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libSTdisp.so)
    add_library(Stereoblur  SHARED  IMPORTED)
    set_target_properties(Stereoblur PROPERTIES IMPORTED_LOCATION
                          ${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libStereoblur.so)
    
    
    
    # Searches for a specified prebuilt library and stores the path as a
    # variable. Because CMake includes system libraries in the search path by
    # default, you only need to specify the name of the public NDK library
    # you want to add. CMake verifies that the library exists before
    # completing its build.
    
    find_library( # Sets the name of the path variable.
                  log-lib
    
                  # Specifies the name of the NDK library that
                  # you want CMake to locate.
                  log )
    
    # Specifies libraries CMake should link to your target library. You
    # can link multiple libraries, such as libraries you define in this
    # build script, prebuilt third-party libraries, or system libraries.
    
    target_link_libraries( # Specifies the target library.
                           jniSTDualCamPreview
                           STdisp
                           Stereoblur
                           # Links the target library to the log library
                           # included in the NDK.
                           ${log-lib} )
    
    

    三、遇到的一个异常情况

    1.编译.cpp正常,但是build生成apk的时候出的异常

    > More than one file was found with OS independent path 'lib/armeabi-v7a/libmainLib.so'
    

    处理方式

    其实是因为自己把编译.cpp生成的.so复制到了jniLibs下,但是cMakeList它其实会自己去编译和复制,并且自动放到jniLibs下,所以最后打包的时候就会报错More than one file

    相关文章

      网友评论

        本文标题:androidstudio cmakelist编译.cpp并且引

        本文链接:https://www.haomeiwen.com/subject/tpdbnxtx.html