2

I am developing an app for which I want skin for different clients. (Header logo image, background image, text strings etc.)

I am thinking of either runtime reading in a configuration file or somehow switch it design-time or compile-time. If runtime, the app would read a config file on startup to configures itself. (Switches things on/off, graphics, texts etc.)

If runtime: Is there any way to ship .apk files with different .zip files (containing resources like images) and then uncompress .zip file in-memory and replace e.g. drawable + texts + SharedPreferences?

If designtime: Is there a way to easily switch between different sets of resources in Eclipse/ADT either compile-time or run-time? Like having different folders with different images, strings etc? I am still new to ADT/Eclipse, so I am unsure if there is an established way of doing what I desire?

1
  • 2
    Using external build tools Ant/Maven/Gradle can help you manage this at project build time, check out this Q&A for some ideas on this topic.
    – yorkw
    Apr 29, 2013 at 3:33

3 Answers 3

7

Look at "Build Variants" and "Product flavors" here

It can be done with the new Gradle build for Android. You can use the Gradle build from the command line or out from the new Android Studio (presented at Google IO 2013), see here

At the Google IO keynote the product flavors have been presented as a teaser, the session was this one, the video can be found here. Jump to 14:10 for the discussion of the build variants. A detailed presentation was The New Android SDK Build System.

In short you can have different source sets (directories) for different flavors, e.g. one common directory and one for each flavor as you mentioned above.

3
+50

No, you can't do that. Your best bet is a Android Library project, and another project for every "client / app. Please refer to the official Android docs for more information about Android Library projects: https://stackoverflow.com/a/1986071/198996

1
  • While this used to be correct, I would recommend the solution provided by @ChrLipp. May 19, 2013 at 14:14
2

Like ChrLipp said, on bleeding edge Android Studio, there is a convenient solution called "flavours".

It's based on mirrored directories hierarchy. A "main" directory hierarchy that contains all files of a "main" build, and "flavours" directories hierarchies, each flavor directory contains files that will overwrite or complement those "main" ones at build time.

For my use (I can't migrate to Android Studio), I wrote a simple ant script to imitate "flavours" feature. It works on Netbeans and Eclipse, and I think it's some sort of project-independent.

To use it, the full project folder have to step back one level of hierarchy, and the original one needs to be placed on "main" directory. A build directory must be created, and multiple "flavors" folders be put inside "flavors", as this:

├── build.xml                           < - ant script file above
├── main                                < - original project
├── flavors
│     └── freeVersion                   < - files related with a freeVersion 'skin'
└── build                               < - temporary build folder

Running the script ($ ant change-flavor), it will ask witch flavor directory you want to build. After user input, it checks flavor directories existence, and copy all main directory into build folder, plus eventual "flavors" files, overwriting the "main" ones.

The resultant build folder is a complete new native Android/Cordova/whatever project, that can be normally opened and compiled through the IDE.

<?xml version="1.0" encoding="UTF-8"?>

<project name="Flavors" basedir="." >

<property name="flavors.dir" value="flavors"/>
<property name="flavors.build.dir" value="build"/>
<property name="flavors.main.dir" value="main"/>

<target name="change-flavor">
    <input message="Which Flavor to build?" addproperty="flavor.dir" />

     <fail message="Empty flavor not allowed">
         <condition>
                 <equals arg1="${flavor.dir}" arg2=""/>
         </condition>
    </fail>     

    <fail message="Directory ${flavors.dir}/${flavor.dir} not exists">
        <condition>
            <not>
                <available file="${flavors.dir}/${flavor.dir}" type="dir" />
            </not>
        </condition>
    </fail>

    <echo message="Deleting build dir ${flavors.build.dir}"/>
    <delete includeemptydirs="true">
        <fileset dir="${flavors.build.dir}" includes="**/*"/>
    </delete>

    <echo message="Copying from main ${flavors.build.dir}"/>
    <copy todir="${flavors.build.dir}" includeemptydirs="true" >  
            <fileset dir="${flavors.main.dir}" includes="**"/>  
     </copy>        

    <echo message="Copying from flavor ${flavors.build.dir}"/>
    <copy todir="${flavors.build.dir}" includeemptydirs="true" overwrite="true" >
            <!-- exclude folder is here because my flavors directories are also netbeans
                 projects. If similar approach is used on eclipse, maybe put here .project 
                 and .settings folders -->
            <fileset dir="${flavors.dir}/${flavor.dir}" includes="**" excludes="nbproject/**"/>
     </copy>        

    </target>    
</project>

There is a penalty time for each flavor build, because there is no pre-compiled stuff, nor well integrated on main project build system, but in my case, I think its worth, since the only things that changes from a flavor to another are properties files and assets. Most of development process can be done on "main" project, flavors are just skins. Beside that, it avoids intromission on cordova own build system/netbeans integration.

1
  • 1
    But this doesn't create seperate package names for each flavor or am I missing something ? In need seperate package names so our sale guys can install a demo, and a real version at the same time on the same device. And so that we can host multiple versions in the app store.
    – Knarf
    Mar 18, 2016 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.