All Projects → idisfkj → Android Startup

idisfkj / Android Startup

Licence: apache-2.0
🔥The Android Startup library provides a straightforward, performant way to initialize components at the application startup. Both library developers and app developers can use Android Startup to streamline startup sequences and explicitly set the order of initialization.

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to Android Startup

Lightio
LightIO is a userland implemented green thread library for ruby
Stars: ✭ 165 (-74.02%)
Mutual labels:  async, thread
Swiftcoroutine
Swift coroutines for iOS, macOS and Linux.
Stars: ✭ 690 (+8.66%)
Mutual labels:  async, thread
Taskmanager
A simple、 light(only two file)、fast 、powerful 、easy to use 、easy to extend 、 Android Library To Manager your AsyncTask/Thread/CallBack Jobqueue ! 一个超级简单,易用,轻量级,快速的异步任务管理器,类似于AsyncTask,但是比AsyncTask更好用,更易控制,从此不再写Thread ! ^_^
Stars: ✭ 25 (-96.06%)
Mutual labels:  async, thread
Gear Lib
Gear-Lib, C library for IOT Embedded Multimedia and Network
Stars: ✭ 2,381 (+274.96%)
Mutual labels:  async, thread
Runtimepermission
Simpliest way to ask runtime permissions on Android, no need to extend class or override permissionResult method, choose your way : Kotlin / Coroutines / RxJava / Java7 / Java8
Stars: ✭ 860 (+35.43%)
Mutual labels:  manifest, async
Taskscheduler
A concise,practical async library for Android project,already was used in million devices
Stars: ✭ 385 (-39.37%)
Mutual labels:  async, thread
Transmittable Thread Local
📌 TransmittableThreadLocal (TTL), the missing Java™ std lib(simple & 0-dependency) for framework/middleware, provide an enhanced InheritableThreadLocal that transmits values between threads even using thread pooling components.
Stars: ✭ 4,678 (+636.69%)
Mutual labels:  async, thread
Context Switch
Comparison of Rust async and Linux thread context switch time.
Stars: ✭ 483 (-23.94%)
Mutual labels:  async, thread
Sockets
🔌 Non-blocking TCP socket layer, with event-driven server and client.
Stars: ✭ 559 (-11.97%)
Mutual labels:  async
Aredis
redis client for Python asyncio (has support for redis server, sentinel and cluster)
Stars: ✭ 576 (-9.29%)
Mutual labels:  async
Http Client
Async HTTP/1.1+2 client for PHP based on Amp.
Stars: ✭ 553 (-12.91%)
Mutual labels:  async
Tornado Celery
Non-blocking Celery client for Tornado
Stars: ✭ 561 (-11.65%)
Mutual labels:  async
Vs Threading
The Microsoft.VisualStudio.Threading is a xplat library that provides many threading and synchronization primitives used in Visual Studio and other applications.
Stars: ✭ 585 (-7.87%)
Mutual labels:  async
Maya
Maye 一个简洁小巧的快速启动工具
Stars: ✭ 553 (-12.91%)
Mutual labels:  startup
Usockets
Miniscule cross-platform eventing, networking & crypto for async applications
Stars: ✭ 611 (-3.78%)
Mutual labels:  async
Javatutorial
Java教程,包括多线程,泛型,反射,IO,容器类,注解
Stars: ✭ 552 (-13.07%)
Mutual labels:  thread
Fern.vim
🌿 General purpose asynchronous tree viewer written in Pure Vim script
Stars: ✭ 552 (-13.07%)
Mutual labels:  async
Micro Router
🚉 A tiny and functional router for Zeit's Micro
Stars: ✭ 621 (-2.2%)
Mutual labels:  async
Yappi
Yet Another Python Profiler, but this time thread&coroutine&greenlet aware.
Stars: ✭ 595 (-6.3%)
Mutual labels:  thread
Heim
Cross-platform async library for system information fetching 🦀
Stars: ✭ 572 (-9.92%)
Mutual labels:  async

English|中文

android-startup

Author Platform API Language Release Code Size License

The android-startup library provides a straightforward, performant way to initialize components at application startup. Both library developers and app developers can use android-startup to streamline startup sequences and explicitly set the order of initialization.

At the same time, the android-startup support async await and sync await. And topological ordering is used to ensure the initialization order of dependent components.

Here is a piece of with Google App Startup feature comparison table.

indicator App Startup Android Startup
Manually Config
Automatic Config
Support Dependencies
Handle Circle
Thread Of Control
Async Await
Callback Dependencies
Manual Notify
Topology Optimization
Time Cost Statistics
Thread Priority
Multiple Processes

Open source is not easy, I hope friends shake hands, a star in the upper right corner, thank you🙏

Related Articles

Why I abandoned the Jetpack App Startup?

Android Startup Analysis

Setup

