Skip to content

fix(ffi): Using rustls in Android#6306

Merged
jmartinesp merged 1 commit intomainfrom
fix/rustls-with-android
Mar 18, 2026
Merged

fix(ffi): Using rustls in Android#6306
jmartinesp merged 1 commit intomainfrom
fix/rustls-with-android

Conversation

@jmartinesp
Copy link
Contributor

@jmartinesp jmartinesp commented Mar 17, 2026

It turns out on Android, rustls needs a custom setup and adding the rustls-platform-verifier-android library that's not available on Maven.

Then, from the Android clients we'd need to call some exposed JNI function so we can provide a JVM context from where Rust can take the Application component and access its contents to read its credentials storage. Thanks to some tricks we can use libloading to simulate this call from Rust itself and properly initialise the platform verifier.

Note self-signed certificates will no longer work with these changes on Android, and providing them in ClientBuilder::add_root_certificates will make most requests fail. This can be handled separately.

  • I've documented the public API Changes in the appropriate CHANGELOG.md files.
  • This PR was made with the help of AI.

Signed-off-by:

@codecov
Copy link

codecov bot commented Mar 17, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.82%. Comparing base (60ce062) to head (c49ad73).
⚠️ Report is 8 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #6306   +/-   ##
=======================================
  Coverage   89.81%   89.82%           
=======================================
  Files         376      376           
  Lines      102447   102447           
  Branches   102447   102447           
=======================================
+ Hits        92017    92019    +2     
+ Misses       6850     6843    -7     
- Partials     3580     3585    +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@codspeed-hq
Copy link

codspeed-hq bot commented Mar 17, 2026

Merging this PR will not alter performance

✅ 50 untouched benchmarks


Comparing fix/rustls-with-android (c49ad73) with main (60ce062)

Open in CodSpeed

@jmartinesp jmartinesp force-pushed the fix/rustls-with-android branch from cbd1f9a to eea7063 Compare March 17, 2026 14:32
Comment on lines +748 to +752
#[cfg(target_os = "android")]
builder.on_thread_start(|| {
android_platform::android_attach_current_thread_permanently()
.expect("Failed to attach thread");
});
Copy link
Contributor Author

@jmartinesp jmartinesp Mar 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we have access to the JavaVM now, it seems like it's recommended to use this method to attach JVM threads to Rust ones according to uniffi.

@jmartinesp jmartinesp force-pushed the fix/rustls-with-android branch 2 times, most recently from 10f9f6b to 29298ab Compare March 17, 2026 15:29
@jmartinesp jmartinesp marked this pull request as ready for review March 17, 2026 15:40
@jmartinesp jmartinesp requested a review from a team as a code owner March 17, 2026 15:40
@jmartinesp jmartinesp requested review from stefanceriu and removed request for a team March 17, 2026 15:40
@stefanceriu
Copy link
Member

Thank you but no thank you, you don't want me reviewing this 😅

@stefanceriu stefanceriu requested review from poljar and removed request for stefanceriu March 17, 2026 15:54
Copy link
Contributor

@poljar poljar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I think all of this is making sense to me. Obviously I trust you that you checked that this actually works.

Note self-signed certificates will no longer work with these changes on Android, and providing them in ClientBuilder::add_root_certificates will make most requests fail. This can be handled separately.

This will be mostly done on the EXA side? I remember pushing a PR there which used this method to add custom certs, and now we can just use the system cert store for this, right?

@jmartinesp
Copy link
Contributor Author

Obviously I trust you that you checked that this actually works.

Now you made me anxious and I'll have to quadruple-check it!

This will be mostly done on the EXA side? I remember pushing a PR there which used this method to add custom certs, and now we can just use the system cert store for this, right?

Yeah, I think we'll just stop providing these custom certs manually in ClientBuilder.

@jmartinesp jmartinesp force-pushed the fix/rustls-with-android branch from 6feb4eb to 0918b5a Compare March 18, 2026 14:14
It turns out on Android, rustls needs [a custom setup](https://github.com/rustls/rustls-platform-verifier#android) and adding the `rustls-platform-verifier-android` library that's [not available on Maven](rustls/rustls-platform-verifier#115).

Then, from the Android clients we'd need to call some exposed JNI function so we can provide a JVM context from where Rust can take the `Application` component and access its contents to read its credentials storage. Thanks to some tricks we can use `libloading` to simulate this call from Rust itself and properly initialise the platform verifier.

Note self-signed certificates will no longer work with these changes on Android, and providing them in `ClientBuilder::add_root_certificates` will make most requests fail. This can be handled separately.
@jmartinesp jmartinesp force-pushed the fix/rustls-with-android branch from 0918b5a to c49ad73 Compare March 18, 2026 14:16
@jmartinesp jmartinesp merged commit 29aa3c2 into main Mar 18, 2026
56 of 57 checks passed
@jmartinesp jmartinesp deleted the fix/rustls-with-android branch March 18, 2026 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants