本篇重点内容,Kotlin通过JNI直接调用C,C调用C++相关的类。实际上只是JAVA换成Kotlin的写法而已。

一.添加Kotlin类

声明KotlinPerson类

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

class KotlinPerson {

    var mPerson: Long = 0

    init {
        mPerson = createPerson()
    }

    fun setPersonName(name: String) {
        setName(mPerson, name)
    }

    fun personName(): String {
        return name(mPerson)
    }


    fun setPersonAge(age: Int) {
        setAge(mPerson,age)
    }

    fun personAge() : Int {
        return age(mPerson)
    }

    companion object {
        init {
            System.loadLibrary("JNIPerson")
        }
    }

    external fun createPerson(): Long
    external fun destroyPerson(person: Long)
    external fun setName(person: Long, name: String)
    external fun name(person: Long): String
    external fun setAge(person: Long, age: Int)
    external fun age(person: Long): Int
}

二.添加JNI接口及实现

JNIPerson.c内容如下:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203

	#include "JNIPerson.h"
	#include "PersonExtension.hpp"
	#include "JNILog.h"
	#include <stdlib.h>
	#include <string.h>
	
	
	  /*
	   * Class:     com_ksnowlv_hellojniforjava_JavaCallJNI
	   * Method:    createPerson
	   * Signature: ()Ljava/lang/Object;
	   */
	JNIEXPORT jlong JNICALL Java_com_ksnowlv_kotlinjni_KotlinPerson_createPerson
	    (JNIEnv *env, jobject cls) {
	
	
	      void *p = createPerson();
	
	      return (jlong)p;
	    }
	
	  /*
	   * Class:     com_ksnowlv_hellojniforjava_JavaCallJNI
	   * Method:    destroyPerson
	   * Signature: (Ljava/lang/Object;)V
	   */
	  JNIEXPORT void JNICALL Java_com_ksnowlv_kotlinjni_KotlinPerson_destroyPerson
	    (JNIEnv *env, jobject cls, jlong o) {
	
	        destroyPerson((void*)o);
	    }
	
	  /*
	   * Class:     com_ksnowlv_hellojniforjava_JavaCallJNI
	   * Method:    setPersonAge
	   * Signature: (Ljava/lang/Object;I)V
	   */
	  JNIEXPORT void JNICALL Java_com_ksnowlv_kotlinjni_KotlinPerson_setAge
	    (JNIEnv *env, jobject cls, jlong o, jint value) {
	      setPersonAge((void*)o,value);
	    }
	
	  /*
	   * Class:     com_ksnowlv_hellojniforjava_JavaCallJNI
	   * Method:    personAge
	   * Signature: (Ljava/lang/Object;)I
	   */
	  JNIEXPORT jint JNICALL Java_com_ksnowlv_kotlinjni_KotlinPerson_age
	    (JNIEnv *env, jobject cls, jlong o) {
	     return personAge((void*)o);
	    }
	
	
	  JNIEXPORT void JNICALL Java_com_ksnowlv_kotlinjni_KotlinPerson_setName
	    (JNIEnv *env, jobject cls, jlong o, jstring string) {
	
	      int length1 = (*env)->GetStringLength(env, string);
	      const jchar * jcstr = (*env)->GetStringChars(env, string, NULL);
	
	
	       jclass   clsstring   =   (*env)->FindClass(env,"java/lang/String");  //String
	       jstring   code   =   (*env)->NewStringUTF(env,"UTF-8"); //"UTF-8"
	       jmethodID   methodID   =   (*env)->GetMethodID(env,clsstring,   "getBytes",   "(Ljava/lang/String;)[B"); //getBytes();
	       jbyteArray   byteArray =   (jbyteArray)(*env)->CallObjectMethod(env,string,methodID,code);
	       jsize   length   =   (*env)->GetArrayLength(env,byteArray);
	       jbyte*   bytes   =   (*env)->GetByteArrayElements(env,byteArray,JNI_FALSE);
	
	
	       if( length > 0){
	
	          char* buf =   (char*)malloc(length+1);         //"\0"
	          memcpy(buf,bytes,length);
	          buf[length]=0;
	          setPersonName((void*)o, buf);
	          LOGI("set name = %s",buf);
	          free(buf);
	       }else{
	          setPersonName((void*)o, NULL);
	       }
	
	       (*env)->ReleaseByteArrayElements(env,byteArray,bytes,0);  //释放内存空间
	    }
	
	  /*
	   * Class:     com_ksnowlv_hellojniforjava_JavaCallJNI
	   * Method:    personName
	   * Signature: (Ljava/lang/Object;)Ljava/lang/String;
	   */
	  JNIEXPORT jstring JNICALL Java_com_ksnowlv_kotlinjni_KotlinPerson_name
	    (JNIEnv *env, jobject cls, jlong o) {
	
	      LOGI("jni get name");
	      const char *name = personName((void*)o);
	
	      if ( name != NULL ) {
	          LOGI("jni name = %s",name);
	          return  (*env)->NewStringUTF(env,name);
	      }else {
	          return (*env)->NewStringUTF(env,"");
	      }
	    }
	#include "JNIPerson.h"
#include "PersonExtension.hpp"
#include "JNILog.h"
#include <stdlib.h>
#include <string.h>


  /*
   * Class:     com_ksnowlv_hellojniforjava_JavaCallJNI
   * Method:    createPerson
   * Signature: ()Ljava/lang/Object;
   */
JNIEXPORT jlong JNICALL Java_com_ksnowlv_kotlinjni_KotlinPerson_createPerson
    (JNIEnv *env, jobject cls) {


      void *p = createPerson();

      return (jlong)p;
    }

  /*
   * Class:     com_ksnowlv_hellojniforjava_JavaCallJNI
   * Method:    destroyPerson
   * Signature: (Ljava/lang/Object;)V
   */
  JNIEXPORT void JNICALL Java_com_ksnowlv_kotlinjni_KotlinPerson_destroyPerson
    (JNIEnv *env, jobject cls, jlong o) {

        destroyPerson((void*)o);
    }

  /*
   * Class:     com_ksnowlv_hellojniforjava_JavaCallJNI
   * Method:    setPersonAge
   * Signature: (Ljava/lang/Object;I)V
   */
  JNIEXPORT void JNICALL Java_com_ksnowlv_kotlinjni_KotlinPerson_setAge
    (JNIEnv *env, jobject cls, jlong o, jint value) {
      setPersonAge((void*)o,value);
    }

  /*
   * Class:     com_ksnowlv_hellojniforjava_JavaCallJNI
   * Method:    personAge
   * Signature: (Ljava/lang/Object;)I
   */
  JNIEXPORT jint JNICALL Java_com_ksnowlv_kotlinjni_KotlinPerson_age
    (JNIEnv *env, jobject cls, jlong o) {
     return personAge((void*)o);
    }


  JNIEXPORT void JNICALL Java_com_ksnowlv_kotlinjni_KotlinPerson_setName
    (JNIEnv *env, jobject cls, jlong o, jstring string) {

      int length1 = (*env)->GetStringLength(env, string);
      const jchar * jcstr = (*env)->GetStringChars(env, string, NULL);


       jclass   clsstring   =   (*env)->FindClass(env,"java/lang/String");  //String
       jstring   code   =   (*env)->NewStringUTF(env,"UTF-8"); //"UTF-8"
       jmethodID   methodID   =   (*env)->GetMethodID(env,clsstring,   "getBytes",   "(Ljava/lang/String;)[B"); //getBytes();
       jbyteArray   byteArray =   (jbyteArray)(*env)->CallObjectMethod(env,string,methodID,code);
       jsize   length   =   (*env)->GetArrayLength(env,byteArray);
       jbyte*   bytes   =   (*env)->GetByteArrayElements(env,byteArray,JNI_FALSE);


       if( length > 0){

          char* buf =   (char*)malloc(length+1);         //"\0"
          memcpy(buf,bytes,length);
          buf[length]=0;
          setPersonName((void*)o, buf);
          LOGI("set name = %s",buf);
          free(buf);
       }else{
          setPersonName((void*)o, NULL);
       }

       (*env)->ReleaseByteArrayElements(env,byteArray,bytes,0);  //释放内存空间
    }

  /*
   * Class:     com_ksnowlv_hellojniforjava_JavaCallJNI
   * Method:    personName
   * Signature: (Ljava/lang/Object;)Ljava/lang/String;
   */
  JNIEXPORT jstring JNICALL Java_com_ksnowlv_kotlinjni_KotlinPerson_name
    (JNIEnv *env, jobject cls, jlong o) {

      LOGI("jni get name");
      const char *name = personName((void*)o);

      if ( name != NULL ) {
          LOGI("jni name = %s",name);
          return  (*env)->NewStringUTF(env,name);
      }else {
          return (*env)->NewStringUTF(env,"");
      }
    }

三.调用

1
2
3
4
5
6
7
8
	
	var person = KotlinPerson()
	person.setPersonAge(10)
	
	Log.i("----Android age = ", "" + person.personAge())
	person.setPersonName("ksnowlv(律威)")
	
	Log.i("----Android name = ", person.personName())

四.日志输出