Question

I am creating an Android app with a paid and free version. To do this I have created one shared library and then just changed some resource files in each project.

In the free version I want to add google analytics and i'm struggling to do this.

Google says I need to add this code to all the activities I want to track:

 @Override
  public void onStart() {
    super.onStart();
    ... // The rest of your onStart() code.
    EasyTracker.getInstance(this).activityStart(this);  // Add this method.
  }

  @Override
  public void onStop() {
    super.onStop();
    ... // The rest of your onStop() code.
    EasyTracker.getInstance(this).activityStop(this);  // Add this method.
  }

I want this code to be added just to the free version of all activities.

Is it possible to do this?

I was thinking in the library I could extend a class AnalyticsActivity which just extends Activity, and then in the free version I could override this class and add the required code.

public class MainActivity extends AnalyticsActivity { //rest of code }

Then this will be the class that is overridden in the free project:

public class AnalyticsActivity extends Activity{
    @Override
    public void onStart() {
        super.onStart();
        EasyTracker.getInstance(this).activityStart(this); // Add this method.
    }

    @Override
    public void onStop() {
        super.onStop();
        EasyTracker.getInstance(this).activityStop(this); // Add this method.
    }
}

Is this a possible solution and how can it be done, or is there a better solution?

Was it helpful?

Solution

I would create a stub EasyTracker class that implements the same API but does nothing (or perhaps logs output only in a debug build). That will require the fewest changes in the rest of your code.

You could include this stub class in flavors that don't import the Analytics library.

Your stub EasyTracker class would look something like this. I haven't actually compiled this code, so you'll need to figure out imports and fine-tune it (You'll have to work around the other analytics classes that it needs for imports; either mock them as well or try to include them from the Analytics library without picking up the real version of EasyTracker, though that could be tricky -- maybe help yourself out by only including stub versions of the method calls you actually use), but this should get you started:

package com.google.android.apps.analytics.easytracking;

public class EasyTracker {
    private static final EasyTracker INSTANCE = new EasyTracker();

    public static EasyTracker getTracker() {
        return INSTANCE;
    }

    void addItem(Item item) {}
    void addTransaction(Transaction transaction) {}
    void clearTransactions() {}
    void dispatch() {}
    // Stub out all the rest of the methods like this
}

Basically you implement all the methods in the EasyTracker class API, but they're all empty method bodies. Note that this class is in the same package as the real version that actually does something. This means that you can swap this stub class in for the original with no changes to the code that calls it.

You could either compile this class to a jar file and include the jar in the flavors that need it, or you could make it a plain Java module.

Then you need to selectively include the code in the flavors that need it. You probably already have dependencies that look something like this:

dependencies {
    ....
    compile files('libs/libGoogleAnalyticsV2.jar')
}

Make the dependency on the Analytics library dependent on the flavor:

dependencies {
    compile 'com.android.support:appcompat-v7:+'
    flavor1Compile files('libs/libGoogleAnalyticsV2.jar')
    flavor2Compile project(':MockAnalytics')
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top