Koin: Support AndroidX ViewModel

Created on 8 May 2018  路  19Comments  路  Source: InsertKoinIO/koin

Is your feature request related to a problem? Please describe.
Google is changing the packaging to AndroidX, so, when we use Koin viewModel to do the injection it crashes with:
Type parameter bound for T in inline fun <reified T : ViewModel> Context.viewModel(name: String = ..., noinline definition: Definition<T> /* = (ParameterProvider) -> T */): Unit is not satisfied: inferred type MyViewModel is not a subtype of ViewModel e: ...

Describe the solution you'd like
Allow to use androidx.lifecycle.ViewModel

Describe alternatives you've considered
hum... I don't think there are not a lot of alternatives

Target Koin project
android

Most helpful comment

koin-androidx is available in jcenter 馃憤

implementation "org.koin:koin-androidx:1.0.0-alpha-11"

All 19 comments

Hello,

AndroidX is in alpha-1. Let's keep it under the hand and upgrade it when it will be in a more stable state (else we will have to regularly provide updates).

Same issue here. I have to remove koin viewmodel injection until there is support or a workaround for this.

We need to create a new koin android project to let you use the androidx library and keep compat with old support lib.

We are on it. Stay tuned 馃憤

I have done the change for our use. It really not that big, just some import and one dependency to change in the build.gradle.

For those who doesn't want to wait, just copy, paste the 3 classes inside android-architecture folder and update imports.

I've created a fork with an androidx version, see https://github.com/mattmook/koin, however I have not currently raised a PR as it depends on the yet unreleased Android-P as well as alpha versions of androidx.

@mattmook have you made it from the 1.0.0 branch?

@arnaudgiuliani no, but I have just reapplied the changes... https://github.com/mattmook/koin/tree/1.0.0

Assuming you are happy with the changes I can create a PR, as stated before I haven't yet because of the pre-release nature of androidx and android-p

Thanks @mattmook, I've just created the koin-androidx module - double check my changes with your changes. Just need a bit of time to test it and make deployment on bintray 馃憤

A first version is available under koin-androidx, version 1.0.0-alpha-11.

I see the other modules on bintray, but not koin-androidx. From what I remember of bintray new modules need to be explicitly linked to jcenter?

Argh ... yes :/ I need too link it to jcenter. Sorry.

koin-androidx is available in jcenter 馃憤

implementation "org.koin:koin-androidx:1.0.0-alpha-11"

@arnaudgiuliani thanks that works well

Looking at the updated LifecycleOwner.getViewModelByClass, with this is Fragment where fromActivity is true, should it read:

ViewModelProvider(this.activity.viewModelStore, KoinFactory)

Cool, thanks

note: package to check is koin-androidx-scope or koin-androidx-viewmodel

I can't download package
Failed to resolve: org.koin:koin-androidx:1.0.0-beta-3
while
implementation "org.koin:koin-androidx-scope:$koin_version"
implementation "org.koin:koin-androidx-viewmodel:$koin_version"
are doing fine

The first package is independent of androidx so is simply org.koin:koin-android:$koin_version

Documentation for the setup of the beta is https://beta.insert-koin.io/docs/1.0/documentation/koin-android/index.html#_gradle_setup

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iRYO400 picture iRYO400  路  3Comments

miladsalimiiii picture miladsalimiiii  路  3Comments

caleb-allen picture caleb-allen  路  4Comments

LukasAnda picture LukasAnda  路  3Comments

fmobus picture fmobus  路  4Comments