Repository: cloudflare/boringtun (language: rust)

Issue:
JNI Bindings require using Kotlin
The exported names require you to use Kotlin in order to use JNI.

For example `Java_com_cloudflare_app_boringtun_BoringTunJNI_00024Companion_x25519_1secret_1key`,
requires a class with a `companion` object, making it incompatible with Java only projects.

Removing `00024Companion` from all exported names should do the trick. Adding a `@JVMStatic` annotation to all the Kotlin methods will generate the same names as their Java counterparts. 

In the following example, both will link to `Java_com_cloudflare_app_boringtun_BoringTunJNI_x25519_1secret_1key`.

```kotlin
class BoringTunJNI {
    companion object {
        init {
            System.loadLibrary("boringtun")
        }

    @JvmStatic
    external fun x25519_secret_key(): ByteArray
}
```

```java
public class BoringTunJNI {
    static {
        System.loadLibrary("boringtun");
    }

    public static native byte[] x25519_secret_key();
}
```

Also, the functions are expecting a `JClass` argument instead of a `JInstance`, therefore the Java methods must all be static. The companion object methods without `@JvmStatic` are instance methods.


Discussion / hints:

Fix patch:
diff --git a/src/jni.rs b/src/jni.rs
index 75ba107d..271e9393 100644
--- a/src/jni.rs
+++ b/src/jni.rs
@@ -35,7 +35,7 @@ pub extern "C" fn log_print(_log_string: *const c_char) {
 
 /// Generates new x25519 secret key and converts into java byte array.
 #[no_mangle]
-#[export_name = "Java_com_cloudflare_app_boringtun_BoringTunJNI_00024Companion_x25519_1secret_1key"]
+#[export_name = "Java_com_cloudflare_app_boringtun_BoringTunJNI_x25519_1secret_1key"]
 pub extern "C" fn generate_secret_key(env: JNIEnv, _class: JClass) -> jbyteArray {
     match env.byte_array_from_slice(&x25519_secret_key().as_bytes()) {
         Ok(v) => v,
@@ -45,7 +45,7 @@ pub extern "C" fn generate_secret_key(env: JNIEnv, _class: JClass) -> jbyteArray
 
 /// Computes public x25519 key from secret key and converts into java byte array.
 #[no_mangle]
-#[export_name = "Java_com_cloudflare_app_boringtun_BoringTunJNI_00024Companion_x25519_1public_1key"]
+#[export_name = "Java_com_cloudflare_app_boringtun_BoringTunJNI_x25519_1public_1key"]
 pub unsafe extern "C" fn generate_public_key1(
     env: JNIEnv,
     _class: JClass,
@@ -73,7 +73,7 @@ pub unsafe extern "C" fn generate_public_key1(
 
 /// Converts x25519 key to hex string.
 #[no_mangle]
-#[export_name = "Java_com_cloudflare_app_boringtun_BoringTunJNI_00024Companion_x25519_1key_1to_1hex"]
+#[export_name = "Java_com_cloudflare_app_boringtun_BoringTunJNI_x25519_1key_1to_1hex"]
 pub unsafe extern "C" fn convert_x25519_key_to_hex(
     env: JNIEnv,
     _class: JClass,
@@ -99,7 +99,7 @@ pub unsafe extern "C" fn convert_x25519_key_to_hex(
 
 /// Converts x25519 key to base64 string.
 #[no_mangle]
-#[export_name = "Java_com_cloudflare_app_boringtun_BoringTunJNI_00024Companion_x25519_1key_1to_1base64"]
+#[export_name = "Java_com_cloudflare_app_boringtun_BoringTunJNI_x25519_1key_1to_1base64"]
 pub unsafe extern "C" fn convert_x25519_key_to_base64(
     env: JNIEnv,
     _class: JClass,
@@ -126,7 +126,7 @@ pub unsafe extern "C" fn convert_x25519_key_to_base64(
 
 /// Creates new tunnel
 #[no_mangle]
-#[export_name = "Java_com_cloudflare_app_boringtun_BoringTunJNI_00024Companion_new_1tunnel"]
+#[export_name = "Java_com_cloudflare_app_boringtun_BoringTunJNI_new_1tunnel"]
 pub unsafe extern "C" fn create_new_tunnel(
     env: JNIEnv,
     _class: JClass,
@@ -154,7 +154,7 @@ pub unsafe extern "C" fn create_new_tunnel(
 
 /// Encrypts raw IP packets into WG formatted packets.
 #[no_mangle]
-#[export_name = "Java_com_cloudflare_app_boringtun_BoringTunJNI_00024Companion_wireguard_1write"]
+#[export_name = "Java_com_cloudflare_app_boringtun_BoringTunJNI_wireguard_1write"]
 pub unsafe extern "C" fn encrypt_raw_packet(
     env: JNIEnv,
     _class: JClass,
@@ -189,7 +189,7 @@ pub unsafe extern "C" fn encrypt_raw_packet(
 
 /// Decrypts WG formatted packets into raw IP packets.
 #[no_mangle]
-#[export_name = "Java_com_cloudflare_app_boringtun_BoringTunJNI_00024Companion_wireguard_1read"]
+#[export_name = "Java_com_cloudflare_app_boringtun_BoringTunJNI_wireguard_1read"]
 pub unsafe extern "C" fn decrypt_to_raw_packet(
     env: JNIEnv,
     _class: JClass,
@@ -225,7 +225,7 @@ pub unsafe extern "C" fn decrypt_to_raw_packet(
 
 /// Periodic function that writes WG formatted packets into destination buffer
 #[no_mangle]
-#[export_name = "Java_com_cloudflare_app_boringtun_BoringTunJNI_00024Companion_wireguard_1tick"]
+#[export_name = "Java_com_cloudflare_app_boringtun_BoringTunJNI_wireguard_1tick"]
 pub unsafe extern "C" fn run_periodic_task(
     env: JNIEnv,
     _class: JClass,


Test patch: