用法
git clone https://github.com/codesharp-hu/JNIGenerator.git
cd JNIGenerator
mkdir generated
cd JNIGenerator
dotnet run -- template/config.json
编辑template/config.json到
#ifndef MESSAGE_H
#define MESSAGE_H
struct Message {
char* subject;
char* text;
};
int sendMessage(struct Message message);
#endif
Examle输出
Message.kt
//Generated code. Do not edit!
package com.jnigen.model
data class Message (var subject: String = String(), var text: String = String())
JniApi.kt
//Generated code. Do not edit!
package com.jnigen
import com.jnigen.model.*
class JniApi {
external fun sendMessage(message: Message): Int
companion object {
init {
System.loadLibrary("native-lib")
}
}
}
jni-wrapper.c
您可以在这里找到生成的示例JNI代码:https://github.com/codesharp hu/JNIGenerator/blob/master/example output/jni wrapper.c
规则
结构体
支持以下形式的结构:
struct Message {
int isUrgent;
char* subject;
char* text;
};
重要的是,左括号({)与结构体写在同一行中。typedef struct Message {
int isUrgent;
char* subject;
char* text;
} Message;
指针
指针被解释为数组,字符指针被解释为字符串。除了char*之外,不要从函数返回原始指针,也不要将它们用作参数。指针(数组)必须始终包装在结构中,并且必须具有相应的长度变量。此长度变量必须:
struct Contact {
char* name;
char* email;
};
struct Message {
int isUrgent;
char* subject;
char* text;
struct Contact sender;
int addresseesLength;
struct Contact* addressees;
};
等长数组struct Message {
int isUrgent;
char* subject;
char* text;
struct Contact sender;
int addresseesLength;
struct Contact* addressees;//array-length:5
};
排除在JNI之外struct Message {
int isUrgent;
char* subject;
char* text;
};//no-jni
排除函数:
void sendMessage(struct Message message);//no-jni