在发行版APK中未在路径:DexPathList错误上找到类“ com.jambod.user.model.ProcessModel”

时间:2019-10-16 07:23:08

标签: android kotlin release android-multidex

我编写了一个应用程序,并在其上使用了dagger-android和导航组件。为减小apk的大小,我启用了minifyEnabledshrinkResources。但是,当我生成apk并打开应用程序时,该应用程序崩溃了。我跟踪了logcat,错误为Caused by java.lang.ClassNotFoundException Didn't find class "com.jambod.user.model.ProcessModel" on path: DexPathList。这是ProcessModel的代码:

    data class ProcessModel(
    var id: String,
    var state: String ,
    var originLat: Double,
    var originLon: Double,
    var originAddress: String,
    var city: String ,
    var client: User ,
    var broker: Broker,
    var handover: HandOver ,
    var startTime: Long ,
    var endTime: Long ,
    var clientScore: Int,
    var role:String,
    var isFirstFinishedProcess:Boolean

) : Serializable {
}

这是我的Application类:

    class MyApplication : DaggerApplication() {

    val TAG="MyApplication"
    @Inject
    lateinit var mapboxMap: CedarMaps

    override fun applicationInjector(): AndroidInjector<out DaggerApplication> {

        Log.e(TAG,"applicationInjector")
        return DaggerAppComponent.builder().application(this).build()

    }

    override fun onCreate() {
        super.onCreate()
        Log.e(TAG,"onCreate")
        setMapview()
    }

    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(base)
        Log.e(TAG,"attachBaseContext")
        MultiDex.install(this)
    }

    private fun setMapview() {
        mapboxMap.setClientID("jambod-3804728105994932624")
                .setClientSecret("b37qDGphbWJvZLqxi8hCuQhrjCXtqKs9V536SyiAAHQP01FzTg3G0iat")
                .setContext(this)
    }

}

在logcat中的代码移动导航文件的这一部分有错误:

  <fragment android:id="@+id/detailProcessFragment" android:name="com.jambod.user.ui.DetailProcessFragment"
          android:label="fragment_detail_process" tools:layout="@layout/fragment_detail_process">
    <argument android:name="KEY_PROCESS"
        app:argType="com.jambod.user.model.ProcessModel"
        app:nullable="true"
        android:defaultValue="@null"/>
</fragment>

根据document,我创建了一个multidex-config.txt文件并将其添加到build.gradle上,但问题没有解决。这是build.gradle文件:

 defaultConfig {
    applicationId "com.jambod.user"
    minSdkVersion 16
    targetSdkVersion 29
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    multiDexEnabled true
}
buildTypes {
    release {
        multiDexKeepFile file('multidex-config.txt')
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}

1 个答案:

答案 0 :(得分:0)

使用"minifyEnabled true"和proguard时。 “ minifyEnabled true” 将在一段时间后删除您在应用中使用的模型类。在 Gradle脚本-> proguard-rules.pro 文件中添加以下行。下面的一行将使班级及其成员保持最终的APK。

-keepclasseswithmembernames  class  com.jambod.user.model.ProcessModel.** { *; }