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

时间:2016-01-13 11:14:39

标签: android proguard android-support-library

我正在使用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测试过,同样的事情发生了。 任何建议都是有用的,提前谢谢。

3 个答案:

答案 0 :(得分:16)

以下对我有用:

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

defaultConfig {
...
multiDexEnabled true
...

}

然后:

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

}

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

一个。如果您扩展您的应用程序类:

<?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 { ... }

℃。如果您执行扩展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

答案 1 :(得分:1)

我正在使用 androidx 库,并遇到相同的错误。因此,在 AndroidManifest.xml 中,我更改了这一行:

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

对此:

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

答案 2 :(得分:0)

经过几个小时的谷歌搜索后,我最终更新了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'

我想谷歌服务库需要有一些东西