Hossam Hassan
Hossam Hassan

Reputation: 791

Build Single Apk with multiable template/flavors

Is it possible to build apk with more than 1 product flavor?

For example: I have a Project with 3 flavors (App1 - App2 - App3).
and each app has its own configurations such as applicationId and so on.

and now I want to build different templates (different XML layouts), and the user should be able to switch from layout to another from inside the app.

My problem is the res folder will be huge and it will be hard to maintain, so I'm trying to find a way to separate the different layouts and keep it clean as much as possible.

And if it's possible to do so, then how do I intent or restart the app to the other flavors build?

Another thing I had in mind was to build all XML files in the main res and choose different qualifiers like when we do while creating different screen size (sm - larg - etc.. ) but i couldnt find anyway to add custom qualifiers.

My Gradle code is like this :

flavorDimensions "default"

    productFlavors {

        demo {
            applicationId "test.demo"
            versionCode 2
            versionName "1.1.2"
            resValue "string", "backage_name_file", "test.demo.fileprovider"
            resValue "string", "bc", "com.demo"
            resValue "string", "bc_e", "extra_data.com.demo"
            resValue "string", "default_hostname", "demo.test.com"
            resValue "string", "default_username", "demo"
            resValue "string", "default_password", "demo"
        }
         AppOne {
            applicationId "test.AppOne"
            versionCode 2
            versionName "1.1.2"
            resValue "string", "backage_name_file", "test.AppOne.fileprovider"
            resValue "string", "bc", "com.AppOne"
            resValue "string", "bc_e", "extra_data.com.AppOne "
            resValue "string", "default_hostname", "AppOne.test.com"
            resValue "string", "default_username", "AppOne"
            resValue "string", "default_password", "AppOne"
        }
          AppTwo {
            applicationId "test.AppTwo"
            versionCode 2
            versionName "1.1.2"
            resValue "string", "backage_name_file", "test.AppTwo.fileprovider"
            resValue "string", "bc", "com.AppTwo"
            resValue "string", "bc_e", "extra_data.com.AppTwo"
            resValue "string", "default_hostname", "demoAppTwotest.com"
            resValue "string", "default_username", "AppTwo"
            resValue "string", "default_password", "AppTwo"
        }
}

Upvotes: 4

Views: 467

Answers (2)

OmerCohen1994
OmerCohen1994

Reputation: 184

You can merge resources from different flavours using sourceSets command. SourceSet Allows you to congifure buildVariants resources folders for example you can configure your App2 flavour to include App2 res folder and App1 res folder. Example code:

sourceSets {


        App2Debug{
            res.srcDirs = ['src/App1/res', 'src/App2/res']

        }

    }

Upvotes: 0

Jake Lee
Jake Lee

Reputation: 7979

You cannot build an APK with multiple flavours, just as you cannot build one in both debug and release buildTypes. The selected flavour's config / resources gets pulled into the actual APK metadata / manifest, so cannot be modified at runtime.

You will need to include all of your content inside the res folder, but there are a few ways that may help make it easier to manage. I'd recommend the first 3 options, and the 4th if you have a very large number of code + layout files with different behaviour:

  1. Use Fragments to avoid most of your Java / Kotlin code needing to be duplicated.
  2. Include XML layouts instead of redefining everything each time to reuse common elements.
  3. Carefully name your files, for example template1_background, template2_background.
  4. Use multiple modules, one for each "template". You'll then have multiple sensible res folders.

I can see why flavours might have seemed like the solution, but since you need all flavours in one app, this approach is unfortunately not going to work. You'll likely find step #2 will remove almost all of the duplicate files, avoiding the issue entirely!

Upvotes: 2

Related Questions