Multiple apps in one Android Studio project

JavaAndroidAndroid Studio

Please contribute by voting. Thanks!
4

Let's say that you'd like to create a few apps using the same (maybe with a few differences) code base. This is actually pretty easy in Android Studio. By using Build Flavors, you can host several flavors of your app in the same Android Studio project.

Here's one example of this:

  • App1: Show nearby bird sightings spots for Ornithologists on a map
  • App2: Show nearby archaeological sites for Archaeologists on a map

Their features are practically identical:

  • Fetch points of interests from an API
  • Show the points of interests on a map

But as the audiences are completely different, it wouldn't make sense to have the functionality of both apps in one.

This is how we can host both these apps in the same Android Studio project using build flavors:

1) Create new app

Start by creating a new app in Android Studio

Give it a Package Name and Application Name that generically describes the functionality, such as

  • Package Name: com.example.nearbyapp
  • Application Name: NearbyApp

2) Add build flavors

Open the build.gradle file for your app module and add a flavor-section inside the android-element and above the buildTypes-section. The flavor section should contain an item per app to create. In our case we add two apps: birdmap and archaeologicalmap. We also set some basic configuration for each flavor, such as their unique package name. The productFlavors-section will look something like this:

productFlavors {
    birdmap {
        applicationId "com.example.birdmap"
        versionNameSuffix "-birdmap"
    }
    archaeologicalmap {
        applicationId "com.example.archaeologicalmap"
        versionNameSuffix "-archaeologicalmap"
    }
}

3) Add flavor dependent variables

We might need to make decisions in the code based on what flavor we're running (e.g. fetch map data from different API endpoints). In order to do so we need to access some kind of variable in the code that tells us what flavor we're running.

You do this by adding buildConfigFields for each flavor in the buildTypes-section. For this to work, it's important that the buildTypes-section are defined below the flavor-section. The buildTypes-section will look something like this:

buildTypes {
    debug {
        productFlavors.birdmap.buildConfigField "String", "flavor", "\"birdmap\""
        productFlavors.archaeologicalmap.buildConfigField "String", "flavor", "\"archaeologicalmap\""
    }
    release {
        productFlavors.birdmap.buildConfigField "String", "flavor", "\"birdmap\""
        productFlavors.archaeologicalmap.buildConfigField "String", "flavor", "\"archaeologicalmap\""

        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

Make sure to sync your gradle-file before you proceed to the next step.

4) Read flavor dependent variables

The variable defined above will be available in your code through the static class BuildConfig. You can use them like this:

String flavor = BuildConfig.flavor;

5) Flavor dependent resources

We must also adapt the application resources (such as icons, texts and layouts) in order for this to work. Build flavors can have its own resource files that will override (if exists) the default ones. To add a flavor based resource file you right click the res-folder and select appropriate type to add. In the next step you select your flavor from the Source set dropdown.

This can also be done with source files.

6) Select flavor to execute

In order to try this out, we need to select our build flavor first.

There is a vertical tab bar on the left hand side of Android Studio. In the bottom of this bar is a tab called "Build Variants", click this tab and the "Build Variants" pane will appear. Select the build variant that you want to execute from the dropdown in the "Build Variants" column.

7) Package and release

You will need to build and package each flavor, just select the correct flavor when you Generate Signed APK.

Article created: Jul 10 at 16:14. Edited Jul 10 at 17:10.

1 Comment

2
Robert Bengtsson [114]  •  Jul 10 at 17:10  •   •  Reply

Thanks for a great article!

Your comment

You need to sign up / log in to comment this article

Author

Created by David Morales [4] Jul 10 at 16:14

Share article

Do you know about

Markdown?

Write an article