2015年5月26日 星期二

Android Stdio JNI & JAR設定

Android Stdio version : 1.2.11

在eclipse可以跑的程式,移來Android Stdio卻發生點問題,
在compiler時,出現
Error:Execution failed for task ':app:compileDebugNdk'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/opt/android-ndk-r10b/ndk-build'' finished with non-zero exit value 2
花了些時間查尋才解決,
(先確定,*.c這些程式編譯沒問題)
在build.gradle加入
android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"
    之前查到資料,有人建議這兩個值版本設定一樣的,比較不會打牆
    defaultConfig {
        applicationId "com.main"
        minSdkVersion 19
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
        ndk {
            moduleName "serversocket"
            ldLibs "log", "z", "m"
            abiFilters "armeabi", "armeabi-v7a", "x86" 
        }

        // Studio Version 2.1.2
        //第三方 *.so時。注意:同這個方法,然後package name要與*.so的相同@@
        //在 /data/app-libs/com.apkName-* 下(需root權限),會看到 *.so,成功的話
        sourceSets {
            main {
                jniLibs.srcDir(['libs'])
           }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    // for jar
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/LICENSE.txt'
    }

    sourceSets { main { jni.srcDirs = ['src/main/jni', 'src/main/jni/'] } }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.1.1'
    
    // for jar
    compile files('libs/filename.jar') 
}
在local.properties上記得加入NDK路徑
sdk.dir=/opt/android-sdk-linux
ndk.dir=/opt/android-ndk-r10b

ref : Sodino的专栏
ref: Stack Overflow

而當第三方的source code(在src/main/jni/下)包含Android.mk & Application.mk時:
(第三的source code裡有*.so的話,放到src/main/jniLibs/ABI下的就可以了,
ABI=armeabi, armeabi-v7a, x86, x86_64看*.so的種類)
環境:build.gradle - gradle:2.1.2
apply plugin: 'com.android.application'

android {
    compileSdkVersion 19
    buildToolsVersion "23.0.1"

    sourceSets.main {
        jni.srcDirs = [] // This prevents the auto generation of Android.mk
        jniLibs.srcDir 'src/main/jniLibs'  //This is not necessary unless you have precompiled libraries in your project.
    }

    defaultConfig {
        applicationId "com.example.test"
        minSdkVersion 19
        targetSdkVersion 21

        ndk {
            moduleName "ndkTestSample"
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    task buildNative(type: Exec, description: 'Compile JNI source via NDK') {
        def ndkDir = android.ndkDirectory
        commandLine "$ndkDir/ndk-build",
                '-C', file('src/main/jni').absolutePath, // Change src/main/jni the relative path to your jni source
                '-j', Runtime.runtime.availableProcessors(),
                'all',
                'NDK_DEBUG=1'
    }

    task cleanNative(type: Exec, description: 'Clean JNI object files') {
        def ndkDir = android.ndkDirectory
        commandLine "$ndkDir/ndk-build",
                '-C', file('src/main/jni').absolutePath, // Change src/main/jni the relative path to your jni source
                'clean'
    }

    clean.dependsOn 'cleanNative'

    tasks.withType(JavaCompile) {
        compileTask -> compileTask.dependsOn buildNative
    }

}

dependencies {
    compile 'com.google.code.gson:gson:2.1'
}
ref : stackoverflow, stackoverflow

Application.mk
APP_PROJECT_PATH := $(call my-dir)/..
APP_BUILD_SCRIPT := $(APP_PROJECT_PATH)/jni/Android.mk
#APP_OPTIM := debug
APP_OPTIM := release
APP_ABI := armeabi-v7a
APP_STL := gnustl_static

沒有留言:

張貼留言