Android Startup, schedule your startup jobs
Introduction
AndroidStartup is an open source project used to refine your Andriod App startup. Compared with Jetpack Startup, this project can used for ASYNC circumstance. As we konw, in most cases, to accelerate the App startup, we may run our jobs in background threads. You are allow to use background and main thread jobs and specify their dependencies in AndroidStarup. The AndroidStartup could handle their relations and run jobs by their dependencies.
Setup
Add MavenCentral,
repositories { mavenCentral() }
Add the dependency to use startup in your project,
implementation "com.github.Shouheng88:startup:$latest-version"
If you want to use the @StartupJob
annotation to define the job, append the below dependenc,
kapt "com.github.Shouheng88:startup-compiler:$latest-version"
If you want just use the scheduler of startup, you can just use the scheduler by,
implementation "com.github.Shouheng88:scheduler:$latest-version"
Initialize at Android Startup
You have multiple ways to use AndroidStartup.
Implement ISchedulerJob to define your job
The ISchedulerJob
interface is used to define the job in scheduler. You have to implement its three methods,
threadMode()
to specify the thread job that the task will rundependencies()
dependeny jobs the current job relies onrun()
the business for the job
Set up manifest entries
Android Startup includes a special content provider called AndroidStartupProvider
that it uses to discover and call your scheduler jobs. Android Startup discovers jobs by first checking for a <meta-data>
entry under the AndroidStartupProvider
manifest entry.
<provider
android:authorities="${applicationId}.androidx-startup"
android:exported="false"
tools:node="merge"
android:name="me.shouheng.startup.AndroidStartupProvider">
<meta-data android:name="me.shouheng.startupsample.jobs.BlockingBackgroundJob"
android:value="android.startup" />
<meta-data android:name="me.shouheng.startupsample.jobs.CrashHelperInitializeJob"
android:value="android.startup" />
<meta-data android:name="me.shouheng.startupsample.jobs.DependentBlockingBackgroundJob"
android:value="android.startup" />
<meta-data android:name="me.shouheng.startupsample.jobs.ThirdPartLibrariesInitializeJob"
android:value="android.startup" />
</provider>
The tools:node="merge"
attribute ensures that the manifest merger tool properly resolves any conflicting entries.
Manually initialize jobs
The Android Startup also allows you to initialize Startup by directly calling AndroidStartup
builder. You can specify jobs by its jobs()
method and then call launch()
to start these jobs.
AndroidStartup.newInstance(this).jobs(
CrashHelperInitializeJob(),
ThirdPartLibrariesInitializeJob(),
DependentBlockingBackgroundJob(),
BlockingBackgroundJob()
).launch()
Use @StartupJob annotation to initialize jobs
You can alswo use @StartupJob
to define jobs and then call scanAnnotations()
method of AndroidStartup
to scan jobs with @StartupJob
annotation.
AndroidStartup.newInstance(this).scanAnnotations().launch()
To use the annotation driver you will have to add the kotlin-kapt
plugin and the startup-compiler.
License
Copyright (c) 2021 ShouhengWang.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.