Android已有工程支持c++ 使用CMake

Android 已有项目添加c++支持
1、右键module新建CMakeLists.txt
2.module中build.gradle配置文件中在

  android{
     defaultConfig {
         externalNativeBuild {
            cmake {
                cppFlags ""
            }
        }
        ndk {//编译在哪个CPU平台上
            abiFilters 'armeabi-v7a'
        }
      sourceSets.main {//外部so文件路径
            jniLibs.srcDir 'libs'
        }
    }
    externalNativeBuild {

        // Encapsulates your CMake build configurations.
        cmake {

            // Provides a relative path to your CMake build script.
            path "CMakeLists.txt"
        }
        }
}



3、CMakeLists.txt文件编写规则

cmake_minimum_required(VERSION 3.4.1)

add_library( # 为library起名字
             native-lib
             # 设置为SHARE类型,STATIC为需要加载.a文件
             SHARED
             # 需要编译的源代码文件目录
             src/main/cpp/native-lib.cpp )
add_library( avcodec-57# 为library起名字
             SHARED
             IMPORTED ) # 不需要编译,由外部导入
set_target_properties( avcodec-57
                       PROPERTIES IMPORTED_LOCATION
                       ../../../../libs/armeabi-v7a/libavcodec-57.so )//配置导入文件的路径
add_library( avfilter-6
             SHARED
             IMPORTED )
set_target_properties( avfilter-6
                       PROPERTIES IMPORTED_LOCATION
                       ../../../../libs/armeabi-v7a/libavfilter-6.so )
add_library( avformat-57
             SHARED
             IMPORTED )
set_target_properties( avformat-57
                       PROPERTIES IMPORTED_LOCATION
                       ../../../../libs/armeabi-v7a/libavformat-57.so )
add_library( avutil-55
             SHARED
             IMPORTED )
set_target_properties( avutil-55
                       PROPERTIES IMPORTED_LOCATION
                       ../../../../libs/armeabi-v7a/libavutil-55.so )
add_library( swresample-2
             SHARED
             IMPORTED )
set_target_properties( swresample-2
                       PROPERTIES IMPORTED_LOCATION
                       ../../../../libs/armeabi-v7a/libswresample-2.so )
add_library( swscale-4
             SHARED
             IMPORTED )
set_target_properties( swscale-4
                       PROPERTIES IMPORTED_LOCATION
                       ../../../../libs/armeabi-v7a/libswscale-4.so )
include_directories( libs/include )#外部库需要的头文件
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 )//加载NDK上的library
# 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.
                       native-lib
                       avcodec-57
                       avfilter-6
                       avformat-57
                       avutil-55
                       swresample-2
                       swscale-4
                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )



posted @ 2017-06-27 16:18  清澈见底  阅读(295)  评论(0编辑  收藏  举报