Rust Android Kotlin Class not found
⚓ Rust 📅 2025-09-28 👤 surdeus 👁️ 6Hello,
I am trying to create a rust app for android using Slint. This works already fine and the app is running. I am now trying to access android functionality using ndk context and jni.
I have a Kotlin file which contains a simple class
package com.gitlab.Murmele.weather
import android.content.Context
import android.content.SharedPreferences
import android.security.keystore.KeyGenParameterSpec
import android.security.keystore.KeyProperties
import android.util.Base64
import java.security.KeyStore
import javax.crypto.Cipher
import javax.crypto.KeyGenerator
import javax.crypto.SecretKey
import javax.crypto.spec.GCMParameterSpec
public class SecureStore {
    fun test(): String {
        return "Hello";
    }
}
In Rust I am accessing the context and the jvm like:
let ctx = ndk_context::android_context();
let jvm = unsafe { jni::JavaVM::from_raw(ctx.vm().cast())?};
Then I am using the Executor from jni to call my test function:
let exec = Executor::new(jvm);
exec.with_attached(|env| {
            let class_ctx = env.find_class("android/content/Context").expect("No context found");
            let cls = env.find_class("com/gitlab/Murmele/weather/SecureStore".to_owned()).unwrap();
            let result = env.call_static_method(cls, "test", "()Ljava/lang/String", &[])?;
            let s = result.l().ok();
            if let Some(a) = s {
                let string: String = env.get_string(&JString::from(a)).unwrap().into();
                println!("{}", string);
            }
            Ok::<(), jni::errors::Error>(())
        });
Finding class_ctx works fine, but it does not find my own class com/gitlab/Murmele/weather/SecureStore
I tried also the class loader:
exec.with_attached(|env| {
   let context = ndk_context::android_context();
   let context = unsafe { JObject::from_raw(context.context().cast()) };
   // Call Context.getClassLoader()
    let class_loader: JObject = env
        .call_method(&context, "getClassLoader", "()Ljava/lang/ClassLoader;", &[])
        .unwrap()
        .l()?;
    let class_name_jstr = env.new_string("com.gitlab.Murmele.weather.SecureStore").unwrap();
    let clazz: jni::objects::JClass = env
        .call_method(
                 class_loader,
                 "loadClass",
                 "(Ljava/lang/String;)Ljava/lang/Class;",
                 &[(&class_name_jstr).into()],
          )?
          .l()?
          .into();
          Ok::<(), jni::errors::Error>(())
}
But I always get the error message
32024 32060 E AndroidRuntime: java.lang.ClassNotFoundException: Didn't find class "com.gitlab.Murmele.weather.SecureStore" on path: DexPathList[[],nativeLibraryDirectories=[/data/app/~~RIuB9c4eYeRdL9vgA3ASgA==/com.gitlab.Murmele.weather-4Xpu0vtXfypKOXA6bdyjBA==/lib/arm64, /data/app/~~RIuB9c4eYeRdL9vgA3ASgA==/com.gitlab.Murmele.weather-4Xpu0vtXfypKOXA6bdyjBA==/base.apk!/lib/arm64-v8a, /system/lib64, /system/system_ext/lib64]]
This is how the apk content looks like using jadx:
I created the apk using xbuild which calls gradle internally.
What do I need to find my library?
1 post - 1 participant
🏷️ Rust_feed
