Is it possible to include and initialize HockeyApp SDK only for specific flavors?

161 Views Asked by At

I have an Android application with several flavors, each one for a different environment. I wish to specify on which of these flavors the HockeyApp SDK is imported and initialized (or at least to be able to use it in all flavors except one).

Currently I have this line on the build.gradle file:

dependencies {
...
        implementation 'net.hockeyapp.android:HockeySDK:5.1.0'
...
}

and on the main Application.java file, the HockeyApp SDK is imported and initialized:

import net.hockeyapp.android.CrashManager;

@Override
public void onCreate(){
        super.onCreate();

        CrashManager.register(this);
...
}

The current workaround is to comment these lines each time i need to build and/or generate an .apk file for the flavor which I don't want to include the HockeyApp SDK. Surely there's a better way to handle this.

I've tried to create different Application.java files and place them inside app/src/{flavor} (following the same logic of different resources for different flavors), but I could not build the application because every reference for the Application class was "flavor independent" eg. each class that imported the Appplicationclass simply had the line:

import {package}.Application;

Thinking the other way around, if I specify on the build.gradle specific implementation for each flavors eg.

{flavor1}Implementation 'net.hockeyapp.android:HockeySDK:5.1.0'
{flavor2}Implementation 'net.hockeyapp.android:HockeySDK:5.1.0'

then I'm unable to build the other flavors because the package reference doesn't exist for them.

I hope I was able to explain my question. If you need further explanations or details about the current application's implementation, I'm happy to provide them.

Thanks in advance.

1

There are 1 best solutions below

2
Guillaume Perrot On

One solution that always works but not ideal is to use reflection when accessing HockeyApp APIs and catch exceptions.

Another solution is to add a compileOnly dependency that will be on all flavours but have the actual implementation dependency only on some flavours, then try/catch all HockeyApp calls with LinkageError in the catch clause so that the app does not crash when HockeyApp SDK missing at runtime.

Something like:

compileOnly 'net.hockeyapp.android:HockeySDK:5.1.0' // all flavours
{flavor1}Implementation 'net.hockeyapp.android:HockeySDK:5.1.0' // only flavor1 has HockeyApp available at runtime and in binaries.
try {
   CrashManager.register(this);
} catch (LinkageError e) {

   // HockeyApp not available in this flavor.
}