Jetpack App Startup: keep your startup logic organized

栏目: IT技术 · 发布时间: 5年前

内容简介:I doubt that there's an app in production that does not initialize a number of objects.This usually takes place in theLately, Jetpack introduced a new library just for this called

I doubt that there's an app in production that does not initialize a number of objects.

This usually takes place in the Application class. You create a class that extends Application and you declare it in your manifest. Easy, right? When there's only a small number of things to initialize, this makes sense. Application is created only once and remains in context for the lifetime of your app. But what happens when you need to initialize a lot of things and comments such as // DO NOT CHANGE ORDERING! start appearing?

Lately, Jetpack introduced a new library just for this called App Startup . The concept is that you define an Initializer for each item you want to initialize and then define its dependencies (for the framework to figure out the ordering). Following this approach, your initialization logic will remain organized and in order, decoupled from the Application class.

Setup

Firstly, add this to your build.gradle .

implementation "androidx.startup:startup-runtime:1.0.0-alpha01"

Check out the official doc for the latest version.

Initializers

Now you need to define an initializer for each component you want to initialize. A common use case is for the dependency injection framework (e.g. Dagger,Koin) to be initialized during application startup. This can have its own Initializer with no dependencies.

class DependencyInjectionInitializer : Initializer<Unit> {

    override fun create(context: Context) {
        // Init logic for the D.I. framework you use
    }

    override fun dependencies(): List<Class<out Initializer<*>>> = emptyList()
}

Any other component that needs to be initialized, can declare DependencyInjectionInitializer as a dependency. This ensures that when something is requested from the D.I. framework, it will be available.

class BillingControllerInitializer : Initializer<Unit> {

    private val billingController by inject(BillingController::class.java)

    override fun create(context: Context) {
        // Init logic for the component that uses the D.I. framework
        billingController.connect()
    }

    override fun dependencies(): List<Class<out Initializer<*>>> =
        listOf(DependencyInjectionInitializer::class.java)
}

Declare the initializers

The last step for these Initializers to run automatically is to declare them in your AndroidManifest.xml .

<application>
[...]
    <provider
        android:name="androidx.startup.InitializationProvider"
        android:authorities="${applicationId}.androidx-startup"
        android:exported="false"
        tools:node="merge">
        <meta-data android:name="my.java.package.BillingControllerInitializer"
              android:value="androidx.startup" />
    </provider>
</application>
AndroidManifest.xml

Note that there is no need to declare all the Initializers . Only the "leafs" in the dependency graph. In this example, we only need to declare BillingControllerInitializer because it depends on DependencyInjectionInitializer . The App Startup library will find it and initialize it as well.

Manual initialization

The library provides an option for the Initializers not to run automatically. You can make a manual call when the initialization should happen. Further calls to the initialization method will return the same object (i.e. the library will keep a cache internally). Personally, I don't find this approach very useful, but if it fits your use case, consult the official doc .

Happy initializing!


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

计算机程序设计艺术(第1卷)

计算机程序设计艺术(第1卷)

[美] Donald E. Knuth / 清华大学出版社 / 2002-9 / 80.00元

第1卷首先介绍编程的基本概念和技术,然后详细讲解信息结构方面的内容,包括信息在计算机内部的表示方法、数据元素之间的结构关系,以及有效的信息处理方法。此外,书中还描述了编程在模拟、数值方法、符号计算、软件与系统设计等方面的初级应用。此第3版增加了数十项简单但重要的算法和技术,并根据当前研究发展趋势在数学预备知识方面做了大量修改。一起来看看 《计算机程序设计艺术(第1卷)》 这本书的介绍吧!

URL 编码/解码
URL 编码/解码

URL 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换