Skip to content

Add r8/proguard rules for JNA - #358

Merged
tmo1 merged 1 commit into
tmo1:masterfrom
chenxiaolong:proguard
Jul 31, 2026
Merged

Add r8/proguard rules for JNA#358
tmo1 merged 1 commit into
tmo1:masterfrom
chenxiaolong:proguard

Conversation

@chenxiaolong

Copy link
Copy Markdown
Contributor

JNA's Android .aar artifact doesn't include any r8/proguard rules. According to their FAQ [1], application developers are expected to copy and paste the 4 rules into their proguard configs. Without these rules, SodiumAndroid() initialization would fail because JNA's initialization can't find the fields it needed via reflection with r8 renaming fields to single letter names.

java.lang.UnsatisfiedLinkError: Can't obtain peer field ID for class com.sun.jna.Pointer

This only affected release builds since debug builds don't have r8 enabled.

[1] https://github.com/java-native-access/jna/blob/master/www/FrequentlyAskedQuestions.md#jna-on-android

Fixes: #357

JNA's Android .aar artifact doesn't include any r8/proguard rules.
According to their FAQ [1], application developers are expected to copy
and paste the 4 rules into their proguard configs. Without these rules,
SodiumAndroid() initialization would fail because JNA's initialization
can't find the fields it needed via reflection with r8 renaming fields
to single letter names.

    java.lang.UnsatisfiedLinkError: Can't obtain peer field ID for class com.sun.jna.Pointer

This only affected release builds since debug builds don't have r8
enabled.

[1] https://github.com/java-native-access/jna/blob/master/www/FrequentlyAskedQuestions.md#jna-on-android

Fixes: tmo1#357

Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
@chenxiaolong

chenxiaolong commented Jul 30, 2026

Copy link
Copy Markdown
Contributor Author

In my release build, the peer field in com.sun.jna.Pointer got renamed to b by r8, so this:

https://github.com/java-native-access/jna/blob/5.19.1/native/dispatch.c#L2872-L2875

couldn't find the field by name.

app/build/outputs/mapping/standardRelease/mapping.txt:

com.sun.jna.Pointer -> com.sun.jna.Pointer:
# {"id":"sourceFile","fileName":"Pointer.java"}
    long peer -> b

EDIT: JNA should really be providing a proguard config in their .aar artifacts since they already have Android-specific builds. I wonder why they don't. Most Android libraries that require special configs will include that.

@tmo1
tmo1 merged commit 2f5b1e5 into tmo1:master Jul 31, 2026
5 checks passed
@tmo1

tmo1 commented Jul 31, 2026

Copy link
Copy Markdown
Owner

Thanks much! I tested extensively before comitting and releasing this - but only with debug builds :|

@chenxiaolong
chenxiaolong deleted the proguard branch July 31, 2026 01:53
@tmo1

tmo1 commented Jul 31, 2026

Copy link
Copy Markdown
Owner

To be fair to the LazySodium project, it does recommend adding these ProGuard rules in its own documentation, but I had missed it.

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.

App crashing since update

2 participants