6

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.

Community
  • 1
  • 1
fobbymaster
  • 1,406
  • 3
  • 14
  • 22
  • 1
    Why localytics isn't adding a simple api for setting the app key ? – AsafK May 04 '15 at 09:14
  • 1
    +1. In my case the build-time solution mentioned below doesn't work because I don't know until runtime which Localytics app key I want to use. – Jo Jo Jul 17 '15 at 19:38

3 Answers3

5

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.

deRonbrown
  • 635
  • 7
  • 14
2

There is an override on Localytics.integrate that takes an api key.

Localytics.integrate(this, "API_KEY")
sirFunkenstine
  • 8,135
  • 6
  • 40
  • 57
0

If in case you are using autoIntegrate, use following API which takes Application context as first argument.

Localytics.autoIntegrate(this, "API_KEY");
AndyW
  • 1,431
  • 16
  • 22