mvvm 的 Proguard 规则设置

Proguard rule setup for mvvm

代码在没有 Proguard 规则设置的情况下工作正常。但是当我在发布版本中启用 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)

}

这段代码放在创建Viewmodel的BaseActivity中。

这是我的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 个小时。 Whosebug 是最后的希望。

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

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