r/androiddev • u/curiousdeveloperr • Mar 23 '22
Removed: No meme / low effort posts android devs in multi-modular projects be like
19
u/konichiwaaaaaa Mar 23 '22
How do you flatten your dependency graph?
31
u/curiousdeveloperr Mar 23 '22
this is a good starting point
1
u/haroldjaap Mar 23 '22
Nice!
1
Apr 21 '22
Maybe I should comment on the article itself but I don't get the example used. If you need to use analytics module in feature-users, you are going to have to add the analytics dependency to feature-users. How else are you going to do it?
1
u/johnstoehr83 Aug 22 '22
Create an interface and place it in core module or a new module which both config and analtyics depend on.
13
1
27
12
u/slightly_salty Mar 23 '22
M1 max with 64gb ram. Even Android Studio memory leaks can't slow me down now! 😏
20
u/lnkprk114 Mar 23 '22
Every multi-module app I've worked on has taken way longer to build than the single module apps I've worked on. I'm sure they just didn't get the dependency tree correct but boy howdy am I bummed when see a new multi module app these days.
17
u/CrunchitizeMeCaptain Mar 23 '22
The real benefit of a multi modular app is with the incremental compilation. You want to make sure you are getting as close to re-compiling only what you have changed, and that's what will lead to a faster build time.
My team is getting close to about 30 modules so far. The app has been alive for well over 7 years and over a million lines of code. And when I work on a particular feature, my build times are generally 40s-1.5min.
If I change something like a core-level module that is a dependency of everything (which shouldn't happen often) that essentially invalidates the whole app's build cache, that compilation time would take close to 20min depending on the machine. That compilation time would happen every time if we had one single app module.
15
u/lnkprk114 Mar 23 '22
Yeah, I buy that incremental builds become way faster.
My real pain point is that IME gradle syncs become way slower, and because there are so many modules you're faaaaaar more likely to have gradle syncs.
So for my current project which has ~150 modules, every time I switch branches I get up and do something else because it takes 10 minutes to do the actual gradle sync.
Who knows how long it would take to do that sync if it wasn't a multi module app - but I'll say this, I've worked on huge single module apps and I've never had a ten minute sync in one before. But it's been fairly normal for me in all of the 20+ module multi module apps I've worked in. And it seems to scale linearly with the number of modules, so that time just gets worse and worse as time goes on.
Then there's the extraordinary amount of added complexity that comes with multi module apps and it really leaves me feeling like it's not the right choice for the majority of apps. For huge apps, like Uber and Facebook and so on it's a requirement. But for joe-shmoe medium sized companys app? I don't think so.
9
u/droidxav Android Studio Team Mar 23 '22
10 minute sync for 150 modules seems (way) more than expected. We see this kind of numbers for project with 1,000+ modules. I'm not sure what the exact problem could be but is your configuration time slow (what if you do just
./gradlew project?)You are right though that more modules directly impact sync simply because we have more dependency graphs to resolve. We are working with Gradle to improve things with parallelism and caching but it's going to take a bit of time for things to land in Studio.
Then there's the extraordinary amount of added complexity that comes with multi module apps and it really leaves me feeling like it's not the right choice for the majority of apps.
I agree with the first part of the statement. It's not just about sync time but it's also about managing dependency, code architecture, etc... This is more complicated at least on the surface.
Having separate module enforce better separation of concern so it can make your codebase cleaner in the long term. It's a small forcing function and not the only one but it helps.
But there is a wide spectrum of project layout, from a single module to 1,000s of modules, and you should find what works for you and your app. Definitively don't think that reach high number of modules is the right goal for all apps, but a few modules is generally too little for a large app. Finding the right balance is hard and it's not easy to run before/after tests either sadly.
8
u/lnkprk114 Mar 23 '22 edited Mar 23 '22
I'm very open to the idea that my current app (as well as the other multi module apps I've worked on) were configured incorrectly (or maybe even terribly!) but that's part of the problem - it's absolutely non trivial to correctly configure a multi module app. That's a tremendous amount of added build complexity, which is always a huge, huge bummer. Managing builds on Android has always been painful, but multi module architectures just add another giant layer of complexity. Sometimes complexity is inevitable and necessary, I'm just not convinced that this is one of those times.
Code architecture is of course absolutely necessary; you just don't need different modules to do that. Any separation that you get from having different modules could be achieved if everything was in the same module. IMO having your build system be the thing that enforces separation of concerns in your code is questionable.
And yeah find what works best etc etc. The question is for your average, medium sized app is a multi-module app the best approach - my argument is that it is not. Eventually your kind of forced into a multi module approach because your incremental builds take 10+ minutes, but IME the vast majority of apps are not in that space.
5
u/droidxav Android Studio Team Mar 23 '22 edited Mar 23 '22
Code architecture is of course absolutely necessary; you just don't need different modules to do that. Any separation that you get from having different modules could be achieved if everything was in the same module. IMO having your build system be the thing that enforces separation of concerns in your code is questionable.
I agree. I didn't mean to say that this was the only way to achieve this. It can simply be a forcing function.
I'd love to hear more about the complexity of configuring multi-module builds. Dependency management is an obvious point here and there are plugin that can help here (like https://github.com/autonomousapps/dependency-analysis-android-gradle-plugin for instance), but I'd be happy to hear more pain points.
There's no one-size-fits-all for module count. It depends a lot on the app, its code base size, the number of people working on it, etc... "vast majority" is also kind of vague :) but I can tell you that most apps opened in Studio have very few modules indeed.
2
u/lnkprk114 Mar 30 '22
Sorry this took a long time, but here are some complexities I've run into:
Like you mentioned, dependency management goes from being a mostly complexity-free, default solution technical problem to a more complex, not fully supported by the tooling (i.e. the highlighting that says you're on an old version), customized technical problem. Instead of going into a project knowing how dependency management works, it's a new learning point, a new point of complexity, and a new place for bugs to crop up.
DI, which IME is already a challenge to many developers, becomes even more challenging. You're now forced to deal with additional complexity in managing your DI graph and module structure. Maybe hilt fixes this - I haven't used it yet.
I've found you often have drastic duplication of code in multi module applications because you end up having to duplicate model objects between the different modules. Or you pull them out into a more common module (again this decision is a point of complexity that just doesn't exist in monoliths) but that leads to another problem:
It's very easy to end up with a sub optimal module hierarchy structure. Assumptions you made about what modules should depend on what other modules can easily break. IMO you end up with a similar pain point with multi module applications as you have with very OOP oriented codebases - you have a structure that makes sense at the time of design but when your business realities change it becomes an inflexible structure that either encourages a lot of strange workarounds or interconnectedness which then defeats the build speed improvements you introduced it for.
IME gradle problems are much, much more common in multi module applications than in monoliths (where they're admittedly still common). It's not any single specific problem I've found - it's just that gradle edge cases seem to creep up pretty often.
This one is comparatively minor, but the weight of building a new feature goes up. The difference between adding a new package in a monolith vs a new module is pretty large - the new module has to force a sync, it has a new build file, you have to determine what dependencies it has and how it exposes its dependencies, you have to make a new file structure and so on. That's all burden that can go wrong, introduces new complexity, and isn't required in a monolith.
Ultimately my main complaint is it's just a bunch more complexity. Android is already a challenging sphere to break into - I feel like having multi module applications become the default is just going to make it that much harder for juniors to grok what is happening. It makes it harder for me to grok what's happening and I've been doing this for close to a decade. I feel like we should always be striving for more straightforward engineering structures (obviously not at the cost of performance and testability and so on), and this feels like moving in the opposite direction.
And again to reiterate it is absolutely necessary for some applications. Uber is going to have a complicated build structure, that's just the reality of being a big old company with a big old codebase. But does the new startup building an app that interacts with your smart plant feeder need that additional complexity?
4
u/farmerbb Mar 23 '22 edited Mar 23 '22
So for my current project which has ~150 modules, every time I switch branches I get up and do something else because it takes 10 minutes to do the actual gradle sync.
I've also worked on a project with ~150 modules (each screen was its own module) and this has been my exact experience. 10-minute Gradle syncs are painful.
Which is funny because this actually validates the OP's meme somewhat. Personally I'd rather deal with a project that requires an occasional full rebuild whenever enough changes have occurred for the build cache to bust, rather than having to constantly resync Gradle whenever modules are added or removed.
4
u/Zhuinden Mar 23 '22
Then there's the extraordinary amount of added complexity that comes with multi module apps and it really leaves me feeling like it's not the right choice for the majority of apps.
^ this tbh
1
u/farmerbb Mar 23 '22
Yep. IMO you should stick with just a few core modules, domain-layer stuff that isn't changed very often but is heavily utilized throughout your app.
1
Mar 23 '22
I find that I get sensory overload if I don't work in modules, even for personal projects. Oh well
1
2
u/s73v3r Mar 24 '22
Question: Has anyone done the math on the time spent implementing and working with the multi-module stuff vs the time spent saved with incremental builds?
1
u/Zhuinden Mar 24 '22
At this point, we could go back to single-core CPUs and we'd have better quality / easier to maintain apps 😂
1
Mar 23 '22
My team is getting close to about 30 modules so far. The app has been alive for well over 7 years and over a million lines of code.
I'm really curious what kind of app is it. Finance?
2
0
Mar 23 '22
[removed] — view removed comment
1
u/lnkprk114 Mar 23 '22
I believe it!
But either way I'm being slowed down, which is what I care about.
2
Mar 23 '22
[removed] — view removed comment
1
u/lnkprk114 Mar 23 '22
I guess the thing I'm struggling with is like for me, the medium-big (not huge) monolith apps it would take like 3-4 minutes to run a build - sometimes less if stuff was cached but sometime in that timeframe. My gut feeling is that a similar multi module app takes like...1.5 minutes to build an incremental change, so definitely an improvement over the monolith, but like 7-8 minutes to run gradle syncs or an entire build, which is slower than for the monolith.
If that was just a switch I could flip then I'd take that trade. But in reality there's a ton of added build complexity when you have mutli module apps, and the gradle syncs seem to happen much more often so that larger sync time ends up eating up a bunch of my incremental build improvements.
Then you start doing things like abstracting module dependencies using interfaces and whatnot and now you've added a lot of code complexity as well as build complexity all to facilitate multi module builds to gain that incremental build speed improvement.
From where I'm standing, the tradeoff doesn't seem worth it until your incremental builds actually take like 10+ minutes. And that should be a sad decision to make, because now we've added so much complexity to our codebase.
1
Mar 23 '22
[removed] — view removed comment
2
u/lnkprk114 Mar 23 '22
Super legit. 40 second incremental build time seems awesome. I'm just so, so wary of all of the added abstractions and complexity. It takes a long time to actually write but then it takes a long to to maintain too.
I think the thing I'm pushing back against is the idea that multi-module should be the default for new projects. I think it helps a bunch when you're sufficiently large, but I think it hurts a bunch until then.
It kind of feels like microservices to me - super necessary if you're Netflix scale, super harmful if youre 3 person startup scale.
1
Mar 23 '22
[removed] — view removed comment
2
u/lnkprk114 Mar 23 '22
It's made more challenging too by the fact that it's harder to modularize from a monolith then it is to just start with a modular setup.
But like, if you're slowed down by 10% because you use a multi module approach, which I think is absolutely a realistic number, then that's a huuuuuuuuuuuuuge cost to bore for a new app before it becomes actually helpful.
As far as I can tell Google is pretty in on multi module apps. Maybe they'll find a way to abstract the build code so it doesn't make me went to die so much lol
16
Mar 23 '22
Third option: use 100% Java.
4
u/rmvt Mar 23 '22
do you know of any comparisons/stats/etc that would make the argument for using 100% java when pursuing faster compilation (with multimodule)?
0
u/VasiliyZukanov Mar 23 '22
Funny how this comment is downvoted, while being absolutely valid. The hype is strong on this subreddit.
Have my like, fellow stranger.
8
u/wannagotopopeyes Mar 25 '22
Migrating from Kotlin to Java to cut down on build times is like switching from a power drill to a screwdriver to save electricity.
1
u/VasiliyZukanov Mar 25 '22
If you don't use Kotlin to begin with, you don't need to "migrate" -> profit
4
u/Initial-Cherry-3457 Mar 23 '22
How does using Java instead of Kotlin speed up multi module builds though?
3
u/Orangutanion Mar 23 '22
Kotlin has notoriously slow build time
6
u/mntgoat Mar 23 '22
It would be interesting to compare time wasted compiling vs time wasted writing code that Kotlin doesn't make you write or things that are just simpler on kotlin.
1
u/Zhuinden Mar 24 '22
I have a feeling it greatly depends on the readability of the Kotlin code being written. Java doesn't allow a lot of leeway, while Kotlin lets you do whatever you want and name every variable
itif you feel likeit-5
2
1
1
1
0
49
u/occz Mar 23 '22
I mean, one is comparatively simple. Just throw some money at the problem and you can have an M1. Modularisation takes some real effort to achieve successfully.