本列是最新版本的android studio 2.0 测试成功。
1、local.properties中添加ndk路径 如下:

ndk.dir=/Users/apple/Library/Android/sdk/ndk-bundlesdk.dir=/Users/apple/Library/Android/sdk

2、gradle.properties 中添加 android.useDeprecatedNdk=true
3、修改 build.gradle
引入 import org.apache.tools.ant.taskdefs.condition.Os
在 android {} 中添加如下代码

sourceSets {        main {            jni.srcDirs=[] //禁止使用默认的ndk编译系统            jniLibs.srcDirs 'src/main/libs' //so存放地方        }    }    task ndkBuild(type: org.gradle.api.tasks.Exec) {        workingDir file('src/main')        commandLine getNdkBuildCmd() //执行ndk-build 命令    }    tasks.withType(JavaCompile) {        compileTask -> compileTask.dependsOn ndkBuild    }    task cleanNative(type: Exec) {        workingDir file('src/main')        commandLine getNdkBuildCmd(), 'clean'    }    clean.dependsOn cleanNative

android{} 外面添加如下:

def getNdkDir() {    if (System.env.ANDROID_NDK_ROOT != null)       return System.env.ANDROID_NDK_ROOT    Properties properties = new Properties()    properties.load(project.rootProject.file('local.properties')       .newDataInputStream())       //从属性文件里得到ndk路径    def ndkdir = properties.getProperty('ndk.dir', null)    if (ndkdir == null)       throw new GradleException("NDK location not found. Define  location with ndk.dir in the local.properties file or with an ANDROID_NDK_ROOT environment variable.")    return ndkdir}def getNdkBuildCmd() {   def ndkbuild = getNdkDir() + "/ndk-build"   if (Os.isFamily(Os.FAMILY_WINDOWS))       ndkbuild += ".cmd"    return ndkbuild}

4、整体 添加如下:

import org.apache.tools.ant.taskdefs.condition.Osapply plugin: 'com.android.application'android {    compileSdkVersion 23    buildToolsVersion "23.0.2"    defaultConfig {        applicationId "cody.com.jnidemo"        minSdkVersion 18        targetSdkVersion 23        versionCode 1        versionName "1.0"//        ndk { //这里用系统自带的mk文件编译,不需要些mk文件//            moduleName "jnidemo"//            ldLibs "log"////        }    }    buildTypes {        release {            minifyEnabled false            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'        }    }    //sourceSets { main { jni.srcDirs = ['src/main/jni', 'src/main/jni/', 'src/main/jniLibs'] } }    sourceSets {        main {            jni.srcDirs=[]            jniLibs.srcDirs 'src/main/libs'        }    }    task ndkBuild(type: org.gradle.api.tasks.Exec) {        workingDir file('src/main')        commandLine getNdkBuildCmd()    }    tasks.withType(JavaCompile) {        compileTask -> compileTask.dependsOn ndkBuild    }    task cleanNative(type: Exec) {        workingDir file('src/main')        commandLine getNdkBuildCmd(), 'clean'    }    clean.dependsOn cleanNative}dependencies {    compile fileTree(dir: 'libs', include: ['*.jar'])    testCompile 'junit:junit:4.12'    compile 'com.android.support:appcompat-v7:23.3.0'}def getNdkDir() {       if (System.env.ANDROID_NDK_ROOT != null)           return System.env.ANDROID_NDK_ROOT              Properties properties = new Properties()       properties.load(project.rootProject.file('local.properties')       .newDataInputStream())       def ndkdir = properties.getProperty('ndk.dir', null)    if (ndkdir == null)       throw new GradleException("NDK location not found. Define location with ndk.dir in the local.properties file or with an ANDROID_NDK_ROOT environment variable.")    return ndkdir}def getNdkBuildCmd() {       def ndkbuild = getNdkDir() + "/ndk-build"       if (Os.isFamily(Os.FAMILY_WINDOWS))               ndkbuild += ".cmd"    return ndkbuild}

ndk编译出错如下错误处理,在相应mk文件加

warning: shared library text segment is not shareable error: treating warnings as errors

添加内容如下:

 LOCAL_DISABLE_FATAL_LINKER_WARNINGS := true LOCAL_LDFLAGS += -Wl,--no-warn-shared-textrel

方法二:简单一点的

apply plugin: 'com.android.application'android {    compileSdkVersion 22    buildToolsVersion '22.0.0'    defaultConfig {        applicationId "cody.com.android5mediacodec"        minSdkVersion 19        targetSdkVersion 22        versionCode 1        versionName "1.0"//        ndk {//            moduleName "xiaoyao_live"//so文件的名称//            ldLibs "log","z","m"//            abiFilters "armeabi-v7a"//        }    }    buildTypes {        release {            minifyEnabled false            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'        }    }    sourceSets {        main{//            jni.srcDirs = []            jniLibs.srcDirs 'src/main/libs' //so存放地方        }    }    task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {        commandLine "/Users/apple/Library/Android/sdk/ndk-bundle/ndk-build",                'NDK_PROJECT_PATH=build/intermediates/ndk',                'NDK_LIBS_OUT=src/main/libs',                'APP_BUILD_SCRIPT=src/main/jni/Android.mk',                'NDK_APPLICATION_MK=src/main/jni/Application.mk'    }    tasks.withType(JavaCompile) {        compileTask -> compileTask.dependsOn ndkBuild    }}dependencies {    compile fileTree(dir: 'libs', include: ['*.jar'])    testCompile 'junit:junit:4.12'    //    compile 'com.android.support:appcompat-v7:23.3.0'    compile 'com.android.support:appcompat-v7:22.1.0'}

更多相关文章

  1. Android(安卓)Camera使用小结
  2. Android的Menu
  3. Android系列之Android(安卓)命令行手动编译打包详解
  4. Android使用gradle生成maven库,上传githu并使用远程maven库
  5. Android开发如何正确使用WebView
  6. Android学习笔记之mainfest文件中android属性
  7. Android(安卓)Camera使用小结
  8. Android(安卓)Studio-Gradle项目中添加JNI生成文件(.so文件)
  9. Activity去除标题栏和状态栏

随机推荐

  1. android 适配器Adpter的使用总结 之 Curs
  2. 使用GreenDroid开源项目
  3. android上传图片和参数(属性)到服务器
  4. Android(安卓)jni调用第三方so库和.h文件
  5. Android中SQLite数据库存储方式 .(转)
  6. ubuntu下Qt之android环境配置以及一些常
  7. Android中Touch手势分发
  8. HTML iframe标签用法案例详解
  9. HTML DOM setInterval和clearInterval方
  10. 利用模糊实现视觉3D效果实例讲解