【问题标题】:Android FileProvider class not found in release builds在发布版本中找不到 Android FileProvider 类
【发布时间】:2016-01-13 11:14:38
【问题描述】:

我正在使用 FileProvider 从设备获取照片。该实现在调试版本中运行良好(minifyEnabled false)但是当我构建发布版本时(minifyEnabled true)我收到一个错误:

java.lang.RuntimeException: Unable to get provider android.support.v4.content.FileProvider: 
java.lang.ClassNotFoundException: Didn't find class "android.support.v4.content.FileProvider" 
on path: DexPathList[[zip file "/data/app/com.package.name-2/base.apk"],
nativeLibraryDirectories=[/data/app/om.package.name-2/lib/arm, /vendor/lib, /system/lib]]

所以我猜这与 proguard 设置有关

我有

compile 'com.android.support:support-v13:23.1.1'

这是我的 gradle 文件中 v4 的超集,并且

minSdkVersion 21
targetSdkVersion 23

-keep class android.support.v4.app.** { *; }
-keep class android.support.v4.content.** { *; }
-keep interface android.support.v4.app.** { *; }
-keep interface android.support.v4.content.** { *; }
-keep class * extends android.content.ContentProvider

在我的 proguard-rules.pro 文件中

我已经在 Android 5 和 6 上进行了测试,并且发生了同样的事情。 任何建议都会很有用,在此先感谢。

【问题讨论】:

标签: android proguard android-support-library


【解决方案1】:

以下内容对我有用:

在你的模块 build.gradle 文件中:

defaultConfig {
...
multiDexEnabled true
...

}

然后:

dependencies {
...
compile 'com.android.support:multidex:1.0.2'
...

}

最后,确保您的应用程序类具有以下条件之一:

A.如果您扩展您的应用程序类:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp">
    <application
            android:name="android.support.multidex.MultiDexApplication" >
        ...
    </application>
</manifest>

B.如果您确实扩展了您的 Application 类并且可以更改基类:

public class MyApplication extends MultiDexApplication { ... }

C.如果您确实扩展了您的 Application 类并且无法更改基类:

  public class MyApplication extends SomeOtherApplication {
  @Override
  protected void attachBaseContext(Context base) {
     super.attachBaseContext(base);
     MultiDex.install(this);
  }
}

更多信息:

https://developer.android.com/studio/build/multidex.html#mdex-gradle

【讨论】:

  • 你好@Munier Parker,我已经完成了所有这些,但仍然遇到同样的问题。
【解决方案2】:

我正在使用 androidx 库并得到同样的错误。所以,在 AndroidManifest.xml 中,我更改了这一行:

android:name="android.support.v4.content.FileProvider"

到这里:

android:name="androidx.core.content.FileProvider"

【讨论】:

    【解决方案3】:

    经过几个小时的谷歌搜索,我最终将 gradle 和 google 服务更新为

    dependencies {
        classpath 'com.android.tools.build:gradle:1.5.0'
        classpath 'com.google.gms:google-services:1.5.0'
    }
    

    以前的版本件

        classpath 'com.android.tools.build:gradle:1.3.0'
        classpath 'com.google.gms:google-services:1.5.0-beta2'
    

    我想 google-services 库需要一些东西

    【讨论】:

    • com.google.gms:google-servicesandroid.support.v4.content.FileProvider 有什么关系?
    猜你喜欢
    • 2017-03-02
    • 2017-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-09
    • 2022-10-21
    • 1970-01-01
    相关资源
    最近更新 更多