Skip to content

Commit 4906878

Browse files
committed
add JNI_OnLoad
1 parent 2a8fe5c commit 4906878

File tree

4 files changed

+31
-77
lines changed

4 files changed

+31
-77
lines changed

src/HttpsCaller.class

-1.76 KB
Binary file not shown.

src/call_java

-33.3 KB
Binary file not shown.

src/call_java.c

Lines changed: 14 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,39 @@
11
#include <jni.h>
2-
#include <stdio.h>
3-
#include <stdlib.h>
2+
#include <sqlite3ext.h>
3+
SQLITE_EXTENSION_INIT1
44

5-
static JavaVM *gJvm = NULL; // Global JVM pointer set by JNI_OnLoad
5+
JavaVM* gJvm = NULL; // Stored from JNI_OnLoad
66

7-
// Called when library is loaded by Android Runtime
87
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
98
gJvm = vm;
109
return JNI_VERSION_1_6;
1110
}
1211

13-
// Helper to get JNIEnv for current thread, attach if necessary
14-
static JNIEnv* getJNIEnv() {
15-
JNIEnv *env = NULL;
16-
if ((*gJvm)->GetEnv(gJvm, (void **)&env, JNI_VERSION_1_6) != JNI_OK) {
17-
if ((*gJvm)->AttachCurrentThread(gJvm, (void **)&env, NULL) != 0) {
12+
JNIEnv* getEnv() {
13+
JNIEnv* env;
14+
if ((*gJvm)->GetEnv(gJvm, (void**)&env, JNI_VERSION_1_6) != JNI_OK) {
15+
if ((*gJvm)->AttachCurrentThread(gJvm, &env, NULL) != 0) {
1816
return NULL;
1917
}
2018
}
2119
return env;
2220
}
2321

24-
void callHttpsFromC() {
25-
JNIEnv *env = getJNIEnv();
26-
if (env == NULL) {
27-
printf("Failed to get JNIEnv\n");
28-
return;
29-
}
30-
22+
const char* call_https_java(JNIEnv* env) {
3123
jclass cls = (*env)->FindClass(env, "HttpsCaller");
32-
if (cls == NULL) {
33-
printf("Failed to find HttpsCaller class\n");
34-
return;
35-
}
24+
if (!cls) return "Class not found";
3625

3726
jmethodID mid = (*env)->GetStaticMethodID(env, cls, "callHttps", "()Ljava/lang/String;");
38-
if (mid == NULL) {
39-
printf("Failed to find callHttps method\n");
40-
return;
41-
}
27+
if (!mid) return "Method not found";
4228

4329
jstring result = (jstring)(*env)->CallStaticObjectMethod(env, cls, mid);
44-
if (result == NULL) {
45-
printf("callHttps returned null\n");
46-
return;
47-
}
48-
49-
const char *str = (*env)->GetStringUTFChars(env, result, 0);
50-
printf("HTTPS Response: %s\n", str);
51-
(*env)->ReleaseStringUTFChars(env, result, str);
52-
}
53-
54-
JavaVM *jvm;
55-
JNIEnv *env;
56-
57-
void createJVM() {
58-
JavaVMInitArgs vm_args;
59-
JavaVMOption options[1];
60-
options[0].optionString = "-Djava.class.path=."; // Adjust if needed
61-
62-
vm_args.version = JNI_VERSION_1_6;
63-
vm_args.nOptions = 1;
64-
vm_args.options = options;
65-
vm_args.ignoreUnrecognized = JNI_FALSE;
30+
if (!result) return "Java method returned null";
6631

67-
jint res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
68-
if (res != JNI_OK) {
69-
fprintf(stderr, "Failed to create JVM\n");
70-
exit(1);
71-
}
72-
}
73-
74-
void destroyJVM() {
75-
(*jvm)->DestroyJavaVM(jvm);
76-
}
32+
const char* str = (*env)->GetStringUTFChars(env, result, 0);
7733

78-
int main() {
79-
createJVM();
34+
char* copied = strdup(str);
8035

81-
jclass cls = (*env)->FindClass(env, "HttpsCaller");
82-
if (cls == NULL) {
83-
printf("Class not found\n");
84-
destroyJVM();
85-
return 1;
86-
}
87-
88-
jmethodID mid = (*env)->GetStaticMethodID(env, cls, "callHttps", "()Ljava/lang/String;");
89-
if (mid == NULL) {
90-
printf("Method not found\n");
91-
destroyJVM();
92-
return 1;
93-
}
94-
95-
jstring result = (jstring)(*env)->CallStaticObjectMethod(env, cls, mid);
96-
const char *str = (*env)->GetStringUTFChars(env, result, 0);
97-
printf("Result: %s\n", str);
9836
(*env)->ReleaseStringUTFChars(env, result, str);
9937

100-
destroyJVM();
101-
return 0;
38+
return copied;
10239
}

src/network.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@
55
// Created by Marco Bambini on 12/12/24.
66
//
77

8+
extern const char* call_https_java(JNIEnv* env); // Declare your JNI function
9+
extern JNIEnv* getEnv(); // Declare getEnv from call_java.c
10+
811
#ifndef CLOUDSYNC_OMIT_NETWORK
912

1013
#include <stdint.h>
1114
#include "network.h"
1215
#include "dbutils.h"
1316
#include "utils.h"
1417
#include "curl/curl.h"
18+
#include <stdio.h>
19+
#include <android/log.h>
1520

1621
#define CLOUDSYNC_ENDPOINT_PREFIX "v1/cloudsync"
1722
#define CLOUDSYNC_ENDPOINT_UPLOAD "upload"
@@ -449,6 +454,18 @@ void cloudsync_network_init (sqlite3_context *context, int argc, sqlite3_value *
449454
// save site_id string representation: 01957493c6c07e14803727e969f1d2cc
450455
cloudsync_uuid_v7_stringify(site_id, data->site_id, false);
451456

457+
JNIEnv* env = getEnv();
458+
if (!env) {
459+
sqlite3_result_error(context, "Could not get JNIEnv");
460+
return;
461+
}
462+
463+
const char* javaResult = call_https_java(env);
464+
if (javaResult) {
465+
printf("Got from Java: %s\n", javaResult);
466+
__android_log_print(ANDROID_LOG_INFO, "MyNativeCode", "Got from Java: %s", javaResult);
467+
free((void*)javaResult);
468+
}
452469
// connection string is something like:
453470
// https://UUID.g5.sqlite.cloud:443/chinook.sqlite?apikey=hWDanFolRT9WDK0p54lufNrIyfgLZgtMw6tb6fbPmpo
454471
// or https://UUID.g5.sqlite.cloud:443/chinook.sqlite

0 commit comments

Comments
 (0)