【问题标题】:Unresolved reference: MasterKey未解决的参考:MasterKey
【发布时间】:2023-03-25 07:25:01
【问题描述】:

我正在尝试遵循此处给出的示例: https://developer.android.com/topic/security/data

我已经在我的 gradle 中包含了所需的库:

implementation "androidx.security:security-crypto:1.0.0"

// For Identity Credential APIs
implementation "androidx.security:security-identity-credential:1.0.0-alpha02"

然而当我尝试使用代码时:

val mainKey = MasterKey.Builder(applicationContext)
    .setKeyScheme(MasterKey.KeyScheme.AES256_GCM)
    .build()

我得到未解决的参考:MasterKey 错误。图书馆有那个类吗?

提前致谢。

【问题讨论】:

    标签: android encryption androidx


    【解决方案1】:

    将库升级到最新版本(撰写本文时为 1.1.0-alpha03)

    implementation "androidx.security:security-crypto:1.1.0-alpha03"
    

    从release note 开始,MasterKey 被添加到版本 1.1.0-alpha01。

    Version 1.1.0-alpha01
    ...
    New MasterKey class provides more options for keys, also deprecating MasterKeys to support new features and versions of Android that do not have KeyGenParamSpec.
    

    【讨论】:

      【解决方案2】:

      趁着杰恒的反应……

      MasterKey API 是从1.1.0-alpha01 版本的security-crypto 中添加到androidx.security 包中的(您可以访问此库的版本here)。

      如果你申请solution informed by Jie Heng,你就会成功。

      但如果您正在开发的 Android 项目支持 Android API 23(又名 Android Marshmallow 或仅支持 Android 6)……

      应该毫无问题地接受的东西,正如official documentation 中所述。说明如何使用 EncryptedSharedPreferences 和 EncryptedFile API 的文档...

      如果您将minSdkVersion 设置为23,那么您会注意到该项目甚至无法编译。

      这就是我的情况!

      我从事的项目支持 Android API 级别 23。

      所以我applied the following update 到AndroidManifest.xml:

      <?xml version="1.0" encoding="utf-8"?>
      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          package="...">
      
          <uses-sdk tools:overrideLibrary="androidx.security.identity.credential" />
      ...
      

      项目运行顺利,包括在 Android API 23(在 AVD API 23 上)。

      注意:另一种选择是使用MasterKeys API 而不是MasterKey。但请注意,MasterKeys 已弃用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-04-05
        • 1970-01-01
        • 2021-08-17
        • 2016-12-14
        • 2020-05-27
        • 2019-11-16
        • 2021-01-24
        • 2021-08-13
        相关资源
        最近更新 更多