现在的位置: 首页Android开发 > 正文
android studio ndk 编译自定义MK文件
关键词:无 ┊ 来源: 原创收藏

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

ndk.dir=/Users/apple/Library/Android/sdk/ndk-bundle
sdk.dir=/Users/apple/Library/Android/sdk
  • 1
  • 2
  • 1
  • 2

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

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
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

4、整体 添加如下:

import org.apache.tools.ant.taskdefs.condition.Os
apply 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
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78

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

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

添加内容如下:

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

方法二:简单一点的

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'
}
相关文章
    暂无相关文章
本文由 jack 发布于 566天 19小时 7分钟前,目前已有 4404 人浏览
欢迎大家转载分享,请注明来源及链接;商业媒体转载请获得授权,谢谢合作!
 

添加评论