【问题标题】:Unable to instantiate application proguard无法实例化应用程序保护
【发布时间】:2020-07-07 10:12:42
【问题描述】:

当我使用 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 规则文件 MVVMApplication 类
  • 我还没有输入任何规则
  • 我输入了规则-keep class com.example.belajarproguard,没有任何变化

标签: android android-studio proguard


【解决方案1】:

在您的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()) }
    }

}

【讨论】:

    【解决方案2】:

    这个问题已经解决了,问题在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 {
    

    将此添加到类中

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

    并将其添加到依赖项

    implementation 'androidx.multidex:multidex:2.0.1'
    

    来源

    Enable Multidex

    别忘了 之前清理项目

    【讨论】:

      猜你喜欢
      • 2020-02-24
      • 2011-08-17
      • 2013-07-31
      • 2012-04-26
      • 1970-01-01
      • 1970-01-01
      • 2014-03-30
      相关资源
      最近更新 更多