github.com/pojntfx/hydrapp/hydrapp@v0.0.0-20240516002902-d08759d6ca9f/pkg/renderers/apk/implementation.c.tpl (about)

     1  #include "hydrapp_android.h"
     2  #include <stdlib.h>
     3  #include <jni.h>
     4  #include <string.h>
     5  
     6  jstring get_java_string(JNIEnv *env, char *c_string) {
     7    return (*env)->NewStringUTF(env, c_string);
     8  }
     9  
    10  char* get_c_string(JNIEnv *env, jstring java_string) {
    11      const char *raw_utf8 = (*env)->GetStringUTFChars(env, java_string, NULL);
    12      jsize java_string_len = (*env)->GetStringUTFLength(env, java_string);
    13      
    14      char *c_string = (char *)malloc(java_string_len + 1); // Add additional byte for null terminator
    15      if (c_string == NULL) {
    16          (*env)->ReleaseStringUTFChars(env, java_string, raw_utf8);
    17  
    18          return NULL;
    19      }
    20      
    21      memcpy(c_string, raw_utf8, java_string_len);
    22      c_string[java_string_len] = '\0'; // Add null terminator
    23  
    24      (*env)->ReleaseStringUTFChars(env, java_string, raw_utf8);
    25  
    26      return c_string;
    27  }