Ritt
Ritt

Reputation: 3349

Can Android SDK (library) have an application class?

I am writing an android library (SDK) which will be consumed by my main application.

The main application has its own application class where all the basic initialization happens like (Dagger and Stetho and other libraries).

My SDK has its own base application class, where application level components are initialized like Dagger 2.10+ (where application class implements HasActivityInjector) and Stetho, Timber, LeakCanary and few other application-level initializations.

Is it possible to have two application classes in android - I guess no, as a single process is created for per app.

I was wondering what would be the best/recommended way to incorporate SDK Application Class in the main application class.

Any help/suggestions on the same would be great!!

Upvotes: 7

Views: 2445

Answers (2)

Alireza Nezami
Alireza Nezami

Reputation: 715

You can't extend a class from Application in your sdk(s) but You can build a class for initializing your sdk and get the app context from there and use it for everything you wanted or init other dependencies like dagger or everything that requires to initialized in app class.

public class App extends Application {

    public static volatile Context applicationContext;

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

        Raychat.initialize(this,"sdgkdnsfjksdnzksdfmkofcn");
    }
}

public abstract class Raychat {

    @Nullable
    private static Raychat instance;


    public static synchronized Raychat client() {
        if (instance == null) {
            throw new IllegalStateException("Please call Raychat.initialize() before requesting client");
        } else
            return instance;
    }

    public static synchronized void initialize(Application application, String apiKey) {
        if (instance != null) {
            Log.i(TAG, "Raychat has been initialized");
        } else {
            instance = RealRaychat.create(application, apiKey);
            onCreate();
        }
    }

    private static void onCreate() {
        EmojiManager.install(new IosEmojiProvider());
        // other dependencies that you want to init on your app boot
    }
}

Upvotes: 1

peshkira
peshkira

Reputation: 6229

It's not possible to have two Application classes in an Android App, as the system guarantees that the Application is a singleton. So the use of your library has to either use your class or their own or extend from the one provided by Android.

That being said, you can do a few things.

You could provide an Application class from which the user of your library has to extend. This class can be abstract or not depending on your design. Note, however, that this alone is not perfect by design, because it could limit the users of your library as they might have to use their own class or might want to use another library that enforces them to extend another Application class.

Therefore it is considered a good practice to allow both. Provide an Application class that you could just extend and everything works, or provide a different mechanism, that is initialised in a custom class (within the app) and your library works with that.

That is actually, exactly what dagger does. You could either extend from DaggerApplication or you have to implement the HasActivityInjector and inject the DispatchingAndroidInjector by yourself and implement a method that dagger can use to obtain the Injector.

Another way would be to create your own Singleton instance. Some libraries utilise this and let the user of your library to configure some stuff by initialising the singleton (for example in their Apps onCreate method) and then you can access it.

Chances are you have already used such libraries, e.g. Fabric/Crashlytics.

Note, that this could be tricky depending on your requirements, as the App could get killed and the user of the library has to always make sure, they configure the singleton instance properly when it is woken up (e.g. by a push notification).

Upvotes: 3

Related Questions