1. 程式人生 > >使用Android Studio開發 JNI

使用Android Studio開發 JNI

新版本的Android Studio支援使用CMake來編譯JNI,使得開發帶有C或者C++語言的程式碼變得簡單的許多,下面一個簡單的示例專案

環境

Android Studio 2.2
build-tools 版本 2.2.0-alpha6
NDK支援(應下載SDK Tools中的NDK)

SDK Location設定大概如下所示

這裡寫圖片描述

配置

1、 在專案的 build.gradle 中的配置如下

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.0-alpha6'
}
} allprojects { repositories { jcenter() } } task clean(type: Delete) { delete rootProject.buildDir }

2.、在主module的build.gradle如下

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"
    defaultConfig {
        applicationId "duan.key"
minSdkVersion 19 targetSdkVersion 23 versionCode 1 versionName "1.0" externalNativeBuild { cmake { abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'arm64-v8a' } } } buildTypes { release { minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } externalNativeBuild { cmake { path "CMakeLists.txt" } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:23.+' compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha4' testCompile 'junit:junit:4.12' }

3、CMakeLists.txt的內容如下

cmake_minimum_required(VERSION 3.4.1)

add_library( native-lib
             SHARED
             src/main/cpp/native-lib.c )

target_link_libraries(native-lib log android)

4、native-lib.c的程式碼如下,這段程式碼回返回手機的CPU型號

#include <string.h>
#include <jni.h>

jstring
Java_duan_key_MainActivity_stringFromJNI( JNIEnv* env,
                                                  jobject thiz )
{
#if defined(__arm__)
    #if defined(__ARM_ARCH_7A__)
    #if defined(__ARM_NEON__)
      #if defined(__ARM_PCS_VFP)
        #define ABI "armeabi-v7a/NEON (hard-float)"
      #else
        #define ABI "armeabi-v7a/NEON"
      #endif
    #else
      #if defined(__ARM_PCS_VFP)
        #define ABI "armeabi-v7a (hard-float)"
      #else
        #define ABI "armeabi-v7a"
      #endif
    #endif
  #else
   #define ABI "armeabi"
  #endif
#elif defined(__i386__)
#define ABI "x86"
#elif defined(__x86_64__)
#define ABI "x86_64"
#elif defined(__mips64)  /* mips64el-* toolchain defines __mips__ too */
#define ABI "mips64"
#elif defined(__mips__)
#define ABI "mips"
#elif defined(__aarch64__)
#define ABI "arm64-v8a"
#else
#define ABI "unknown"
#endif

    return (*env)->NewStringUTF(env, "Hello from JNI !  Compiled with ABI " ABI ".");
}

需要注意的是方法名Java_duan_key_MainActivity_stringFromJNI

duan_key_MainActivity 表示呼叫該c檔案方法的包名和類名

stringFromJNI 表示呼叫該c檔案方法的方法

5、Java的呼叫類如下

package duan.key;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView textView = (TextView) findViewById(R.id.tv_info);
        textView.setText(stringFromJNI());
    }

    public native String stringFromJNI();

    static {
        System.loadLibrary("native-lib");
    }
}

6、最後執行的結果如下

這裡寫圖片描述