适用于mvvm的Proguard规则设置

时间:2018-10-29 08:28:57

标签: android mvvm kotlin dagger-2 android-proguard

在没有设置Proguard Rule的情况下,代码可以正常工作。但是,当我在发布版本中启用proguard时,该项目已成功构建。应用程序已成功安装,但在启动应用程序时每次都崩溃。

这是错误日志

2018-10-29 13:52:04.299 1942-1942/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.eighsquare.mcoopon, PID: 1942
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.eighsquare.mcoopon/com.eighsquare.mcoopon.ui.splash.SplashActivity}: java.lang.RuntimeException: Cannot create an instance of class com.eighsquare.mcoopon.ui.splash.a
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.RuntimeException: Cannot create an instance of class com.eighsquare.mcoopon.ui.splash.a
        at androidx.lifecycle.v$a.a(ViewModelProvider.java:202)
        at androidx.lifecycle.v.a(ViewModelProvider.java:135)
        at androidx.lifecycle.v.a(ViewModelProvider.java:103)
        at com.eighsquare.mcoopon.base.BaseActivity$b.b(**BaseActivity.kt**:32)
        at com.eighsquare.mcoopon.base.BaseActivity$b.a(BaseActivity.kt:16)
        at kotlin.o.a(LazyJVM.kt:74)
        at com.eighsquare.mcoopon.base.BaseActivity.q(Unknown Source:7)
        at com.eighsquare.mcoopon.base.BaseActivity.onCreate(BaseActivity.kt:50)
        at android.app.Activity.performCreate(Activity.java:7009)
        at android.app.Activity.performCreate(Activity.java:7000)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6494) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
     Caused by: java.lang.NoSuchMethodException: <init> [class android.app.Application]
        at java.lang.Class.getConstructor0(Class.java:2320)
        at java.lang.Class.getConstructor(Class.java:1725)
        at androidx.lifecycle.v$a.a(ViewModelProvider.java:200)
        at androidx.lifecycle.v.a(ViewModelProvider.java:135) 
        at androidx.lifecycle.v.a(ViewModelProvider.java:103) 
        at com.eighsquare.mcoopon.base.BaseActivity$b.b(BaseActivity.kt:32) 
        at com.eighsquare.mcoopon.base.BaseActivity$b.a(BaseActivity.kt:16) 
        at kotlin.o.a(LazyJVM.kt:74) 
        at com.eighsquare.mcoopon.base.BaseActivity.q(Unknown Source:7) 
        at com.eighsquare.mcoopon.base.BaseActivity.onCreate(BaseActivity.kt:50) 
        at android.app.Activity.performCreate(Activity.java:7009) 
        at android.app.Activity.performCreate(Activity.java:7000) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6494) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

当我单击 BaseActivity.kt 时,我将重定向到此代码

   val viewModel by lazy {
    if (viewModelFactory != null)
        ViewModelProviders.of(this, viewModelFactory).get(mViewModelClass)
    else ViewModelProviders.of(this).get(mViewModelClass)

}

此代码放置在BaseActivity中以创建Viewmodel。

这是我的BaseActivity.kt

abstract class BaseActivity<VM : ViewModel, DB : ViewDataBinding>(private val mViewModelClass: Class<VM>) : AppCompatBaseActivity() {

@LayoutRes
abstract fun getLayoutRes(): Int

abstract val viewModelFactory: ViewModelProvider.Factory?


val binding by lazy {
    DataBindingUtil.setContentView(this, getLayoutRes()) as DB
}


val viewModel by lazy {
    if (viewModelFactory != null)
        ViewModelProviders.of(this, viewModelFactory).get(mViewModelClass)
    else ViewModelProviders.of(this).get(mViewModelClass)

}


/**
 * If you want to inject Dependency Injection
 * on your activity, you can override this.
 */
open fun onInject() {}

open fun initView() {}

override fun onCreate(savedInstanceState: Bundle?) {
    onInject()
    LayoutInflaterCompat.setFactory2(layoutInflater, IconicsLayoutInflater2(delegate))

    super.onCreate(savedInstanceState)
    initViewModel(viewModel)

    initView()

}

/**
 *
 *  You need override this method.
 *  And you need to set viewModel to binding: binding.viewModel = viewModel
 *
 */
abstract fun initViewModel(viewModel: VM)


fun isNetworkConnected(): Boolean {
    return NetworkUtils.isNetworkConnected(this)
}


fun hideKeyboard() {
    Utils.hideKeyboard(this)
}

fun changeFragment(fragment: Fragment, cleanStack: Boolean = false, addToBackStack: Boolean = true) {
    Utils.changeFragment(this, fragment, cleanStack, addToBackStack)
}

}

我正在使用Dagger2 我在寻找解决方案上浪费了2个小时。 Stackoverflow是最后的希望。

1 个答案:

答案 0 :(得分:2)

经过长时间的研究,我找到了解决方案。 我只需要在ViewModel类中添加 public @Inject构造函数 例如;

class FullScreenViewModel public @Inject constructor(application: Application) : BaseViewModel<Any?>(application) {}