【问题标题】:No static method isDeviceProtectedStorage runtime error没有静态方法 isDeviceProtectedStorage 运行时错误
【发布时间】:2019-08-21 07:50:20
【问题描述】:

对于同一问题,这里有很多问题,但没有一个解决根本原因或我所看到的 - 因此建议的答案没有帮助。

我的应用程序构建没有问题,但是,在运行时,我不断收到运行时异常:

java.lang.NoSuchMethodError: No static method isDeviceProtectedStorage(Landroid/content/Context;)Z in class Landroid/support/v4/content/ContextCompat; or its super classes (declaration of 'android.support.v4.content.ContextCompat' appears in /data/app/com.ohmd-1/base.apk:classes116.dex)
                      at com.google.firebase.FirebaseApp.zzc(Unknown Source)
                      at com.google.firebase.FirebaseApp.initializeApp(Unknown Source)
                      at com.google.firebase.FirebaseApp.initializeApp(Unknown Source)
                      at com.google.firebase.FirebaseApp.initializeApp(Unknown Source)
                      at com.google.firebase.provider.FirebaseInitProvider.onCreate(Unknown Source)
                      at android.content.ContentProvider.attachInfo(ContentProvider.java:1748)
                      at android.content.ContentProvider.attachInfo(ContentProvider.java:1723)
                      at com.google.firebase.provider.FirebaseInitProvider.attachInfo(Unknown Source)

我从其他问题中看到,这可能与具有不同版本的依赖项中的支持库有关,因此我通过将以下内容添加到我的 gradle 来强制执行单个库版本:

configurations.all {
    resolutionStrategy {
        force 'com.google.firebase:firebase-core:15.0.2'
        force 'com.google.firebase:firebase-messaging:15.0.2'
    }
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group.equals('com.android.support')) {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion '28.0.0'
            }
        }
    }
}

但是,这似乎没有什么不同。我在依赖树中看到所有支持版本确实是 28.0.0,如 resoutionStrategy 中指定的那样,所以看起来该部分正在工作。

我的完整 gradle 文件是 here

我已经发布了我的 gradle 依赖项 (./gradlew app:dependencies) here

此时,我正在尝试学习如何正确调试它。我完全不知所措。 有人知道如何调试此类错误吗?

当然,我已经多次清理过缓存并使其失效。

更新

我发现问题确实出在 FirebaseApp 的 initializeAllApis 部分。具体线路是:

boolean isDeviceProtectedStorage = ContextCompat.isDeviceProtectedStorage(this.applicationContext);

ContextCompat 表示来自com.android.support:support-compat:28.0.0

所以这似乎是引用最新的,但是当这行执行时,我得到了上面的堆栈跟踪。因此,虽然我正在缩小范围,但我仍然不确定为什么会抛出此错误或如何修复它。

【问题讨论】:

  • 转到构建 -> 分析 APK 并检查 classes116 。看看你是否通过那里引用了正确的库。可能是您使用的设备具有 sdk 版本 if 23 并使用默认支持库。

标签: android firebase gradle android-gradle-plugin firebase-cloud-messaging


【解决方案1】:

该问题可能与 firebase-core 有关。 Firebase-core 依赖于 com.android.support:support-v4:24.0.0,最低使用 android API 为 24。确保您的 firebase 核心 lib 版本与您正在使用的 API 版本匹配。看看这里:Firebase library dependencies

【讨论】:

  • 根据OP给出的依赖树,所有支持的版本都是28.0.0,即使是firebase-core提供的那个
【解决方案2】:

方法 ContextCompat#isDeviceProtectedStorage() 自版本 24.1.0 开始存在...而 build.gradle 不是 MCVE,因此我很难在我的 IDE 中重现该问题 - 所以我只能应用常识,而不是像我通常习惯的那样使用反复试验。如果您使用重现问题的最少代码创建MCVE,我可以扩展我的答案(创建这样的结果通常会产生一些效果);而且好像还有一个build.gradle

应避免混淆配置compileimplementation。这些不仅仅是相同的两个名称,而是被视为两种配置。这似乎是意外行为的原因。弯曲版本号并不能使解决此类问题更容易。

有一个compile 依赖项(即使它可能在28.0.0 版本中提供):

compile 'com.android.support:support-v4:26.1.0'

还有这个implementation 依赖:

implementation 'com.google.firebase:firebase-core:15.0.2'

这些是current versions:

implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.google.firebase:firebase-core:16.0.8'
implementation 'com.google.firebase:firebase-messaging:17.5.0'

将所有compile 依赖项重构为implementation 应该会导致两个依赖项都在一个相同的配置中,然后ContextCompat 应该知道FirebaseApp,因为它应该是。刚刚看到,列出的implementation 依赖项有两个unspecified 包,它们看起来并不完全“正常”——而且大多数版本号通常都显得过时了。这些resolutionStrategy 配置应该完全放弃 - 并通过提供合适的版本号然后应用排除来正确修复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-02
    • 2017-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多