DEV Community

Pere Sola
Pere Sola

Posted on

Flutter - how to have variables in the Android Manifest file

If you want to put AdMob ads in your Android app, you will need to specify your AdMob app id in the Android manifest file. However, I am committing this file to source control, so I didn't want to hard code it in there. The Android manifest file is an .xml one and, as such, I had no idea how to even have variables in the first place. Luckily, I found this post, what follow is just a summary mostly for my future self on what to do for Android.

1.- In build.gradle, type the following somewhere above android::

def dartEnvironmentVariables = []

if (project.hasProperty('dart-defines')) {
    dartEnvironmentVariables ='dart-defines')
        .collectEntries { entry ->
            def pair = new String(entry.decodeBase64(), 'UTF-8').split('=')
            [(pair.first()): pair.last()]
Enter fullscreen mode Exit fullscreen mode

2.- In defaultConfig:, add the following:

manifestPlaceholders['APPLICATION_ID'] = dartEnvironmentVariables.APPLICATION_ID
Enter fullscreen mode Exit fullscreen mode

3.- In AndroidManifest.xml, you can now use the variable. In the case of AdMob it needs to be added as meta-data as so:

          android:value="${APPLICATION_ID}" />
Enter fullscreen mode Exit fullscreen mode

4.- That's it, remember to either flutter run or flutter build with the env variables, otherwise your app won't build correctly:

flutter run --dart-define APPLICATION_ID=1234qwerty --dart-define WHATEVER_OTHER_VARIABLE=9876poiuy


flutter build appbundle --dart-define APPLICATION_ID=1234qwerty --dart-define WHATEVER_OTHER_VARIABLE=9876poiuy

Top comments (0)