Skip to content

Instantly share code, notes, and snippets.

@bertrandmartel
Last active September 4, 2016 13:43
Show Gist options
  • Save bertrandmartel/044555230e4ea0bfc009782b90cbc01d to your computer and use it in GitHub Desktop.
Save bertrandmartel/044555230e4ea0bfc009782b90cbc01d to your computer and use it in GitHub Desktop.
A solution to keep fabric API key from source preserving CI build & minimize integration issues

Prevent Fabric key commitment (Android)

A solution to keep fabric API key from source code preserving CI build & minimizing integration issues

  • Create a buildTypes named production in your application build.gradle & disable crashlytics to other BuildType :
buildTypes {

	debug {
	    buildConfigField "boolean", "USE_CRASHLYTICS", "false"
	    ext.enableCrashlytics = false
	}

	release {
	    buildConfigField "boolean", "USE_CRASHLYTICS", "false"
	    ext.enableCrashlytics = false
	    minifyEnabled true
	    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
	}

	production {
	    buildConfigField "boolean", "USE_CRASHLYTICS", "true"
	    ext.enableCrashlytics = true
	    minifyEnabled true
	    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
	}
}
  • Add to your build.gradle :
def build_param = "${build}";

if (build_param != "production") {

    //exclude production build
    android.variantFilter { variant ->
        if (variant.buildType.name.equals('production')) {
            variant.setIgnore(true);
        }
    }
} else {
    //exclude all except production build
    android.variantFilter { variant ->
        if (!variant.buildType.name.equals('production')) {
            variant.setIgnore(true);
        }
    }
}
  • Copy paste your AndroidManifest.xml in a new folder app/src/production. Then remove the key from the original manifest.

You should have in app/src/main/AndroidManifest.xml :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.yourpackage.test">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity
            android:name=".activity.MainActivity"
            android:label="@string/app_name">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

And in app/src/production/AndroidManifest.xml :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.yourpackage.test">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity
            android:name=".activity.MainActivity"
            android:label="@string/app_name">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="io.fabric.ApiKey"
            android:value="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123" />
    </application>

</manifest>
  • In your .gitignore file add :
app/src/production
app/fabric.properties
  • In your code :
if (BuildConfig.USE_CRASHLYTICS) {
    Fabric.with(this, new Crashlytics(), new CrashlyticsNdk());
}

The following will build debug & release :

./gradlew build

To build your production build :

./gradlew -Pbuild=production build
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment