Koin: Proguard and ViewModel No constructor found for class

Created on 6 Mar 2019  路  3Comments  路  Source: InsertKoinIO/koin

Hi
We usually use the create function to provide an instance of viewModel

viewModel { create<LoginViewModel>() }
when we turn on Proguard the function returns an error
Can't create definition for 'Factory [name='b',class='com.example.myApp.ui.login.imei.b']' due to error : No constructor found for class

Complete error

To Reproduce
Steps to reproduce the behavior:

  1. Create a function to provide viewModel.
    for instance:
    viewModel { create() }
  2. Turn on Proguard
  3. Try to get instance in Fragment or Activity

Koin version
org.koin:koin-androidx-viewmodel:1.0.2

Greetings

android workaround

Most helpful comment

we try to add different rules

-keepnames class android.arch.lifecycle.ViewModel -keepclassmembers public class * extends android.arch.lifecycle.ViewModel { public <init>(...); } -keepclassmembers class com.lebao.app.domain.** { public <init>(...); } -keepclassmembers class * { public <init>(...); }

The last rule working correctly, allows the code to be obfuscated correctly and not deleted the constructors.

-keepclassmembers class * { public <init>(...); }

Thank you

All 3 comments

With that notation you are using reflection to instantiate your class. But since proguard cannot find any compile-time uses of your constructor, it gets remmoved. You need to write your own Proguard keep rules if you wish to use the reflection-based APIs.

we try to add different rules

-keepnames class android.arch.lifecycle.ViewModel -keepclassmembers public class * extends android.arch.lifecycle.ViewModel { public <init>(...); } -keepclassmembers class com.lebao.app.domain.** { public <init>(...); } -keepclassmembers class * { public <init>(...); }

The last rule working correctly, allows the code to be obfuscated correctly and not deleted the constructors.

-keepclassmembers class * { public <init>(...); }

Thank you

You can probably declare it more explicit by

-keepclassmembers public class * extends androidx.lifecycle.ViewModel { public <init>(...); }

Your previous

-keepclassmembers public class * extends android.arch.lifecycle.ViewModel { public <init>(...); }

may not work because of old ViewModel package :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

caleb-allen picture caleb-allen  路  4Comments

erikhuizinga picture erikhuizinga  路  3Comments

congtuyenvip picture congtuyenvip  路  4Comments

haroldadmin picture haroldadmin  路  3Comments

iRYO400 picture iRYO400  路  3Comments