本列是最新版本的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 Studio-Gradle项目中添加JNI生成文件(.so文件)
  2. Android 一个APK文件部署产生多个应用安装的效果
  3. Android 资源文件中的符号含义与说明
  4. [zz http://www.cnblogs.com/oldfeel/archive/2012/05/15/250129
  5. android -上传文件到服务器
  6. Android中使用XmlPullParse解析xml文件
  7. 如何将library项目打包成jar文件
  8. How to decompile .dex file on Android如何反编译.dex文件

随机推荐

  1. 使用 jQuery Mobile 与 HTML5 开发 Web A
  2. 我怎么能用javascript编写这个简短的函数
  3. NodeList接口,HTMLCollection接口
  4. 纯 html 以及 js 多域名跳转
  5. 如何在div到底部对齐?
  6. Html5如何使我们开发出来的应用或页面大
  7. HTML网页与FLASH之间的传值
  8. 当点击第二次时,角的ng-点击切换。
  9. 设置密码输入的默认值,以便可以读取
  10. 聊聊css盒子模型