https://kotlinlang.org logo
#dagger
Title
# dagger
c

Colton Idle

08/12/2021, 4:30 PM
More of a DI/android discussion than dagger specific, but was just curious to hear if people try to wrap Android BuildConfig into some kind of wrapper class. Every time I use BuildConfig.debug or BuildConfig.someApiKey I feel like I shouldn't access it directly.
j

Javier

08/12/2021, 4:37 PM
you can use the source sets instead of BuildConfig
c

Colton Idle

08/12/2021, 4:40 PM
@Javier sorry, not following? Are you saying source sets as in different directories /free and /paid and stuff? I'm assuming I would do that, but I guess I'm more curious whether people feel like it's overkill to Dependency Inject BuildConfig?
j

Javier

08/12/2021, 4:41 PM
Yeah, it is not overkill, indeed you can disable the generation of buildConfig improving the build time
c

Colton Idle

08/12/2021, 4:46 PM
Hm. There's another idea. Just disabling buildConfig completely... that wasn't my original intent with my question... but maybe?
t

trevjones

08/12/2021, 5:34 PM
I do data class for the value that needs to be injected down to JVM modules.
Copy code
data class BuildConfigDebug(val value:Boolean)
j

Jeremy

08/12/2021, 7:54 PM
As long as dependency is at the appropriate layer seems same tradeoffs as dependencies on Android framework. Adding more layers/abstractions can make the system harder to reason about
w

wasyl

08/12/2021, 7:55 PM
What does the build config contain for you? For me each value is just provided from application-wide scope, maybe with a qualifier
j

Jeremy

08/12/2021, 7:55 PM
If you have a clean architecture domain layer you shouldn't have dependencies on
BuildConfig
or Android framework in there
w

wasyl

08/12/2021, 7:56 PM
So only dagger application module accesses BuildConfig, everything else uses those values as regular provided dependencies
j

Jeremy

08/12/2021, 7:58 PM
Certain things like feature flags, configuration values (api servers) I often put into dagger qualifiers. It decouples from implementation and provides a way to provide to all modules but its more layers/abstractions so has tradeoffs
If need to provide
BuildConfig.DEBUG
into libraries that are pre-compiled need to inject in somehow, not much choice
c

Colton Idle

08/12/2021, 8:15 PM
In MyApplication.kt I mostly have a bunch of configuration based on BuildConfig. Example:
Copy code
if (BuildConfig.debug){
Firebase.getInstance().getCrashlytics(disabled = true)
} else {
Firebase.init(BuildConfig.MY_FIREBASE_API_KEY)
}
So just looking at that snippet. I have this feeling that I should provide a wrapper around BuildConfig fields AND Firebase lol
j

Jeremy

08/12/2021, 9:35 PM
^ idk personally I think that seems fine
that code is also within the android framework and probably
Application.onCreate
or something?
If it was under test and you wanted to provide alternate behavior you could wrap it as considering and inject in a fake... but I'm not sure you'd want to run
Application.onCreate
in a test...
If you're trying to test an activity or something you can load the activity in the fake application provided by dagger hilt
or if you wanted to abstract the behavior so it wasn't "if build was compiled with debug flag" but "if we are in development testing mode" where it wasn't necessarily debug variant...
that may be useful in scenarios like instrumented tests where you wanted to specify diff behavior
c

Colton Idle

08/13/2021, 2:55 PM
Yeah, I guess I'm not even looking at testing this stuff yet. It's just that I see that I'm strongly tying myself here to BuildConfig and wanted to see if people are larger companies typically just abstract away BuildConfig and don't access it directly.
j

Jeremy

08/13/2021, 4:58 PM
Projects I've seen we only needed
BuildConfig
in few places usually around err handling and
Application.onCreate
so just used direct
c

Colton Idle

08/13/2021, 4:58 PM
Gotcha. Appreciate the insight and experience
2 Views