Building Different flavors of a Flutter app with different Application IDs
Asked Answered
S

1

6

I have some questions about how to do this correctly - Documentation from the Flutter team on using Flavors is somewhat lacking and to this date the tutorials and articles all seem to be from people who are posting as they learn about it. I appreciate the effort here but also recognise that the knowledge is incomplete.

The application ID can be set in a number of places, in particular in the AndroidManifest and in the build.gradle files.

Example:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.domain.app">
    <!-- io.flutter.app.FlutterApplication is an android.app.Application that
         calls FlutterMain.startInitialization(this); in its onCreate method.
         In most cases you can leave this as-is, but you if you want to provide
         additional functionality it is fine to subclass or reimplement
         FlutterApplication and put your custom class here. -->
    <uses-permission android:name="android.permission.INTERNET"/>

Example:

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.domain.appname"
        minSdkVersion 23
        targetSdkVersion 29
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    flavorDimensions "appFlavor"
    productFlavors {
        qa {
            dimension "appFlavor"
            applicationId "com.domain.appflavor1"
        }
        prod {
            dimension "appFlavor"
            applicationId "com.domain.appflavor2"
        }
        dev {
            dimension "appFlavor"
            applicationId "com.domain.appflavor3"
        }
    }

The application ID also appears in the Kotlin MainActivity.kt file, as well as in the path to this file under the src directory. As far as I can tell the value specified here does not have any impact on the app functionality. But surely it must have some relevance somewhere.

The values set in the respective config sets for the individual flavors in build.gradle works as expeced.

But when you have multiple AndroidManifest.xml files - eg one per flavor, these all have to specify the exact same package as the one in the main AndroidManifest.xml file. Attempts to leave the value out leads to an error. The value in the main AndroidManifest does not seem to matter as long as it matches the name in the flavor specific AndroidManifest

But this makes no sense - how can the package name and applicationID be different and conflict between the app build.gradle and the AndroidManifest?

Which one will be used for what exactly?

I have another question open on S.E where I'm battling with issues relating to MissingPlugin exceptions which I feel may be related here: Flutter issues with release-mode only APK builds

Stereotype answered 6/9, 2020 at 20:0 Comment(2)
Are you sure you have to add an Android Manifest for each flavor ? On a project with flavor I worked on, there was just the android manifests for each debug, profile and release..Mcgill
@MathisFouques Yes I use them to provide a unique name and icon to each flavor. If eliminating the AndroidManifestfiles will be beneficial I can probably live without the icons and names until I find another solution.Stereotype
C
0

For each flavor inside the productFlavors block, you can redefine the applicationId property, or you can instead append a segment to the default application ID using applicationIdSuffix

https://developer.android.com/studio/build/application-id

Cronus answered 7/10, 2020 at 14:52 Comment(1)
But build.gradle is specific to android only.Stereotype

© 2022 - 2024 — McMap. All rights reserved.