Secured Android SharedPreferences Error: 'Caused by: java.lang.RuntimeException: Field keySize_ for...'
Asked Answered
M

6

9

In an Android Kotlin project, I implemented EncryptedSharedPreference feature based on this link using the androidx.security library and it worked well in debug mode. But in release mode, I keep getting this error

  java.lang.ExceptionInInitializerError
    at com.package_name.i.a.f(:46)
    at com.package_name.i.a.j(:52)
    at com.package_name.i.a.e(:82)
    at com.package_name.MyApplication.onCreate(:37)
    at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1013)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4707)
    at android.app.ActivityThread.-wrap1(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 Caused by: java.lang.RuntimeException: Field keySize_ for k.a.d.a.h0.u not found. Known fields are [private int k.a.d.a.h0.u.i, private static final k.a.d.a.h0.u k.a.d.a.h0.u.j, private static volatile k.a.d.a.i0.a.a1 k.a.d.a.h0.u.k]
    at k.a.d.a.i0.a.v0.n0(:608)

Please kindly share your ideas on how to solve this error.

Marquess answered 1/6, 2020 at 15:41 Comment(1)
please add your code too :). you may notice this too "This library is currently available as an alpha library"Scorcher
F
7

This bug is related to Minify Enabled, which most likely causes some values to be removed.

A bug is reported already, you can track it here:

https://issuetracker.google.com/issues/157983099

Felisafelise answered 4/6, 2020 at 7:36 Comment(1)
Quick fix: add this to proguard rules -keep class com.google.crypto.** { *; }Josephjosepha
I
6
-keep class com.google.crypto.** { *; }
Ichthyornis answered 21/7, 2020 at 11:36 Comment(1)
Is that a security risk though?Untuck
S
2

Something is wrong with the rc2 version of android crypto of may 20. So it is better to use 1.0.0-alpha02 in your gradle file. Remember, takes care with dependencies which are not in rc. You can also fix the issue with a proguard rules (see comment of Sebas LG).

dependencies {
    implementation "androidx.security:security-crypto:1.0.0-alpha02"
}

Official documentation : https://developer.android.com/jetpack/androidx/releases/security

Linked commits : https://android.googlesource.com/platform/frameworks/support/+log/f66cdf1658639bd74ae850dfe3c1f5bb72eaebe6..6be101c2241593fee3ef9ab4e1fb337b485f2f9a/security/crypto

Scorcher answered 3/6, 2020 at 20:17 Comment(1)
This issue is still present in 1.1.0-alpha01. Using the temporary proguard rules still works.Foxed
B
2

I solved using

dependencies {
..
   implementation "androidx.security:security-crypto:1.0.0-rc03"
Bautram answered 28/8, 2020 at 9:44 Comment(0)
J
1

This is due to the fact that AndroidX security library uses the Google Tink library(This is already fixed in Tink 1.4.0), where there is a flaw in working with Proguard.

Add this code to proguard-rules.pro and this should help:

-keepclassmembers class * extends com.google.crypto.tink.shaded.protobuf.GeneratedMessageLite {
  <fields>;
}

Links:

https://github.com/google/tink/issues/361 https://github.com/google/tink/blob/master/java_src/src/main/resources/META-INF/proguard/protobuf.pro

Jacquline answered 1/8, 2020 at 7:54 Comment(0)
D
0

This issue can be solved if you used these versions specifically

implementation("com.google.crypto.tink:tink-android:1.4.0")
implementation("androidx.security:security-crypto:1.1.0-alpha01")
Decipher answered 22/2, 2023 at 9:10 Comment(1)

© 2022 - 2024 — McMap. All rights reserved.