NDK JNI开发简单跑通

关于NDK和JNI相关介绍就不多提了,直接进入实现

准备工作

1.配置环境变量(如果没有下载ndk的可以去ndk下载):

配置系统变量path加入类似:E:\Studio\sdk\ndk-bundle你ndk的路径 (配置完需要重新打开一个终端测试下ndk-build出现下面提示说明配置成功)

image.png

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下

image.png

2.指定路径到java文件下生成.h文件

image.png

生成了.h文件,为了方便查看和调用改名为JniMethod.h

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* 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 <string.h>
#include <jni.h>

/* 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目录下)


image.png

5.运行项目

源码地址

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,607评论 6 507
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,239评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,960评论 0 355
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,750评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,764评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,604评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,347评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,253评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,702评论 1 315
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,893评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,015评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,734评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,352评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,934评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,052评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,216评论 3 371
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,969评论 2 355

推荐阅读更多精彩内容