3

My Problem might not be valid. And the points I mention here might be little incorrect as I am neither perfect nor expert.

I have a shopping application and I want to start building a modular application. Like I want to add Affiliate User( the module which adds certain functionality or this will display some extra pages in an application) in the application.

A similar situation happens in the Frameworks: We add and enable the certain module and in return framework load everything as required.

for this, I want following changes like:

  1. add an entry in the NavigationView displaying "Affiliate Label".
  2. load fragments (just adding one more fragment for one more label/option from navigation view).

Let's say I have a library project that contains a Fragment and all relevant code.

How can I build the application automatically let's say just by writing "true" somewhere in the XML?

Automatically here means label is added, Intents are performed on click of label etc.

<Modules>
<enable>true/false</enable>
</Modules>

This is just the simple scenario.

Shubham AgaRwal
  • 4,355
  • 8
  • 41
  • 62

2 Answers2

1

You could do this (like everywhere when it comes to writing code) in many possible ways.

  1. The "file" way: Make a new file named something like modules.txt with key value pairs. Load the file and check whether a module is enabled or not.
  2. The "Constant" way: Make an abstract class which only contains public static final variables which describe your modules.
  3. The "package manager" way: See create Android Application plugins/extensions (apk)
  4. The "multiple" apk way: Note that this is not reccomended!

we encourage you to develop and publish a single APK

multiple apk support

codewing
  • 674
  • 8
  • 25
1

To add to codewing's answer, you can also use Gradle's resource management capabilities to accomplish this, so you only ever need to look in one place for an enabled/disabled status.

For this, you have 2 solid options.

The first starts with a boolean which can be split by flavor:

<bool name="module_x_enabled">true</bool>

The second would be to inject your values into a String resource after Gradle merges the resources by adding something like this to your build.gradle file, then comparing that enabled value:

<string name="module_x_enabled">MODULE_X_ENABLED_PLACEHOLDER</string>


android.applicationVariants.all{ variant ->
    variant.mergeResources.doLast{    
        replaceInValues(variant, 'MODULE_X_ENABLED_PLACEHOLDER', MODULE_X_ENABLED)
    }
}

def replaceInValues(variant, fromString, toString) {
    File valuesFile = file("${buildDir}/intermediates/res/merged/${variant.dirName}/values/values.xml")
    String content = valuesFile.getText('UTF-8')
    content = content.replaceAll(fromString, toString)
    valuesFile.write(content, 'UTF-8')
}

Where MODULE_X_ENABLED would be a setting in your gradle.properties file like:

MODULE_X_ENABLED=true 

Edit: or better yet,

Why not pull the settings from some kind of server so that you don't need to rebuild and relaunch to update a client's module?

Cruceo
  • 6,763
  • 2
  • 31
  • 52
  • Sorry @Guardanis, I didn't understood much about the process related to changing strings only and gradle. As i am a new to android technology. I will try to get more relevant information so that i can understand what is the content all about. – Shubham AgaRwal Mar 09 '16 at 17:25
  • Please guide the things should i refer to like (understanding gradle process etc) or please explain a bit more. – Shubham AgaRwal Mar 09 '16 at 17:26
  • Are you using Gradle/Android Studio or was your question for another build system? – Cruceo Mar 09 '16 at 18:14
  • yes my question was for Gradle/Android Studio. but i didn't understand how changing strings makes everything works. – Shubham AgaRwal Mar 09 '16 at 19:21
  • It allows you to check your resources via Boolean.valueOf(context.getString(R.string.module_x_enabled)) and determine whether the module should be implemented within your app or disabled. Similar to codewing's #2 using constants, just using gradle to change your resources – Cruceo Mar 09 '16 at 22:30
  • I am sorry but the question was bit different. May be i confused a little bit (Sorry i said bit)... I want to learn the architecture/(Modular approach) so that whenever i add library module_x in the main project and enable it by your method or from codewing's method. All the work is done automatically – Shubham AgaRwal Mar 10 '16 at 06:17
  • think like app is like browser you add a addon it make an entry on the toolbar and make some actions. Similar way in application you add a library project we get a label on navigation view and corresponding action. This is just a simple scenario. What if many modules interact amny dynamic changes are to be done. What architecture should be envolved – Shubham AgaRwal Mar 10 '16 at 06:19
  • 1
    I think I see what you're saying now. But the issue is: you still have to tell the system about your module no matter what it is. Even if you're using a List or something like that, you still have to add your modules to that list in order for them to work (which is what codewing's and my answer is more about; determining whether it should add the module you've already implemented to that list or not). AFAIK there is no way to just add a library and have it automatically implement itself, even if similar modules are already implemented. – Cruceo Mar 10 '16 at 15:53