Add the following dependency to your build.gradle file:

dependencies {
    implementation 'com.rousetime.android:android-startup:1.0.6'
}

Versions update information: Release

Quick Usage

There are tow ways of using android-startup in your project,need to be initialized before using android-startup.

Define Initialize components

You define each component initializer by creating a class that implements the AndroidStartup abstract. This abstract implements the Startup<T> interface. And this abstract defines four important methods:

  • The callCreateOnMainThread(): Booleanmethod,which control the create()method is in the main thread calls.Othrewise in the other thread.

  • The waitOnMainThread(): Booleanmethod,which control the current component should call waiting in the main thread.If returns true, will block the main thread.

  • The create(): T?method,which contains all of the necessary operations to initialize the component and returns an instance of T

  • The dependencies(): List<Class<out Startup<*>>>?method,which returns a list of the other Startup<*> objects that the initializer depends on.

For example, Define a SampleFirstStartup class that implements AndroidStartup<String>:

class SampleFirstStartup : AndroidStartup<String>() {

    override fun callCreateOnMainThread(): Boolean = true

    override fun waitOnMainThread(): Boolean = false

    override fun create(context: Context): String? {
        // todo something
        return this.javaClass.simpleName
    }

    override fun dependencies(): List<Class<out Startup<*>>>? {
        return null
    }

}

The dependencies() method returns an null list because SampleFirstStartup does not depend on any other libraries.

Suppose that your app also depends on a library called SampleSecondStartup, which in turn depends on SampleFirstStartup. This dependency means that you need to make sure that Android Startup initializes SampleFirstStartup first.

class SampleSecondStartup : AndroidStartup<Boolean>() {

    override fun callCreateOnMainThread(): Boolean = false

    override fun waitOnMainThread(): Boolean = true

    override fun create(context: Context): Boolean {
        // Simulation execution time.
        Thread.sleep(5000)
        return true
    }

    override fun dependencies(): List<Class<out Startup<*>>>? {
        return listOf(SampleFirstStartup::class.java)
    }

}

Because you include SampleFirstStartup in the dependencies() method, Android Startup initializes SampleFirstStartup before SampleSecondStartup.

For example, you also define a SampleThirdStartup and a SampleFourthStartup

Automatic initialization in manifest

The first one is automatic initializes startup in manifest.

Android Startup includes a special content provider called StartupProvider that it uses to discover and call your component startup. In order for it to automatically identify, need in StartupProvider defined in the <meta-data> label.The name as defined by the component class, value values corresponding to the android.startup.

<provider
    android:name="com.rousetime.android_startup.provider.StartupProvider"
    android:authorities="${applicationId}.android_startup"
    android:exported="false">

    <meta-data
        android:name="com.rousetime.sample.startup.SampleFourthStartup"
        android:value="android.startup" />

</provider>

You don't need to add a <meta-data> entry for SampleFirstStartup, SampleSecondStartup and SampleThirdStartup, because them are a dependency of SampleFourthStartup. This means that if SampleFourthStartup is discoverable, then are also.

Manually initialization in application

The second one is manually initializes startup in application.

Consider again the example,to make sure Android Startup can initializes,you can use StartupManager.Builder() directly in order to manually initialize components.

For example, the following code calls StartupManager.Builder() and manually initializes them:

class SampleApplication : Application() {

    override fun onCreate() {
        super.onCreate()
        StartupManager.Builder()
            .addStartup(SampleFirstStartup())
            .addStartup(SampleSecondStartup())
            .addStartup(SampleThirdStartup())
            .addStartup(SampleFourthStartup())
            .build(this)
            .start()
            .await()
    }
}

You can check out the sample app for more code information.

Run the example code, the console will produce the log as follows:

  1. After the initialization sequence sorting optimization
*****/com.rousetime.sample D/StartupTrack: TopologySort result:
    |================================================================
    |         order          |    [1]
    |----------------------------------------------------------------
    |        Startup         |    SampleFirstStartup
    |----------------------------------------------------------------
    |   Dependencies size    |    0
    |----------------------------------------------------------------
    | callCreateOnMainThread |    true
    |----------------------------------------------------------------
    |    waitOnMainThread    |    false
    |================================================================
    |         order          |    [2]
    |----------------------------------------------------------------
    |        Startup         |    SampleSecondStartup
    |----------------------------------------------------------------
    |   Dependencies size    |    1
    |----------------------------------------------------------------
    | callCreateOnMainThread |    false
    |----------------------------------------------------------------
    |    waitOnMainThread    |    true
    |================================================================
    |         order          |    [3]
    |----------------------------------------------------------------
    |        Startup         |    SampleThirdStartup
    |----------------------------------------------------------------
    |   Dependencies size    |    2
    |----------------------------------------------------------------
    | callCreateOnMainThread |    false
    |----------------------------------------------------------------
    |    waitOnMainThread    |    false
    |================================================================
    |         order          |    [4]
    |----------------------------------------------------------------
    |        Startup         |    SampleFourthStartup
    |----------------------------------------------------------------
    |   Dependencies size    |    3
    |----------------------------------------------------------------
    | callCreateOnMainThread |    false
    |----------------------------------------------------------------
    |    waitOnMainThread    |    false
    |================================================================
  1. Consumed components initialization times
