无法实例化应用程序混淆器

Unable to instantiate application proguard

当我使用 proguard 时,它总是出现这样的错误。我不知道该怎么做我已经尝试了所有教程。请帮助任何可以提供帮助的人。

Process: com.example.belajarproguard, PID: 22062
java.lang.RuntimeException: Unable to instantiate application com.example.belajarproguard.MVVMApplication: java.lang.ClassNotFoundException: Didn't find class "com.example.belajarproguard.MVVMApplication" on path: DexPathList[[zip file "/data/app/com.example.belajarproguard-2/base.apk"],nativeLibraryDirectories=[/data/app/com.example.belajarproguard-2/lib/x86, /system/lib, /vendor/lib]]
    at android.app.LoadedApk.makeApplication(LoadedApk.java:802)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5335)
    at android.app.ActivityThread.-wrap2(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
 Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.belajarproguard.MVVMApplication" on path: DexPathList[[zip file "/data/app/com.example.belajarproguard-2/base.apk"],nativeLibraryDirectories=[/data/app/com.example.belajarproguard-2/lib/x86, /system/lib, /vendor/lib]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    at android.app.Instrumentation.newApplication(Instrumentation.java:992)
    at android.app.LoadedApk.makeApplication(LoadedApk.java:796)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5335) 
    at android.app.ActivityThread.-wrap2(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1528) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 

MVVM应用程序

class MVVMApplication : Application(), KodeinAware {

    override val kodein = Kodein.lazy {
        import(androidXModule(this@MVVMApplication))

        bind() from singleton { NetworkConnectionInterceptor(instance()) }
        bind() from singleton { BasicAuth() }
        bind() from singleton { MyApi(instance(), instance()) }
        bind() from singleton { MarketingRepo(instance(), instance()) }
        bind() from singleton { AppDatabase(instance()) }
        bind() from singleton { PageFragmentsViewModelFactory(instance()) }
    }

}

在您的 proguard rules 文件中,请输入以下规则:

-keep class com.example.belajarproguard.** { *; }

如果您正在使用 androidX,则可以使用 @Keep 注释。

@Keep
class MVVMApplication : Application(), KodeinAware {

    override val kodein = Kodein.lazy {
        import(androidXModule(this@MVVMApplication))

        bind() from singleton { NetworkConnectionInterceptor(instance()) }
        bind() from singleton { BasicAuth() }
        bind() from singleton { MyApi(instance(), instance()) }
        bind() from singleton { MarketingRepo(instance(), instance()) }
        bind() from singleton { AppDatabase(instance()) }
        bind() from singleton { PageFragmentsViewModelFactory(instance()) }
    }

}

这个问题已经解决,问题出在multidex

java.lang.RuntimeException: Unable to instantiate application com.example.belajarproguard.MVVMApplication: java.lang.ClassNotFoundException: Didn't find class "com.example.belajarproguard.MVVMApplication" on path: DexPathList[[zip file "/data/app/com.example.belajarproguard-2/base.apk"],nativeLibraryDirectories=[/data/app/com.example.belajarproguard-2/lib/x86, /system/lib, /vendor/lib]]

你只需要改变

class MVVMApplication : Application(), KodeinAware {

class MVVMApplication : MultiDexApplication(), KodeinAware {

将此添加到 class

override fun attachBaseContext(base: Context) {
        super.attachBaseContext(base)
        MultiDex.install(this)
    }

并将其添加到依赖项

implementation 'androidx.multidex:multidex:2.0.1'

来源

Enable Multidex

别忘了 之前清理项目