使用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、最后运行的结果如下

这里写图片描述

智能推荐

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告