Mac上AndroidStudio JNI 开发Demo

Mac上AndroidStudio JNI 开发Demo

配置

  • 下载 NDK和 CMake
  • 在local.properties中配置ndk 路径
  • 将ndk路径添加到系统的class path中 -(export PATH= P A T H : PATH: PATH:ANDROID_NDK_HOME/)

开发

  • 在 app/build.gradle中的defaultConfig中添加 :
ndk {
            moduleName "hello"
            abiFilters "armeabi", "armeabi-v7a"
        }

  • 编写 Java class 里面创建 native 方法
public class Md5Utils {
    
    
    static {
    
    
        System.loadLibrary("hello");//gradle中 ndk中的moduleName
    }

    public static native String sign(String source);
}

  • 在 Terminal中将位置cd到java文件夹下,执行
javah -jni 包名+类名(Md5Utils)
  • 上述命令会在Java 文件下生产 Md5Utils.h 文件(文件的命名规则为:包名中的.替换成_,然后加上类名.h)
  • 在 main 文件夹下创建 jni文件夹,将生产的.h文件copy到jni文件夹下,然后在jni文件夹下创建 .c/.cpp文件,然后在.c/.cpp文件中导入 .h文件,代码如下:

#include <jni.h>
#include <stdlib.h>
#include<string.h>
#include "com_example_demos_Md5Utils.h"

char* Jstring2Cstr(JNIEnv* env, jstring strs){
    
    

char* rtn = NULL;
jclass classString = (*env)->FindClass(env, "java/lang/String");
jstring strenCode = (*env)->NewStringUTF(env,"UTF-8");
jmethodID mid = (*env)->GetMethodID(env,classString,"getBytes","(Ljava/lang/String;)[B");

jbyteArray barr = (jbyteArray) (*env)->CallObjectMethod(env,strs,mid,strenCode);
jsize alen = (*env)->GetArrayLength(env,barr);
jbyte* ba = (*env)->GetByteArrayElements(env,barr,JNI_FALSE);
if(alen > 0){
    
    
rtn = (char*)malloc(alen+1);
memcpy(rtn,ba,alen);
rtn[alen]=0;
}
(*env)->ReleaseByteArrayElements(env,barr,ba,0);
return rtn;
}

 jstring JNICALL Java_com_example_demos_Md5Utils_sign
          (JNIEnv* env, jclass cls, jstring strs){
    
    
    char* str = "::this hello is from jni";

    char* cstr = Jstring2Cstr(env,strs);

    strcat(cstr,str);
    jstring string = (*(*env)).NewStringUTF(env, cstr);
    return string;
}

  • 在 jni 文件夹下创建 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_MODULE    := hello 
LOCAL_SRC_FILES := Md5Utils.c

include $(BUILD_SHARED_LIBRARY)
  • 在 项目的根目录下创建 Application.mk,内容如下:
# Build all machine code.
APP_ABI := ALL
  • 在 terminal 中 cd 到 jni 目录下,执行 ndk-build 命令 (若提示 command not found,在 ndk-build 前面拼接上ndk的路径:ndkPath/ndk-build)

  • 命令执行成功后,在main 文件下会生成 libs 文件夹,里面就有生产的.so文件。

  • 在 app文件夹下创建 jniLibs 文件夹,并在 app/build.gradle 中defaultConfig中添加 sourceSets.main.jniLibs.srcDirs = [‘jniLibs’] 指定 so 文件的lib路径,将上述命名生成的so文件 copy到 该目录下。

  • 然后就可以在 java代码中调用 native方法 ,实现 java调用c代码了。

猜你喜欢

转载自blog.csdn.net/genmenu/article/details/107247211