我最近更新了Kotlin
to的版本1.4.0-rc
。
从那时起Koin
返回以下运行时错误:
java.lang.NoSuchMethodError: No virtual method elapsedNow()D in class Lkotlin/time/TimeMark
考虑这个例子。
对于身份验证,我们将使用 2 个屏幕 - 一个屏幕用于输入电话号码,另一个屏幕用于输入 OTP。
这两个屏幕都是用 Jetpack Compose 制作的,对于 NavGraph,我们使用组合导航。
另外我必须提到 DI 是由 Koin 处理的。
val navController = rememberNavController()
NavHost(navController) {
navigation(
startDestination = "phone_number_screen",
route = "auth"
) {
composable(route = "phone_number_screen") {
// Get's a new instance of AuthViewModel
PhoneNumberScreen(viewModel = getViewModel<AuthViewModel>())
}
composable(route = "otp_screen") {
// Get's a new instance of AuthViewModel
OTPScreen(viewModel = getViewModel<AuthViewModel>())
}
}
}
Run Code Online (Sandbox Code Playgroud)
那么,我们如何在 Jetpack 组合 NavGraph 中的两个或多个可组合项之间共享相同的视图模型呢?
kotlin koin android-jetpack-compose jetpack-compose-navigation
我想使用 DataStore 存储一些首选项。但问题是我的应用程序可以有多个用户,因此需要将这些首选项存储在单独的文件中。我得到了一个仅使用一个用户的工作示例,但我正在努力支持多个用户。
这是我的代码的示例:
class DataStorageRepository(private val context: Context, private val userRepository: UserRepository) {
private object PreferencesKeys {
val SETTING_ONE = intPreferencesKey("setting_one")
}
// retrieve datastore for currently logged in user.
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = userRepository.currentRegistration().name)
val userPreferencesFlow: Flow<UserPreferences> = context.dataStore.data.map { preferences ->
val settingOne = preferences[PreferencesKeys.SETTING_ONE] ?: 0
UserPreferences(settingOne)
}
suspend fun storeSettingOne(settingOne: Int) {
context.dataStore.edit { preferences ->
preferences[PreferencesKeys.SETTING_ONE] = settingOne
}
}
data class UserPreferences(val lastUsedToAccountTab: Int)
}
Run Code Online (Sandbox Code Playgroud)
我正在使用Koin
并尝试卸载DataStorageRepository
注销并在登录时重新创建它,但数据存储似乎一直保持活动状态,直到应用程序被终止并且出现以下崩溃: …
android koin android-jetpack coroutinescope android-jetpack-datastore
您更喜欢使用Kotlin安装Android的依赖注入事项?我已经开始使用Kodein,但我不想浪费我的时间,如果Koin更好.
我已经阅读了Makery Kotlin Development的演示文稿https://www.kotlindevelopment.com/koin-vs-kodein/,这真的很棒.
从该演示文稿中可以看出这些差异:
Kodein
孝允
Github明星 Kodein 1164 vs 1350 Koin
谢谢 !!
我有一个ViewModel
它有它应该从采取的依赖Fragment
的arguments
。
所以它是这样的:
class SomeViewModel(someValue: SomeValue)
Run Code Online (Sandbox Code Playgroud)
现在这个片段SomeValue
在它的争论中收到了这样的:
class SomeFragment : Fragment() {
val someViewModel: SomeViewModel by viewModel()
companion object {
fun newInstance(someValue: SomeValue) = SomeFragment().apply {
arguments = bundleof("someKey" to someValue)
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题是我不知道如何添加SomeValue
从Fragment
's arguments
to Koin
's 模块中获取的内容。
有没有办法让片段对 Koin 依赖图有所贡献?
每次我旋转手机时,无论 isDetached
值是否为 false,它都会崩溃并在 SearchResultFragment 中打印奇怪的异常:
java.lang.IllegalStateException: Can't access ViewModels from detached fragment
Run Code Online (Sandbox Code Playgroud)
在这部分代码中:
private val searchViewModel: SearchViewModel by viewModel()
fun searchWord(word: String) {
if(!isDetached) searchResultViewModel.searchWord(word)
}
Run Code Online (Sandbox Code Playgroud)
这是从另一个片段 SearchFragment 调用的:
searchResultFragment.searchWord(searchWord)
Run Code Online (Sandbox Code Playgroud)
SearchResultFragment 添加到 SearchFragment 中的方式如下:
private val searchResultFragment = SearchResultFragment()
private fun addFragment() {
val fragmentTransaction = childFragmentManager.beginTransaction()
fragmentTransaction.add(R.id.searchContainer, searchResultFragment)
fragmentTransaction.commit()
}
Run Code Online (Sandbox Code Playgroud)
我正在使用 Koin 进行依赖注入。我将不胜感激所有的提示。
我们最近将主要的Application类转换为Kotlin.
从那时起,我们遇到了崩溃,特别是在夜间(当我们的应用程序可能被系统杀死时),当我们的JobService是startet时.
我们以静态方式访问应用程序上下文以获取某些依赖项,这些依赖项在将类转换为Kotlin之前运行良好.从那以后,静态getter是lateinit var
在应用程序onCreate
函数中初始化的.
发布后Google Play报告了这些崩溃:
Caused by: kotlin.UninitializedPropertyAccessException:
at x.y.z.application.App.access$getAppContext$cp
[...]
at x.y.z.jobs.JobSchedulerService.onCreate (JobSchedulerService.java:27)
Run Code Online (Sandbox Code Playgroud)
这导致了问题,我们还Application.onCreate()
没有执行?
我们稍微重构了JobService,以减少静态上下文访问量,直到需要进行大量重构.之后,我们在Google Play控制台中收到了用户的这些崩溃消息:
Caused by: kotlin.UninitializedPropertyAccessException:
at org.koin.standalone.StandAloneContext.getKoinContext (StandAloneContext.java:45)
at org.koin.java.standalone.KoinJavaComponent.get (KoinJavaComponent.java:66)
at org.koin.java.standalone.KoinJavaComponent.get$default (KoinJavaComponent.java:64)
at org.koin.java.standalone.KoinJavaComponent.get (KoinJavaComponent.java)
at x.y.z.SearchState.<init> (SearchState.java:21)
[...]
at x.y.z.jobs.JobSchedulerService.onStartJob (JobSchedulerService.java:54)
Run Code Online (Sandbox Code Playgroud)
这些崩溃告诉我们同样的事情:Application.onCreate()
由于Koin没有初始化,因此尚未执行.
那我的问题呢?Application.onCreate()
转换到Kotlin时为什么要执行更改时间?为什么在JobService启动之前我们的应用程序不再创建?
我的意思是,当然,我们可以重构整个应用程序依赖项以使用JobService本身提供的上下文,但是如果应用程序是在之后创建的并且我们仍然想要使用Koin呢?我们的应用程序可能会再次崩溃AlreadyStartetException
.如果我们的应用程序尚未"存在",那么该服务将具有哪些上下文?
应用
abstract class App : MultiDexApplication() {
companion object {
@JvmStatic
lateinit var appContext: Context
@JvmStatic
val isDevelopment: Boolean = BuildConfig.DEBUG
// @JvmStatic
// …
Run Code Online (Sandbox Code Playgroud) 我试图用 DI 的 Hilt(坏主意)替换 Koin,现在我遇到了这个错误:Hilt_App.java:21: 错误:找不到符号返回 DaggerApp_HiltComponents_ApplicationC.builder() ^ 符号:变量 DaggerApp_HiltComponents_ApplicationC
它是什么?如何解决?
我正在尝试使用 Koin 注入我的 viewModel (它也有一些依赖项),如下所示:
我不明白为什么当我进行以下导入时它找不到 getViewModel:
我正在使用这个 Koin 版本:实现“io.insert-koin:koin-android:$koin_version”
其中 $koin_version = '3.2.0-beta-1'
有什么想法为什么我的导入在这里被忽略?
我最近1.3.70
在我的 Android 项目中更新了 Kotlin 的版本。
从那时起Koin
(版本2.1.0-beta-1
:)给了我下面的运行时错误:
java.lang.NoClassDefFoundError: Failed resolution of: Lkotlin/time/MonoClock;