简体   繁体   English

Kotlin,proguard,Android体系结构组件

[英]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. 我的活动会观察它以便更新UI。

In debug, it is all working nicely. 在调试中,一切正常。 When I enable proguard, it does not observe anymore the livedata changes. 当我启用proguard时,它将不再观察实时数据更改。

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? 关于如何配置Proguard的任何提示以及可能出了什么问题?

I tried keeping classes of my package without success: 我试图保持我的包裹的类没有成功:

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

Here is the ViewModel: 这是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"

You must upgrade to alpha4 to get the appropriate ProGuard configuration. 您必须升级到alpha4才能获得适当的ProGuard配置。

alpha3 had a issue in that it did not include the appropriate ProGuard configuration. alpha3一个问题是它不包含适当的ProGuard配置。 As per the alpha4 release notes , this has been fixed. 根据alpha4发行说明 ,此问题已修复。

I guess you are looking for the operator Observable.interval and not Observable.timer 我猜您正在寻找运算符Observable.interval而不是Observable.timer

The one you are using emits ONLY ONCE after the specified time. 在指定的时间后,您使用的那个发射一次。 However Observable.interval emits continuously every the specified interval. 但是, Observable.interval每个指定的时间间隔连续发射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM