我的应用程序仅从 Android 10 用户那里收到单一崩溃报告。这是堆栈跟踪:
Fatal Exception: java.lang.NullPointerException: Attempt to read from field 'int android.security.keymaster.ExportResult.resultCode' on a null object reference
at android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStorePublicKeyFromKeystore(AndroidKeyStoreProvider.java:256)
at android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStoreKeyPairFromKeystore(AndroidKeyStoreProvider.java:296)
at android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStorePrivateKeyFromKeystore(AndroidKeyStoreProvider.java:316)
at android.security.keystore.AndroidKeyStoreProvider.loadAndroidKeyStoreKeyFromKeystore(AndroidKeyStoreProvider.java:378)
at android.security.keystore.AndroidKeyStoreSpi.engineGetKey(AndroidKeyStoreSpi.java:105)
at java.security.KeyStore.getKey(KeyStore.java:1062)
at we.lmk.iqQ(we.lmk:19)
at we.Vmk.apply(we.Vmk:51)
at io.reactivex.internal.operators.single.SingleMap$MapSingleObserver.onSuccess(SingleMap.java:57)
at io.reactivex.internal.operators.single.SingleFromCallable.subscribeActual(SingleFromCallable.java:56)
at io.reactivex.Single.subscribe(Single.java:3603)
at io.reactivex.internal.operators.single.SingleMap.subscribeActual(SingleMap.java:34)
at io.reactivex.Single.subscribe(Single.java:3603)
at io.reactivex.internal.operators.single.SingleZipArray.subscribeActual(SingleZipArray.java:63)
at io.reactivex.Single.subscribe(Single.java:3603)
at io.reactivex.internal.operators.maybe.MaybeFilterSingle.subscribeActual(MaybeFilterSingle.java:40)
at io.reactivex.Maybe.subscribe(Maybe.java:4290)
at io.reactivex.internal.operators.maybe.MaybeMap.subscribeActual(MaybeMap.java:40)
at io.reactivex.Maybe.subscribe(Maybe.java:4290)
at io.reactivex.internal.operators.maybe.MaybeMap.subscribeActual(MaybeMap.java:40)
at io.reactivex.Maybe.subscribe(Maybe.java:4290)
at io.reactivex.internal.operators.maybe.MaybeFlatMapSingle.subscribeActual(MaybeFlatMapSingle.java:47)
at io.reactivex.Single.subscribe(Single.java:3603)
at io.reactivex.internal.operators.single.SingleFlatMap.subscribeActual(SingleFlatMap.java:36)
at io.reactivex.Single.subscribe(Single.java:3603)
at io.reactivex.internal.operators.single.SingleFlatMap.subscribeActual(SingleFlatMap.java:36)
at io.reactivex.Single.subscribe(Single.java:3603)
at io.reactivex.internal.operators.single.SingleSubscribeOn$SubscribeOnObserver.run(SingleSubscribeOn.java:89)
at io.reactivex.Scheduler$DisposeTask.run(Scheduler.java:578)
at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66)
at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:919)
这是我的代码:
@NonNull
private static KeyStore getKeyStoreInstance() throws SecureStorageException {
try {
// Get the AndroidKeyStore instance
KeyStore keyStore = KeyStore.getInstance(KEY_KEYSTORE_NAME);
// Relict of the JCA API - you have to call load even
// if you do not have an input stream you want to load or it'll crash
keyStore.load(null);
return keyStore;
} catch (Exception e) {
throw new SecureStorageException(e.getMessage(), e, KEYSTORE_EXCEPTION);
}
}
此代码由该库提供:https://github.com/adorsys/secure-storage-android
我还没有找到任何解决方案,这可能是Android 10内部的错误吗?
我遇到了同样的错误,并在睡眠超时后使用 try/catch 和递归调用方法用周围的代码解决了它:
private int getDataAttemptNumber = 0;
public byte[] getData(String key) {
KeyStore ks = null;
try {
ks = KeyStore.getInstance(KEYSTORE_PROVIDER_ANDROID_KEYSTORE);
ks.load(null);
PrivateKey privateKey = (PrivateKey) ks.getKey(alias, null);
return decrypt(privateKey, preferences.getString(key, null));
} catch (NullPointerException e) {
getDataAttemptNumber++;
if (getDataAttemptNumber > 10) {
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
// nothing to do
}
getDataAttemptNumber = 0;
return null;
}
return getData(key);
} catch (Exception e) {
try {
if (ks != null)
ks.deleteEntry(alias);
} catch (Exception e1) {
// Just ignore any errors here
}
}
return null;
}