feat(sdk): add JNI bridge between Java NativeBridge and Rust C ABI

This commit is contained in:
2026-05-25 15:02:12 +08:00
parent 7104976bf9
commit d3d26ba9b4
4 changed files with 46 additions and 3 deletions
@@ -18,7 +18,7 @@ import cn.craftlabs.auth.internal.NativeBridge;
*/ */
public final class BitAnswerProvider implements AuthProvider { public final class BitAnswerProvider implements AuthProvider {
static { static {
System.loadLibrary("craftlabs_auth_bitanswer"); System.loadLibrary("craftlabs_auth_core");
} }
private long nativeHandle; private long nativeHandle;
-1
View File
@@ -16,7 +16,6 @@ fn main() {
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap()); let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
fs::write(out_dir.join("build_hash.txt"), format!("{}\n", hash_hex)).unwrap(); fs::write(out_dir.join("build_hash.txt"), format!("{}\n", hash_hex)).unwrap();
// 嵌入 RSA 公钥(用于 selfhosted 验签)
let pubkey_path = PathBuf::from(&manifest_dir).join("embedded").join("pubkey.pem"); let pubkey_path = PathBuf::from(&manifest_dir).join("embedded").join("pubkey.pem");
if let Ok(pubkey) = fs::read_to_string(&pubkey_path) { if let Ok(pubkey) = fs::read_to_string(&pubkey_path) {
let trimmed = pubkey.trim(); let trimmed = pubkey.trim();
+45
View File
@@ -0,0 +1,45 @@
/* JNI bridge — connects Java NativeBridge to Rust C ABI
* Compile: gcc -shared -fPIC -I$JAVA_HOME/include -I$JAVA_HOME/include/darwin
* -I native/include -o libcraftlabs_jni_bridge.so jni_bridge.c -L. -lcraftlabs_auth_core
*
* Then: java -Djava.library.path=. ... loads both libraries.
* Or rename the output to craftlabs_auth_core and load it directly.
*/
#include <jni.h>
#include "craftlabs_auth.h"
JNIEXPORT jlong JNICALL Java_cn_craftlabs_auth_internal_NativeBridge_nativeInitialize(
JNIEnv *env, jclass clazz, jstring configJson) {
const char *utf = (*env)->GetStringUTFChars(env, configJson, NULL);
jlong handle = (jlong)(intptr_t)craft_initialize(utf);
(*env)->ReleaseStringUTFChars(env, configJson, utf);
return handle;
}
JNIEXPORT void JNICALL Java_cn_craftlabs_auth_internal_NativeBridge_nativeDestroy(
JNIEnv *env, jclass clazz, jlong handle) {
(void)env; (void)clazz;
if (handle) craft_destroy((AuthHandle)(intptr_t)handle);
}
JNIEXPORT jobject JNICALL Java_cn_craftlabs_auth_internal_NativeBridge_nativeActivate(
JNIEnv *env, jclass clazz, jlong handle, jstring licenseKey) {
(void)clazz;
const char *key = (*env)->GetStringUTFChars(env, licenseKey, NULL);
AuthResult r = craft_activate((AuthHandle)(intptr_t)handle, key);
(*env)->ReleaseStringUTFChars(env, licenseKey, key);
jclass cls = (*env)->FindClass(env, "cn/craftlabs/auth/AuthResult");
jmethodID ctor = (*env)->GetMethodID(env, cls, "<init>", "(ZLjava/lang/String;)V");
jstring msg = (*env)->NewStringUTF(env, r.message ? r.message : "");
return (*env)->NewObject(env, cls, ctor, r.success ? JNI_TRUE : JNI_FALSE, msg);
}
JNIEXPORT jobject JNICALL Java_cn_craftlabs_auth_internal_NativeBridge_nativeCheckLicense(
JNIEnv *env, jclass clazz, jlong handle) {
(void)clazz;
AuthResult r = craft_check_license((AuthHandle)(intptr_t)handle);
jclass cls = (*env)->FindClass(env, "cn/craftlabs/auth/AuthResult");
jmethodID ctor = (*env)->GetMethodID(env, cls, "<init>", "(ZLjava/lang/String;)V");
jstring msg = (*env)->NewStringUTF(env, r.message ? r.message : "");
return (*env)->NewObject(env, cls, ctor, r.success ? JNI_TRUE : JNI_FALSE, msg);
}
-1
View File
@@ -9,7 +9,6 @@ mod session;
pub mod crypto; pub mod crypto;
pub mod device; pub mod device;
pub mod provider_selfhosted; pub mod provider_selfhosted;
use trait_provider::{Provider, ActivateResponse, HeartbeatResponse, LicenseStatus}; use trait_provider::{Provider, ActivateResponse, HeartbeatResponse, LicenseStatus};
use provider_selfhosted::SelfHostedProvider; use provider_selfhosted::SelfHostedProvider;