Skip to content

Commit 5a78a61

Browse files
authored
Fix rustls-platform-verifier initialization (#155)
## 🎟️ Tracking <!-- Paste the link to the Jira or GitHub issue or otherwise describe / point to where this change is coming from. --> ## 📔 Objective Update rustls-platform-verifier initialization on Android to support being manually loaded using `System.loadLibrary()`. This bypasses the need to dynamically load `JNI_GetCreatedJavaVMs`, which is not available for APIs <31. I've still left the original code that calls `JNI_GetCreatedJavaVMs` as a fallback, but once the Android APP is on minAPI >31, we should just statically call `JNI_GetCreatedJavaVMs` using `jni::sys::JNI_GetCreatedJavaVMs`. ## ⏰ Reminders before review - Contributor guidelines followed - All formatters and local linters executed and passed - Written new unit and / or integration tests where applicable - Protected functional changes with optionality (feature flags) - Used internationalization (i18n) for all UI strings - CI builds passed - Communicated to DevOps any deployment requirements - Updated any necessary documentation (Confluence, contributing docs) or informed the documentation team ## 🦮 Reviewer guidelines <!-- Suggested interactions but feel free to use (or not) as you desire! --> - 👍 (`:+1:`) or similar for great changes - 📝 (`:memo:`) or ℹ️ (`:information_source:`) for notes or general info - ❓ (`:question:`) for questions - 🤔 (`:thinking:`) or 💭 (`:thought_balloon:`) for more open inquiry that's not quite a confirmed issue and could potentially benefit from discussion - 🎨 (`:art:`) for suggestions / improvements - ❌ (`:x:`) or ⚠️ (`:warning:`) for more significant problems or concerns needing attention - 🌱 (`:seedling:`) or ♻️ (`:recycle:`) for future improvements or indications of technical debt - ⛏ (`:pick:`) for minor or nitpick changes
1 parent 795fdca commit 5a78a61

File tree

4 files changed

+40
-11
lines changed

4 files changed

+40
-11
lines changed

crates/bitwarden-uniffi/kotlin/app/src/main/java/com/bitwarden/myapplication/MainActivity.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ class MainActivity : FragmentActivity() {
8989

9090
private var accessToken = ""
9191

92+
companion object {
93+
init {
94+
System.loadLibrary("bitwarden_uniffi");
95+
}
96+
}
97+
9298
override fun onCreate(savedInstanceState: Bundle?) {
9399
super.onCreate(savedInstanceState)
94100
biometric = Biometric(this)

crates/bitwarden-uniffi/kotlin/publish-local.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ cross build -p bitwarden-uniffi --release --target=aarch64-linux-android
1212
mv $SDK_REPO_ROOT/target/aarch64-linux-android/release/libbitwarden_uniffi.so ./sdk/src/main/jniLibs/arm64-v8a/libbitwarden_uniffi.so
1313

1414
# Build other archs
15-
if [ $1 = "all" ]; then
15+
if [ "$1" = "all" ]; then
1616
echo "Building for all architectures"
1717

1818
cross build -p bitwarden-uniffi --release --target=armv7-linux-androideabi

crates/bitwarden-uniffi/src/android_support.rs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,43 @@
1-
use std::error::Error;
1+
use std::{error::Error, sync::OnceLock};
22

33
use jni::sys::{jint, jsize, JavaVM};
44

5-
pub fn init() {
6-
static ANDROID_INIT: std::sync::Once = std::sync::Once::new();
5+
pub static JAVA_VM: OnceLock<jni::JavaVM> = OnceLock::new();
6+
7+
// This function is called when the Android app calls `System.loadLibrary("bitwarden_uniffi")`
8+
// Important: This function must be named `JNI_OnLoad` or otherwise it won't be called
9+
#[allow(non_snake_case)]
10+
#[no_mangle]
11+
pub extern "system" fn JNI_OnLoad(vm_ptr: jni::JavaVM, _reserved: *mut std::ffi::c_void) -> jint {
12+
log::info!("JNI_OnLoad initializing");
13+
JAVA_VM.get_or_init(|| vm_ptr);
14+
jni::sys::JNI_VERSION_1_6
15+
}
716

17+
pub fn init() {
818
fn init_inner() -> Result<(), Box<dyn Error>> {
9-
let jvm = java_vm()?;
19+
let jvm = match JAVA_VM.get() {
20+
Some(jvm) => {
21+
log::info!("JavaVM already initialized");
22+
jvm
23+
}
24+
None => {
25+
log::info!("JavaVM not initialized, initializing now");
26+
let jvm = java_vm()?;
27+
JAVA_VM.get_or_init(|| jvm)
28+
}
29+
};
30+
1031
let mut env = jvm.attach_current_thread_permanently()?;
32+
log::info!("Initializing Android verifier");
1133
init_verifier(&mut env)?;
34+
log::info!("SDK Android support initialized");
1235
Ok(())
1336
}
1437

15-
ANDROID_INIT.call_once(|| {
16-
if let Err(e) = init_inner() {
17-
log::error!("Failed to initialize Android support: {}", e);
18-
}
19-
});
38+
if let Err(e) = init_inner() {
39+
log::error!("Failed to initialize Android support: {:#?}", e);
40+
}
2041
}
2142

2243
type JniGetCreatedJavaVms =

crates/bitwarden-uniffi/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ fn init_logger() {
107107

108108
#[cfg(target_os = "android")]
109109
android_logger::init_once(
110-
android_logger::Config::default().with_max_level(uniffi::deps::log::LevelFilter::Info),
110+
android_logger::Config::default()
111+
.with_tag("com.bitwarden.sdk")
112+
.with_max_level(uniffi::deps::log::LevelFilter::Info),
111113
);
112114
}

0 commit comments

Comments
 (0)