Ad

Proguard Rule Setup For Mvvm

The code is working fine without Proguard Rule setup. But when I enabled proguard in release build, The project is build successfully. App is installed successfully but while launching the app it crashed every time .

Here is the error log

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) 

when I click on the BaseActivity.kt I am redirected to this code

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

}

This code is placed in the BaseActivity to create Viewmodel.

Here is My 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)
}

}

I am using Dagger2 I lost my 2 hrs in searching the solution. Stackoverflow is the last hope.

Ad

Answer

After a long research I got a solution. I Simply needed to add public @Inject constructor in my ViewModel class eg;

class FullScreenViewModel public @Inject constructor(application: Application) : BaseViewModel<Any?>(application) {}
Ad
source: stackoverflow.com
Ad