Android 在发布版本中找不到 FileProvider class

Android FileProvider class not found in release builds

我正在使用 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 进行了测试,同样的事情发生了。 任何建议都会有用,在此先感谢。

经过几个小时的谷歌搜索,我最终将 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 库需要一些东西

以下对我有用:

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

defaultConfig {
...
multiDexEnabled true
...

}

然后:

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

}

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

一个。如果您延长您的申请class:

<?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>

乙。如果您 扩展您的应用程序 class 但可以更改基础 class:

public class MyApplication extends MultiDexApplication { ... }

摄氏度。如果您 扩展您的应用程序class 并且无法更改基础class:

  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

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

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

对此:

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