0

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 Answers1

0

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.
}
Guillaume Perrot
  • 4,278
  • 3
  • 27
  • 37
  • I've tried to put the `compileOnly` option but I got the following error `Android dependency (...) is set to compileOnly/provided which is not supported`. After further digging it seems that my version of the Gradle plugin (3.3.2) doesn't support the `compileOnly`option. I've changed it to `api` and everything seemed to build. However, no exceptions were thrown when executing all flavors. Does that mean the SDK is now included on all flavors? Maybe it's just simpler to revert to the `implementation`tag and simply call the `Register` method on the desired flavors. Thanks for your quick reply. – Diogo Salgueiro Jun 06 '19 at 17:49
  • api is like implementation except it will also expose the dependency to other libraries. api is thus not different from implementation if put into an app and not a library. I would suggest updating gradle plugin so that you can use compileOnly. – Guillaume Perrot Jun 06 '19 at 21:03