Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 701
leeCoder Avatar asked Jun 01 '20 15:06

leeCoder


People also ask

How to use getsharedpreferences method in Android?

Using this method you can SET or GET preference literally from anywhere Let’s Begin with code. Step 1) Create a new app with MainActivity. Step 2) In MainActivity add below line of code to above OnCreate method. Step 3) Get instance of getSharedPreferences method providing name and MODE.

What is unsatisfiedlinkerror in Java?

The UnsatisfiedLinkError error is a subclass of the java.lang.LinkageError class which means this error is captured at program startup, during the JVM’s class loading and linking process.

Is there a performance difference between sharedpreferences and encryptedsharedpreferences?

Care: there is a significant performance difference between SharedPreferences and EncryptedSharedPreferences. You should notice that EncryptedSharedPreferences.create (...) is not so fast so you should have to store one instance at all.


5 Answers

I solved using

dependencies {
..
   implementation "androidx.security:security-crypto:1.0.0-rc03"
like image 136
Antonio Romano Avatar answered Oct 19 '22 07:10

Antonio Romano


-keep class com.google.crypto.** { *; }
like image 31
Artemiy Avatar answered Oct 19 '22 07:10

Artemiy


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

like image 28
Alex F. Avatar answered Sep 20 '22 06:09

Alex F.


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

like image 7
Raz Avatar answered Oct 19 '22 07:10

Raz


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

like image 2
Zhar Avatar answered Oct 19 '22 09:10

Zhar