androidmanifestlocalytics

Localytics - Programmatically set app key


Is it possible to programmatically set the app key for Localytics? From the integration guide (https://support.localytics.com/Android_SDK_integration), it seems like you must set it in the Manifest file as meta-data.

<meta-data android:name="LOCALYTICS_APP_KEY" android:value="APP KEY FROM STEP 2"/>

From the following post, it also seems like it's impossible to dynamically set Android meta-data. How to add metadata dynamically (Not in manifest but inside code)?

I'd like to be able to set the app key dynamically based on Gradle buildType so I can have a release app key and a debug app key.


Solution

  • You can use manifest merging to support different app keys for your build types (e.g. debug versus release) or your product flavors (e.g. free versus paid).

    To support different app keys for your builds types:

    1. Create src/debug/AndroidManifest.xml and src/release/AndroidManifest.xml.
    2. Remove the meta-data tag from src/main/AndroidManifest.xml.
    3. Add the appropriate meta-data tag to your build type specific manifest.

    src/debug/AndroidManifest.xml

        <?xml version="1.0" encoding="utf-8"?>
        <manifest xmlns:android="http://schemas.android.com/apk/res/android"
            package="com.example.app" >
    
            <application>
    
                <meta-data
                    android:name="LOCALYTICS_APP_KEY"
                    android:value="DEBUG_APP_KEY" />
    
            </application>
    
        </manifest>
    

    src/release/AndroidManifest.xml

        <?xml version="1.0" encoding="utf-8"?>
        <manifest xmlns:android="http://schemas.android.com/apk/res/android"
            package="com.example.app" >
    
            <application>
    
                <meta-data
                    android:name="LOCALYTICS_APP_KEY"
                    android:value="RELEASE_APP_KEY" />
    
            </application>
    
        </manifest>
    

    For different app keys based on your product flavors, just replace debug and release above with your product flavor names.