关于NDK和JNI相关介绍就不多提了,直接进入实现
准备工作
1.配置环境变量(如果没有下载ndk的可以去ndk下载):
配置系统变量path加入类似:E:\Studio\sdk\ndk-bundle你ndk的路径 (配置完需要重新打开一个终端测试下ndk-build出现下面提示说明配置成功)
2.配置local.properties
加入你的路径下的ndk
ndk.dir=E\:\\Studio\\sdk\\ndk-bundle
sdk.dir=E\:\\Studio\\sdk
3.配置gradle.properties
加入android.useDeprecatedNdk=true
4.配置app的build.gradle(指定so包的地址,我这里设置了libs,你们可以自己指定文件夹,最近studio3.0测试发现找不到so,需要设置成src/main/libs才可以)
android {
......
sourceSets.main {
jniLibs.srcDir "src/main/libs"
jni.srcDirs = []//disable automatic ndk-build call
}
}
5.配置app的build.gradle(设置支持的so,移动端armeabi为主)
defaultConfig {
...
ndk {
//设置支持的SO库架构
abiFilters'armeabi' ,'x86','armeabi-v7a','x86_64','arm64-v8a'
}
}
编写代码
1.创建工具类JniMethod(其中System.loadLibrary("JniMethod");中的"JniMethod"是so文件的名字去掉"lib",用来指定要用到的so文件,有的人可能getNativeString会报红线,不用管,手动去除就好)
public class JniMethod {
static {
System.loadLibrary("JniMethod");
}
public static native String getNativeString(String s);
}
2.Activity调用c/c++方法(就一句话很简单)
textView=(TextView)findViewById(R.id.mtext);
button=(Button) findViewById(R.id.mbutton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText(getNativeString("666666666666"));
}
});
生成so文件
1.生成class文件
指定路径到你的JniMethod.java下
2.指定路径到java文件下生成.h文件
生成了.h文件,为了方便查看和调用改名为JniMethod.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* Header for class com_sinosoft_testjni_JniMethod */
#ifndef _Included_com_sinosoft_testjni_JniMethod
#define _Included_com_sinosoft_testjni_JniMethod
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_sinosoft_testjni_JniMethod
* Method: getNativeString
* Signature: (Ljava/lang/String;)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_sinosoft_testjni_JniMethod_getNativeString
(JNIEnv *, jclass, jstring);
#ifdef __cplusplus
}
#endif
#endif
3.main下创建文件夹jni 并把JniMethod.h导入进去,并创建JniMethod.c、Android.mk、Application.mk文件(.c文件格式和.h格式一致,为:Java_包名类名函数名,如果已经有.c文件直接修改方法名即可)
(1)JniMethod.c
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include
#include
/* This is a trivial JNI example where we use a native method
* to return a new VM String. See the corresponding Java source
* file located at:
*
* E:\Lilac-Applications\Test\app\src\main\java\com\lilacouyang\firstjni\JniMethod.java
*/
JNIEXPORT jstring JNICALL
Java_com_sinosoft_testjni_JniMethod_getNativeString(JNIEnv *env, jobject thiz, jstring string)
{
#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
char* str=(char*)(*env)->GetStringUTFChars(env,string,NULL);
char* hellostr ="Hello from JNI ! Compiled with ABI " ABI ".";
strcat(str,hellostr);
return (*env)->NewStringUTF(env,str);
}
(2)Android.mk
# Copyright (C) 2009 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
LOCAL_PATH := $(call my-dir) #提定当前路径
include $(CLEAR_VARS) #清除全局配置变量,LOCAL_XXX,除了
LOCAL_PATH
LOCAL_MODULE := JniMethod #指定生成动态库名JniMethod,生成的动态库
文件JniMethod.so
LOCAL_SRC_FILES := JniMethod.c #指定生成动态库的源文件
include $(BUILD_SHARED_LIBRARY) #提定生成动态库
注意把LOCAL_MODULE 名字改成和你的.h/.c文件一致
(3)Application.mk(设置so指的是CPU架构平台all是全部,可以修改,指定多个平台。如: APP_ABI := armeabi armeabi-v7a x86 mips)
APP_ABI := all
4.进入jni目录执行ndk-build命令,把生成的so文件导入到你设置的文件夹上(我设置的是lib目录下)
5.运行项目
源码地址