Search code examples
androidkotlinproguardandroid-architecture-components

Kotlin, proguard, Android Architecture Components


I have a view model with a live data variable in there.

My activity observes it in order to update the UI.

In debug, it is all working nicely. When I enable proguard, it does not observe anymore the livedata changes.

I have checked that the live data is properly updated. However, the observer callback is never called.

Any hints on how to configure Proguard and what could be wrong?

I tried keeping classes of my package without success:

-keep class com.example.myapp.** { *; }

Here is the ViewModel:

class SplashViewModelImpl : JapetViewModel(), SplashViewModel {    
    private val isTimeUp = MutableLiveData<Boolean>()

    init {
        isTimeUp.value = false
        Observable.timer(2000L, TimeUnit.MILLISECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe { isTimeUp.postValue(true) }

    }

    override fun isTimeUp(): LiveData<Boolean> = isTimeUp
}

And the activity:

class SplashActivity : MyBaseActivity() {

    lateinit var viewModel: SplashViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)

        viewModel = kodein.with(this).instance()

        // Working in debug
        // viewModel.isTimeUp().observe(this, Observer(this::transitionIfTimeUp))

        // Working in debug too, tried this way of writing it too
        viewModel.isTimeUp().observe(this, Observer<Boolean> { t -> transitionIfTimeUp(t) })

        // Also tried doing it with an object : Observer... without success either
    }

    private fun transitionIfTimeUp(isTimeUp: Boolean?) {
        if (isTimeUp == null || !isTimeUp) return

        startActivity<LoginActivity>()
        finish()
    }
}

Edited: dependencies

testCompile "android.arch.persistence.room:testing:1.0.0-alpha3"
androidTestCompile "android.arch.persistence.room:testing:1.0.0-alpha3"

compile "android.arch.lifecycle:extensions:1.0.0-alpha3"
compile "android.arch.lifecycle:reactivestreams:1.0.0-alpha3"
kapt "android.arch.lifecycle:compiler:1.0.0-alpha3"

compile "android.arch.persistence.room:runtime:1.0.0-alpha3"
compile "android.arch.persistence.room:rxjava2:1.0.0-alpha3"
kapt "android.arch.persistence.room:compiler:1.0.0-alpha3"

Solution

  • You must upgrade to alpha4 to get the appropriate ProGuard configuration.

    alpha3 had a issue in that it did not include the appropriate ProGuard configuration. As per the alpha4 release notes, this has been fixed.