【问题标题】:Android studio doesnt recognise EncryptedSharedPreferences importAndroid Studio 无法识别 EncryptedSharedPreferences 导入
【发布时间】:2019-07-15 01:36:13
【问题描述】:

所以我正在尝试创建加密共享首选项,如 dev android 端所示:https://developer.android.com/reference/androidx/security/crypto/EncryptedSharedPreferences#inherited-methods 而 android studio 似乎并没有全部认出来。 我的最小 skd 是 23 是我目前所理解的 androidx 不需要最新的 android 版本的原因吗?

尝试干净重建构建等。 尝试从 Android Studio 菜单迁移到 androidx(收到项目中未找到任何用法的消息,但我确实有一些 androidx 导入,因为我在检查后了解到)

import androidx.security.crypto.EncryptedSharedPreferences;


 String masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC);
        SharedPreferences sharedPreferences = EncryptedSharedPreferences.create(
                "secret_shared_prefs",
                masterKeyAlias,
                context,
                EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
                EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
        );

//doesnt recognise MasterKeys nor EncryptedSharedPreferences classes

【问题讨论】:

    标签: java android sharedpreferences androidx


    【解决方案1】:

    根据androidx.security Declaring dependencies documentation,需要添加对库的依赖:

    dependencies {
        def security_version = "1.0.0-alpha02"
        implementation "androidx.security:security-crypto:$security_version"
    }
    

    【讨论】:

    • minSdkVersion 不能小于版本 23
    • @Praveen - 这确实是安全的 minSdkVersion。随意star the existing issue 以降低 minSdkVersion
    • @ianhanniballake 我知道该库仍处于 alpha 版本。在生产中使用它有多安全?我想使用EncryptedSharedPreferences 来存储令牌。
    • @sam33r - 关于 AndroidX 库的“alpha”,alpha 是关于 API 稳定性(当您选择升级时它可能会在未来的 alpha 中发生变化)并且不是关于运行时稳定性.作为参考,Google 应用使用所有 AndroidX 库的 alpha 版本。
    • @Blackbelt - 是的,这正是 API 稳定性的含义
    【解决方案2】:

    将此添加到 build.gradle 文件中

    api 'androidx.security:security-crypto:1.0.0'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-09
      • 2015-06-21
      • 2016-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多