Android studio ndkカスタムMKファイルのコンパイル

10541 ワード

この列は最新バージョンのandroid studio 2.0のテストに成功しました.1、local.propertiesにndkパスを追加するには、次のようにします.
ndk.dir=/Users/apple/Library/Android/sdk/ndk-bundle
sdk.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.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
}

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

方法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'
}