A solution to keep fabric API key from source code preserving CI build & minimizing integration issues
- Create a
buildTypes
namedproduction
in your applicationbuild.gradle
& disable crashlytics to otherBuildType
:
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