Android Studio Cmake配置项

Cmake的配置

Android Studio 2.2以上支持了Cmake的配置JNI的相关参数,简化了通过Android.mk配置。并很好的继承了C++的编辑方式。以下是对应的引入第三方so和第三方.cpp文件的路径脚本编写。对应于:CMakeLists.txt

#定义变量ProjectRoot为工程根目录,用相对路径没通过编译,可能是路径写错,以后再试
#本次使用绝对路径作为参数
set(Project D:/GitHub/EchoCancellation)
#动态方式加载 speex是libxxxx.so的xxxx部分
add_library(speex SHARED IMPORTED)
#设置要连接的so的相对路径,${ANDROID_ABI}表示so文件的ABI类型的路径,这一步引入了动态加入编译的so
set_target_properties(speex PROPERTIES IMPORTED_LOCATION
                      ${Project}/app/src/main/jni/${ANDROID_ABI}/libspeex.so)
#配置加载native依赖
#include_directories( ${Project}/app/src/main/jni/include )
#将native-lib加入到编译源中
add_library( # Sets the name of the library.
             native-lib

             SHARED

             src/main/cpp/native-lib.cpp )


find_library( # Sets the name of the path variable.
              log-lib

              log )

#这里多出了引用的speex
target_link_libraries( # Specifies the target library.
                       native-lib
                       speex

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )


build.gradle的配置

此处配置主要是我们写的JNI层的代码,可以通过编译为SO文件,被我们的项目引用。生成的so,在你对应项目的build中进行查找。


android {
    compileSdkVersion 25
    buildToolsVersion "25.0.1"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags "-std=c++11 -frtti -fexceptions"
//                moduleName "netContent"
                abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'mips', 'mips64'
            }
        }
        sourceSets {
            main {
                jniLibs.srcDirs = ['D:\\progress\\FaqRobotSdk_v2\\sdk_v2\\src\\main\\jniLibs']
            }
        }
    }
    signingConfigs {
        release {
            storeFile file('debug.jks')
            storePassword "houshuai"
            keyAlias "houshuai"
            keyPassword "houshuai"
        }
        debug {
            storeFile file('debug.jks')
            storePassword "houshuai"
            keyAlias "houshuai"
            keyPassword "houshuai"
        }

    }
    buildTypes {
        release {
            // 不显示Log
            buildConfigField "boolean", "LOG_DEBUG", "false"
            //混淆
            minifyEnabled false
            //Zipalign优化
            zipAlignEnabled false
            // 移除无用的resource文件
            shrinkResources false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release // 签名配置
        }
        debug {
            buildConfigField "boolean", "LOG_DEBUG", "true"

            minifyEnabled false

            zipAlignEnabled false

            shrinkResources false

            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

            signingConfig signingConfigs.debug
        }
    }
    externalNativeBuild {
        cmake {
            path 'CMakeLists.txt'
        }
    }
}
发布了20 篇原创文章 · 获赞 24 · 访问量 6万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章