question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Android Module and KodeinGlobal

See original GitHub issue

I tried to imitate the bindings of android module in my Kodein Global. For example:

bind() from ProviderBinding(erased()) { ctx().getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager }

In order to avoid momory leak, I made it like this:

bind<ConnectivityManager >() with instance(this@MyApp.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager )

The code above didn’t work, I print the bindings out and this binding exists, I also try to null-check the instance from Kodein.global and it is not null but when I try something like:

Kodein.global.instance<ConnectivityManager >().activeNetworkInfo.isConnected

my app crash and stack trace said that I tried to call these function on a null reference.

Howerver, when I change the code to this and explicitly cast it at retrieve time, it’s ok:

bind<Any>("connectivity") with instance(this@MyApp.getSystemService(Context.CONNECTIVITY_SERVICE))

I don’t know if I’m doing anything wrong. I wonder why it become null just when I call a function on it and not when I check if it is null. Another question is that if it is null at binding time, why does it run without yielding any error until I retrieve the faulty binding?

P/S: Sorry if I said something stupid (I just started with android recently) or any typo (English’s not my native language). Hope someone help me with this or even make an android module exclusively for Kodein Global (of course, with some usage note to avoid leaking memory)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
InNoobWeTrustcommented, Jul 26, 2017

Well, after trying again with a refreshed mind I found the problem. It’s a stupid mistake I didn’t realize the important difference between the changes of my code.

The code that did not work:

if ((instance<ConnectivityManager>().activeNetworkInfo.isConnected)

Working one:

if(instance<Any>(Context.CONNECTIVITY_SERVICE) as? ConnectivityManager)
                            ?.activeNetworkInfo?.isConnected ?: false)

The one that it should be:

if (instance<ConnectivityManager>().activeNetworkInfo?.isConnected ?: false)

There it is, I miss one null check at first and suppose I misuse Kodein but the one I misuse is Android’s API. Well, I should never code when feeling tired again.

Anyway, thanks for your support, and great library you made. It solves many of my problems and simplifies my code a bunch. 👍

1reaction
SalomonBryscommented, Jul 26, 2017

Well, if instance<ConnectivityManager>().activeNetworkInfo.isConnected does not crash and yields false, that it cannot be an error in Kodein.

bind<Any>() is a really bad practice !

Read more comments on GitHub >

github_iconTop Results From Across the Web

Setting Up Android Modules With Kodein | by Elye - Medium
We have to put this kodein global static variable at the BaseModule , as we'll need to access it later from other modules....
Read more >
Guide to Android app modularization
This guide encompasses best practices and recommended patterns for developing multi-module Android apps. Note: This page assumes a basic ...
Read more >
Common modularization patterns - Android Developers
App modules are an entry point to your application. They can contain source code, resources, assets and an AndroidManifest.xml . The output of ......
Read more >
Configure the base module - Android Developers
Get started; The base module build configuration ... An app bundle is different from an APK in that you can't deploy one to...
Read more >
Add a module for a new device - Android Developers
Find out how Android Studio uses modules to make it easy to add new devices to your project.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found