You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MAUI Android端调用SecureStorage.SetAsync抛出FileNotFoundException问题

问题解决:SecureStorage.SetAsync 触发 FileNotFoundException

问题场景

调用自定义的SetAsync方法时抛出FileNotFoundException,查阅官方文档仅要求清单文件添加两项配置,但问题仍未解决。

代码实现

public async Task SetAsync(string key, string value)
{
  await SecureStorage.SetAsync(key, value);
}

AndroidManifest.xml 配置

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="31" />
    <application android:allowBackup="false" android:icon="@mipmap/appicon" android:exported="false" android:fullBackupContent="false" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true">
    </application>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>

异常信息(中文翻译)

[monodroid-assembly] 从bundle加载程序集失败:Xamarin.AndroidX.Security.SecurityCrypto.dll
已加载程序集:/data/data/com.abc.mypp.test/files/.__override__/Xamarin.AndroidX.Security.SecurityCrypto.dll [外部]
[AndroidKeysetManager] 未找到密钥集,将生成新密钥集
[AndroidKeysetManager] java.io.FileNotFoundException: 无法读取密钥集;偏好设置值__androidx_security_crypto_encrypted_prefs_key_keyset__不存在
[AndroidKeysetManager]  at com.google.crypto.tink.integration.android.SharedPrefKeysetReader.readPref(SharedPrefKeysetReader.java:71)
[AndroidKeysetManager]  at com.google.crypto.tink.integration.android.SharedPrefKeysetReader.readEncrypted(SharedPrefKeysetReader.java:89)
[AndroidKeysetManager]  at com.google.crypto.tink.KeysetHandle.read(KeysetHandle.java:116)
[AndroidKeysetManager]  at com.google.crypto.tink.integration.android.AndroidKeysetManager$Builder.read(AndroidKeysetManager.java:311)
[AndroidKeysetManager]  at com.google.crypto.tink.integration.android.AndroidKeysetManager$Builder.readOrGenerateNewKeyset(AndroidKeysetManager.java:287)
[AndroidKeysetManager]  at com.google.crypto.tink.integration.android.AndroidKeysetManager$Builder.build(AndroidKeysetManager.java:238)
[AndroidKeysetManager]  at androidx.security.crypto.EncryptedSharedPreferences.create(EncryptedSharedPreferences.java:155)
[AndroidKeysetManager]  at androidx.security.crypto.EncryptedSharedPreferences.create(EncryptedSharedPreferences.java:120)
[AndroidKeysetManager] 未找到密钥集,将生成新密钥集
[AndroidKeysetManager] java.io.FileNotFoundException: 无法读取密钥集;偏好设置值__androidx_security_crypto_encrypted_prefs_value_keyset__不存在
[AndroidKeysetManager]  at com.google.crypto.tink.integration.android.SharedPrefKeysetReader.readPref(SharedPrefKeysetReader.java:71)
[AndroidKeysetManager]  at com.google.crypto.tink.integration.android.SharedPrefKeysetReader.readEncrypted(SharedPrefKeysetReader.java:89)
[AndroidKeysetManager]  at com.google.crypto.tink.KeysetHandle.read(KeysetHandle.java:116)
[AndroidKeysetManager]  at com.google.crypto.tink.integration.android.AndroidKeysetManager$Builder.read(AndroidKeysetManager.java:311)
[AndroidKeysetManager]  at com.google.crypto.tink.integration.android.AndroidKeysetManager$Builder.readOrGenerateNewKeyset(AndroidKeysetManager.java:287)
[AndroidKeysetManager]  at com.google.crypto.tink.integration.android.AndroidKeysetManager$Builder.build(AndroidKeysetManager.java:238)
[AndroidKeysetManager]  at androidx.security.crypto.EncryptedSharedPreferences.create(EncryptedSharedPreferences.java:160)
[AndroidKeysetManager]  at androidx.security.crypto.EncryptedSharedPreferences.create(EncryptedSharedPreferences.java:120)
[EngineFactory] 无法获取GmsCore_OpenSSL提供程序

解决步骤

  • 检查并更新NuGet包:确保Android项目已安装Xamarin.AndroidX.Security.Crypto包,且版本与其他Xamarin.AndroidX包兼容,建议更新到最新稳定版。
  • 清理重建项目:删除Android项目的bin和obj文件夹,清理解决方案后重新生成,解决缓存导致的程序集加载问题。
  • 移除不必要的权限:SecureStorage使用应用内部存储,无需READ_EXTERNAL_STORAGE和WRITE_EXTERNAL_STORAGE权限,移除这两项权限避免潜在冲突。
  • 安装Google Play Services相关包:针对GmsCore_OpenSSL not available提示,安装Xamarin.GooglePlayServices.Base包,确保加密库依赖正常。
  • 验证备份配置:确认android:allowBackup="false"和android:fullBackupContent="false"已正确设置,避免备份机制干扰密钥集生成。

内容的提问来源于stack exchange,提问作者boss

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 12:45:28