Purple Material Design Theme

Wondering what your phone’s UI would look like in purple? This theme for CyanogenMod 12 made by XDA Senior … more

Both Cellular Nexus 7s Get 5.0.2 Lollipop!

If you are eager to try out the latest and greatest on your Nexus 7 LTE (2013) or Nexus 7 Mobile … more

Google Now Update Brings Third Party Cards

The latest update for Google Now brings the support of third party cards fromover 30 … more

The Witcher Battle Arena: Hectic Multiplayer MOBA Fun

Do you like MOBAs? Do you like gaming on your phone? Given that MOBAs (multi player … more

Welcome to XDA

Search to go directly to your device's forum

Register an account

Unlock full posting privileges

Ask a question

No registration required
Post Reply

[Q] [DEV][Gradle] How to make a modular Application ?

OP joplayer

26th January 2015, 10:42 AM   |  #1  
joplayer's Avatar
OP Senior Member
Flag Valence
Thanks Meter: 94
 
252 posts
Join Date:Joined: Aug 2008
Donate to Me
More
Hi guys,

I'm currently working on an app for a company that asked me to make it modular.
Actually the app contains different features and the company would like to be able to compile the differents flavors of the App via Gradle.
If I understood correctly the current flavor solution offered in Android Studio doesn't fit my needs as if I have like 5 differents modules I'd have to make a flavor for : feature 1, feature 1 + 2, feature 1 + 2 + 3, feature 1 + 2 + 3 + 4, feature 1 + 2 + 3 + 4 + 5, feature 2, feature 2 + 3, ...... and so on.

So how can I say to gradle I just want to compile this part of my code ?
And how can I manage my code in a clean way for my app to understand which modules are enabled or not ?

Thanks a lot for your answers !
26th January 2015, 04:27 PM   |  #2  
Senior Member
Flag Austin,TX
Thanks Meter: 76
 
1,111 posts
Join Date:Joined: Jul 2007
More
Not sure I'm understanding the problem. Create the flavors and in each of the src/<flavor>/ directories you can specify a build.gradle file and in that gradle file you can add dependencies for other modules. So this modular code would exist in <project root>/libprojects and the separate flavors would then conditionally include that code as a dependency. Make sense?

.
libprojects/module1
libprojects/module2
libprojects/module3
src/flavor1/build.gradle
src/flavor2/build.gradle
/src/flavor3/build.gradle

Each of those build.gradle files in the separate flavors could include dependencies for modules that it needs. Remember, code in a flavor takes precedence over code in src/main/java. So use that to your advantage to creating modular code.
The Following User Says Thank You to shafty023 For This Useful Post: [ View ]
26th January 2015, 04:56 PM   |  #3  
joplayer's Avatar
OP Senior Member
Flag Valence
Thanks Meter: 94
 
252 posts
Join Date:Joined: Aug 2008
Donate to Me
More
Thanks a lot for your answer

It kinda give me an answer for the concept of isolating code on libraries. But the code isn't the biggest issue for me. Is it possible to put the resources (drawable and raw) in the libs so that I can grab them only on the required flavor of my app ? And do this via gradle dependency as I would have too much flavors otherwise.

Thanks again
26th January 2015, 06:42 PM   |  #4  
Senior Member
Flag Austin,TX
Thanks Meter: 76
 
1,111 posts
Join Date:Joined: Jul 2007
More
Quote:
Originally Posted by joplayer

Thanks a lot for your answer

It kinda give me an answer for the concept of isolating code on libraries. But the code isn't the biggest issue for me. Is it possible to put the resources (drawable and raw) in the libs so that I can grab them only on the required flavor of my app ? And do this via gradle dependency as I would have too much flavors otherwise.

Thanks again

FYI, make sure you tap "reply" in the future so a person's post is "quoted" otherwise that person, me, doesn't get notified you responded from XDA. I just happened to click back on this post. Didn't receive a notification.

When you say libs do you mean "libprojects" folder or do you mean libs as in creating a jar file with those resources? Don't forget you can copy the dependencies from another flavor if you need to at compile time. Not sure if that would help in your scenario.
27th January 2015, 04:57 PM   |  #5  
joplayer's Avatar
OP Senior Member
Flag Valence
Thanks Meter: 94
 
252 posts
Join Date:Joined: Aug 2008
Donate to Me
More
Quote:
Originally Posted by shafty023

FYI, make sure you tap "reply" in the future so a person's post is "quoted" otherwise that person, me, doesn't get notified you responded from XDA. I just happened to click back on this post. Didn't receive a notification.

When you say libs do you mean "libprojects" folder or do you mean libs as in creating a jar file with those resources? Don't forget you can copy the dependencies from another flavor if you need to at compile time. Not sure if that would help in your scenario.

Sorry, bad old reflex

Actually that is my problem. How am I supposed to isolate my features ? Should I make an Android library ? Should I brutally use gradle to isolate some files ?

Thanks again
27th January 2015, 05:02 PM   |  #6  
Senior Member
Flag Austin,TX
Thanks Meter: 76
 
1,111 posts
Join Date:Joined: Jul 2007
More
Quote:
Originally Posted by joplayer

Sorry, bad old reflex

Actually that is my problem. How am I supposed to isolate my features ? Should I make an Android library ? Should I brutally use gradle to isolate some files ?

Thanks again

It really depends on what you mean by isolate features. If you mean paid vs free app then you should undoubtedly isolate those features with a gradle variable that is set at compile time and read from at runtime via the BuildConfig class.

Code:
    if (BuildConfig.IS_PRO) {
        ... some code
    }
It's more of a mess to exclude code then to just make a single code source where you conditionally include/exclude based on compile time variables.
27th January 2015, 05:21 PM   |  #7  
joplayer's Avatar
OP Senior Member
Flag Valence
Thanks Meter: 94
 
252 posts
Join Date:Joined: Aug 2008
Donate to Me
More
Quote:
Originally Posted by shafty023

It really depends on what you mean by isolate features. If you mean paid vs free app then you should undoubtedly isolate those features with a gradle variable that is set at compile time and read from at runtime via the BuildConfig class.

Code:
    if (BuildConfig.IS_PRO) {
        ... some code
    }
It's more of a mess to exclude code then to just make a single code source where you conditionally include/exclude based on compile time variables.

Thanks a lot I think this kind of syntax might help me.
To make things clear the product will be compilable for different end products. One might want geolocalization, another not. As features end up taking space in the APK I need to exclude them from compilation. And finally this app must be compilable out of Android Studio so Gradle is my only savior ! And I'm far from beeing a Gradle expert so thanks for your answer I'll look at it
Last edited by joplayer; 28th January 2015 at 08:35 AM.

Post Reply Subscribe to Thread

Tags
android studio, application, gradle, modular
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes