AS運用NDK編譯及調用

環境

android studio 2.0

安裝

File>Other Settings>Default Project Structure…

注意,這裏有可能你會發現你的路經不能點擊,那是因爲被牆了,破牆下載

配置

  • build.gradle
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle-experimental:0.4.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}
  • app/build.gradle

注意自己寫時,要添加=.apiLevel,寫法不一樣

apply plugin: 'com.android.model.application'

model {
    android {
        compileSdkVersion = 23
        buildToolsVersion = "23.0.2"

        defaultConfig.with {
            applicationId = "com.xuie.jnidemo"
            minSdkVersion.apiLevel = 15
            targetSdkVersion.apiLevel = 23
            versionCode = 1
            versionName = "1.0"
        }
    }

    compileOptions.with {
        sourceCompatibility=JavaVersion.VERSION_1_7
        targetCompatibility=JavaVersion.VERSION_1_7
    }

    /*
     * native build settings
     */
    android.ndk {
        moduleName = "hello-jni"
        /*
         * Other ndk flags configurable here are
         * cppFlags.add("-fno-rtti")
         * cppFlags.add("-fno-exceptions")
         * ldLibs.addAll(["android", "log"])
         * stl       = "system"
         */
    }
    android.buildTypes {
        release {
            minifyEnabled = false
            proguardFiles.add(file('proguard-rules.txt'))
        }
    }
    android.productFlavors {
        // for detailed abiFilter descriptions, refer to "Supported ABIs" @
        // https://developer.android.com/ndk/guides/abis.html#sa
        create("arm") {
            ndk.abiFilters.add("armeabi")
        }
        create("arm7") {
            ndk.abiFilters.add("armeabi-v7a")
        }
        create("arm8") {
            ndk.abiFilters.add("arm64-v8a")
        }
        create("x86") {
            ndk.abiFilters.add("x86")
        }
        create("x86-64") {
            ndk.abiFilters.add("x86_64")
        }
        create("mips") {
            ndk.abiFilters.add("mips")
        }
        create("mips-64") {
            ndk.abiFilters.add("mips64")
        }
        // To include all cpu architectures, leaves abiFilters empty
        create("all")
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
}

編譯

  • 新建JniInterface.java
    這裏直接寫出函數添加了native關鍵字,用補全,就會在hello-jni.c裏自動生成函數名稱,比以前用javah有質有飛躍。。。
public class JniInterface {

    public native String  stringFromJNI();

    static {
        System.loadLibrary("hello-jni");
    }
}
  • 自身調用
JniInterface jniInterface = new JniInterface();
((TextView)findViewById(R.id.tv)).setText(jniInterface.stringFromJNI());
  • 他人調用
    我們本身NDK調用,是不希望自己的工程裏出現C/C++,那就直接可以調用.so,我以armeabi-v7a架構爲例進行調用(因爲我手機是這個架構),拷貝目錄build/intermediates/binaries/debug/all/lib/armeabi-v7a/下的libhello-jni.so到所用工程main/jniLibs/armeabi-v7a下,調用時java目錄下同樣需要添加JniInterface.java注意包名要根原來一模一樣,然後再調用,實際項目中要將所有架構下的.so都拷貝進去
JniInterface jniInterface = new JniInterface();    
((TextView)findViewById(R.id.tv)).setText("[Use]" + jniInterface.stringFromJNI());

參考

https://github.com/googlesamples/android-ndk

源碼下載

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章