*****/com.rousetime.sample D/StartupTrack: startup cost times detail:
    |=================================================================
    |      Startup Name       |   SampleFirstStartup
    | ----------------------- | --------------------------------------
    |   Call On Main Thread   |   true
    | ----------------------- | --------------------------------------
    |   Wait On Main Thread   |   false
    | ----------------------- | --------------------------------------
    |       Cost Times        |   0 ms
    |=================================================================
    |      Startup Name       |   SampleSecondStartup
    | ----------------------- | --------------------------------------
    |   Call On Main Thread   |   false
    | ----------------------- | --------------------------------------
    |   Wait On Main Thread   |   true
    | ----------------------- | --------------------------------------
    |       Cost Times        |   5001 ms
    |=================================================================
    |      Startup Name       |   SampleThirdStartup
    | ----------------------- | --------------------------------------
    |   Call On Main Thread   |   false
    | ----------------------- | --------------------------------------
    |   Wait On Main Thread   |   false
    | ----------------------- | --------------------------------------
    |       Cost Times        |   3007 ms
    |=================================================================
    |      Startup Name       |   SampleFourthStartup
    | ----------------------- | --------------------------------------
    |   Call On Main Thread   |   false
    | ----------------------- | --------------------------------------
    |   Wait On Main Thread   |   false
    | ----------------------- | --------------------------------------
    |       Cost Times        |   102 ms
    |=================================================================
    | Total Main Thread Times |   5008 ms
    |=================================================================

More

Optional Config

  • LoggerLevel: control Android Startup log level, include LoggerLevel.NONE, LoggerLevel.ERROR and LoggerLevel.DEBUG.

  • AwaitTimeout: control Android Startup timeout of await on main thread.

  • StartupListener: Android Startup listener, all the component initialization completes the listener will be called.

config in manifest

To use these config, you must define a class than implements the StartupProviderConfig interface:

class SampleStartupProviderConfig : StartupProviderConfig {

    override fun getConfig(): StartupConfig =
        StartupConfig.Builder()
            .setLoggerLevel(LoggerLevel.DEBUG)
            .setAwaitTimeout(12000L)
            .setListener(object : StartupListener {
                override fun onCompleted(totalMainThreadCostTime: Long, costTimesModels: List<CostTimesModel>) {
                    // can to do cost time statistics.
                }
            })
            .build()
}

At the same time, you need add StartupProviderConfig to manifest file:

<provider
    android:name="com.rousetime.android_startup.provider.StartupProvider"
    android:authorities="${applicationId}.android_startup"
    android:exported="false">

    <meta-data
        android:name="com.rousetime.sample.startup.SampleStartupProviderConfig"
        android:value="android.startup.provider.config" />

</provider>

StartupProvider that it uses to discover and call SampleStartupProviderConfig.

config in application

To use these config,you need use StartupManager.Builder() in application.

override fun onCreate() {
    super.onCreate()

    val config = StartupConfig.Builder()
        .setLoggerLevel(LoggerLevel.DEBUG)
        .setAwaitTimeout(12000L)
        .setListener(object : StartupListener {
            override fun onCompleted(totalMainThreadCostTime: Long, costTimesModels: List<CostTimesModel>) {
                // can to do cost time statistics.
            }
        })
        .build()

    StartupManager.Builder()
        .setConfig(config)
        ...
        .build(this)
        .start()
        .await()
}

AndroidStartup

  • createExecutor(): Executor: If the startup not create on main thread, them the startup will run in the executor.

  • onDependenciesCompleted(startup: Startup<*>, result: Any?): This method is called whenever there is a dependency completion.

  • manualDispatch(): Boolean: Returns true that manual to dispatch. but must be call onDispatch(), in order to notify children that dependencies startup completed.

  • onDispatch(): Start to dispatch when manualDispatch() return true.

StartupCacheManager

  • hadInitialized(zClass: Class<out Startup<*>>): Check whether the corresponding component initialization has been completed.

  • obtainInitializedResult(zClass: Class<out Startup<*>>): T?: Obtain corresponding components of has been initialized the returned results.

  • remove(zClass: Class<out Startup<*>>): To get rid of the corresponding component initialization cache the results.

  • clear(): Remove all the component initialization cache the results.

Annotation

  • ThreadPriority: Set Startup to initialize thread priority.

  • MultipleProcess: The process on which Startup is initialized.

Sample

License

Please see LICENSE

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].