【问题标题】:How's koin works with properties in android?加入如何与android中的属性一起使用?
【发布时间】:2021-08-22 09:04:29
【问题描述】:

我在官方 koin 文档中发现属性默认位置是 src/main/resources/koin.properties,但是当我尝试使用它时,我得到了这个漂亮的异常。 谁能解释一下 koin 如何在 Android 上使用属性?

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.app.AppApplication, PID: 12387
    java.lang.RuntimeException: Unable to create application com.example.app.AppApplication: org.koin.core.error.NoPropertyFileFoundException: No properties found for file 'koin.properties'
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6608)
        at android.app.ActivityThread.access$1500(ActivityThread.java:235)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1916)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:225)
        at android.app.ActivityThread.main(ActivityThread.java:7563)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:994)
     Caused by: org.koin.core.error.NoPropertyFileFoundException: No properties found for file 'koin.properties'
        at org.koin.core.registry.PropertyRegistry.loadPropertiesFromFile(PropertyRegistry.kt:100)
        at org.koin.core.KoinApplication.fileProperties(KoinApplication.kt:92)

使用 onCreate() 类:

override fun onCreate() {
        super.onCreate()
        startKoin {
            androidLogger(Level.NONE)
            androidContext(this@AppApplication)
                
            //copied from docs
            // Load properties from the default location 
            // (i.e. `/src/main/resources/koin.properties`)
            fileProperties("koin.properties") //CRASH

            modules(appModule)
        }
    }

【问题讨论】:

    标签: java android kotlin properties koin


    【解决方案1】:

    问题是我应该使用不带参数的fileProperties(),而不是fileProperties("filename")。 然后只需使用getProperty("property_name")

    这是因为/src/main/resources/koin.properties 是默认位置包括文件名...

    【讨论】:

    猜你喜欢
    • 2013-09-23
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 2023-01-21
    • 1970-01-01
    • 1970-01-01
    • 2016-12-08
    • 1970-01-01
    相关资源
    最近更新 更多