没有静态方法isDeviceProtectedStorage运行时错误

时间:2019-03-30 15:57:45

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

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

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

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'
            }
        }
    }
}

但是,这似乎没有什么不同。我在依存关系树中看到,所有支持版本的确是resoutionStrategy中指定的 28.0.0 ,因此该部分似乎正常工作。

我的完整gradle文件是here

我已发布我的gradle依赖项(./gradlew app:dependencieshere

在这一点上,我正在尝试学习如何正确调试它。我完全不知所措。 有人知道如何调试这种类型的错误吗?

我当然有多次次清理和无效的缓存。

更新

我发现问题确实在FirebaseApp的initializeAllApis部分中。具体行是:

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

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

因此,这似乎是引用最新的,但是当执行此行时,我得到了上面的stacktrace。因此,在缩小范围的同时,我仍然不确定为什么会引发此错误或如何解决该错误。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

方法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配置应完全抛弃-并通过提供适当的版本号,然后应用排除项来正确固定。