Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StrictMode violation in Application.onCreate on Android #96

Closed
mattmook opened this issue Mar 29, 2018 · 9 comments
Closed

StrictMode violation in Application.onCreate on Android #96

mattmook opened this issue Mar 29, 2018 · 9 comments
Assignees
Milestone

Comments

@mattmook
Copy link

mattmook commented Mar 29, 2018

With StrictMode enabled on Android, Koin v0.9.1 causes a violation due to disk access when initialised from the main thread in Application.onCreate. This is caused by the access to ClassLoader.getResources in bindKoinProperties.

03-29 22:03:25.213 27304-27304/? D/StrictMode: StrictMode policy violation; ~duration=80 ms: android.os.StrictMode$StrictModeDiskReadViolation: policy=196671 violation=2
        at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1440)
        at java.io.UnixFileSystem.getBooleanAttributes(UnixFileSystem.java:241)
        at java.io.File.isDirectory(File.java:837)
        at dalvik.system.DexPathList$Element.maybeInit(DexPathList.java:649)
        at dalvik.system.DexPathList$Element.findResource(DexPathList.java:682)
        at dalvik.system.DexPathList.findResource(DexPathList.java:488)
        at dalvik.system.BaseDexClassLoader.findResource(BaseDexClassLoader.java:144)
        at java.lang.ClassLoader.getResource(ClassLoader.java:793)
        at java.lang.Class.getResource(Class.java:2255)
        at org.koin.Koin.bindKoinProperties(Koin.kt:31)
        at org.koin.Koin.bindKoinProperties$default(Koin.kt:30)
        at org.koin.standalone.StandAloneContext.loadProperties(StandAloneContext.kt:77)
        at org.koin.standalone.StandAloneContext.startKoin(StandAloneContext.kt:101)
        at org.koin.standalone.StandAloneContext.startKoin$default(StandAloneContext.kt:95)
        at com.appmattus.aisfor.Application.onCreate(Application.kt:25)

StrictMode enabled with the following:

StrictMode.setThreadPolicy(
    StrictMode.ThreadPolicy.Builder()
        .detectAll()
        .penaltyLog()
        .penaltyDialog()
        .build()
)

Could a property be added tostartKoin to disable this initialisation by default as in most instances I assume this file won't exist so the attempted loading would be unnecessary?

@arnaudgiuliani arnaudgiuliani added this to the 0.9.2 milestone Mar 30, 2018
@arnaudgiuliani arnaudgiuliani self-assigned this Mar 30, 2018
@arnaudgiuliani
Copy link
Member

Fixed for 0.9.2. Please check the 0.9.2-alpha-3.

@mattmook
Copy link
Author

mattmook commented Apr 6, 2018

I'm still seeing the same issue with 0.9.2-alpha-3:

04-06 21:50:19.740 4378-4378/com.appmattus.aisfor D/StrictMode: StrictMode policy violation; ~duration=81 ms: android.os.StrictMode$StrictModeDiskReadViolation: policy=196639 violation=2
    at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1293)
    at java.io.UnixFileSystem.getBooleanAttributes(UnixFileSystem.java:240)
    at java.io.File.isDirectory(File.java:804)
    at java.io.File.toURI(File.java:698)
    at libcore.io.ClassPathURLStreamHandler.<init>(ClassPathURLStreamHandler.java:52)
    at dalvik.system.DexPathList$Element.maybeInit(DexPathList.java:532)
    at dalvik.system.DexPathList$Element.findResource(DexPathList.java:568)
    at dalvik.system.DexPathList.findResource(DexPathList.java:440)
    at dalvik.system.BaseDexClassLoader.findResource(BaseDexClassLoader.java:74)
    at java.lang.ClassLoader.getResource(ClassLoader.java:793)
    at java.lang.Class.getResource(Class.java:2139)
    at org.koin.Koin.bindKoinProperties(Koin.kt:31)
    at org.koin.Koin.bindKoinProperties$default(Koin.kt:30)
    at org.koin.standalone.StandAloneContext.loadProperties(StandAloneContext.kt:91)
    at org.koin.standalone.StandAloneContext.startKoin(StandAloneContext.kt:119)
    at org.koin.standalone.StandAloneContext.startKoin$default(StandAloneContext.kt:112)
    at com.appmattus.aisfor.Application.onCreate(Application.kt:25)

@arnaudgiuliani
Copy link
Member

did you disable the Koin properties loading from assets with loadProperties to false?

@mattmook
Copy link
Author

mattmook commented Apr 7, 2018

Hmm no, although I don't see a loadProperties in the alpha or in the branch 0.9.2 source, am I missing something?

So, at the moment I'm starting Koin with the following in my Application class:

override fun onCreate() {
    super.onCreate()

    enableStrictMode()

    startKoin(
        listOf(
            appContextModule(this),
            frescoModule()
        )
    ) with this
}

@arnaudgiuliani
Copy link
Member

replace your startKoin code with the Android start version:

startKoin(
    this,
    listOf(
            appContextModule(this),
            frescoModule()
        ),
    loadProperties = false
)

@mattmook
Copy link
Author

mattmook commented Apr 7, 2018

I've changed my code as above however that doesn't make a difference because the issue is caused by the attempt to load koin.properties within the StandAloneContext.

@arnaudgiuliani
Copy link
Member

Ok. I'll check that.

@arnaudgiuliani
Copy link
Member

Ok. Fixed in 0.9.2-alpha-5

@mattmook
Copy link
Author

Thanks, works like a charm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants