简体   繁体   English

Android SDK(库)可以有应用程序类吗?

[英]Can Android SDK (library) have an application class?

I am writing an android library (SDK) which will be consumed by my main application.我正在编写一个将由我的主应用程序使用的 android 库 (SDK)。

The main application has its own application class where all the basic initialization happens like (Dagger and Stetho and other libraries).主应用程序有自己的应用程序类,所有基本初始化都在其中发生,如(Dagger 和 Stetho 以及其他库)。

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.我的 SDK 有自己的基础应用程序类,其中应用程序级组件被初始化,如 Dagger 2.10+(应用程序类实现 HasActivityInjector)和 Stetho、Timber、LeakCanary 和其他一些应用程序级初始化。

Is it possible to have two application classes in android - I guess no, as a single process is created for per app. android 中是否有可能有两个应用程序类 - 我猜不是,因为每个应用程序都创建了一个进程。

I was wondering what would be the best/recommended way to incorporate SDK Application Class in the main application class.我想知道在主应用程序类中合并 SDK 应用程序类的最佳/推荐方法是什么。

Any help/suggestions on the same would be great!!任何相同的帮助/建议都会很棒!!

It's not possible to have two Application classes in an Android App, as the system guarantees that the Application is a singleton.在一个 Android 应用程序中不可能有两个应用程序类,因为系统保证应用程序是一个单例。 So the use of your library has to either use your class or their own or extend from the one provided by Android.因此,您的库的使用必须使用您的类或他们自己的类,或者从 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.您可以提供一个 Application 类,您的库的用户必须从中扩展。 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.但是请注意,仅此一点在设计上并不完美,因为它可能会限制您的库的用户,因为他们可能不得不使用自己的类,或者可能想要使用强制他们扩展另一个 Application 类的另一个库。

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.提供一个 Application 类,您可以对其进行扩展并且一切正常,或者提供一种不同的机制,该机制在自定义类(在应用程序内)中初始化并且您的库可以使用它。

That is actually, exactly what dagger does.实际上,这正是 Dagger 的作用。 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.您可以从 DaggerApplication 扩展,或者您必须实现 HasActivityInjector 并自己注入 DispatchingAndroidInjector 并实现一个 dagger 可以用来获取 Injector 的方法。

Another way would be to create your own Singleton instance.另一种方法是创建自己的 Singleton 实例。 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.一些库利用这一点,让您的库的用户通过初始化单例(例如在他们的 Apps onCreate 方法中)来配置一些东西,然后您可以访问它。

Chances are you have already used such libraries, eg Fabric/Crashlytics.您可能已经使用过此类库,例如 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 (eg by a push notification).请注意,根据您的要求,这可能会很棘手,因为应用程序可能会被杀死并且库的用户必须始终确保他们在唤醒时正确配置单例实例(例如通过推送通知)。

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.您不能从 sdk 中的 Application 扩展类,但是您可以构建一个类来初始化您的 sdk 并从那里获取应用程序上下文并将其用于您想要的所有内容或初始化其他依赖项,例如 dagger 或需要的所有内容在应用程序类中初始化。

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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Robolectric Library类支持。 如何从应用程序项目加载库类R引用 - Android Robolectric Library class support. How to have library class R references load from application project android 库能否拥有自己的唯一应用程序 ID? - Can an android library have its own unique application ID? eclipse无法为Android应用程序导入库内部类 - eclipse can't import library inner class for android application Android sdk 管理器我没有 Android 支持库 - Android sdk manager I don't have Android Support Library sagepay是否有适用于iOS和/或Android的应用内付款的库或SDK? - Does sagepay have any library or SDK for in app payment for iOS and/or Android? 库类(来自Android sdk)依赖于程序类(再次,Android sdk) - Library class (from Android sdk) depends on program class (again, Android sdk) 类扩展Android项目库中的应用程序? - Class extending Application in Android project library? Android:调用库项目的应用程序类 - Android: calling the application class of the library project 带有camera2库的Android应用程序在SDK19启动时崩溃 - Android application with camera2 library crash on start for SDK19 是否可以将依赖于Android SDK的Android库发布为JAR? - Can I publish a library for Android with dependecies on Android SDK as JAR?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM