Why open source the SPI project?

In the process of project componentization, Application life cycle componentization has been realized by the way of Andoridmanifest.xml registration, similar to the mechanism used by Glide parsing andoridmanifest.xml to discover GlideModule. It’s a little tedious. Then I learned that Google’s AutoService, with annotations and a Java native SPI, componentized the Appcation life cycle just right. SPI can be no, no SPI, you can use other solutions, like routing. First of all, general routing basically provides the functions that routing should provide. The mechanism of route discovery and registration is limited within the routing framework and does not provide SPI. If implemented by routing, every component that requires the Application life cycle needs to be added into the Application by adding code, which is not easy to maintain. SPI can also be everywhere. As the project becomes componentalized, it is found that SPI mechanism is needed in many places, including AutoService plus native SPI mechanism. Besides reflection and IO, it is time-consuming when there are many interfaces. The SPI mechanism follows the open and closed principle, which also leaves an indescribable impression in my mind…… So slowly, this SPI project came into being.

Introduced in the projectSPI

Add the spI-gradle-plugin plugin to your project


buildscript {
  repositories {
    jcenter()
  }

  dependencies {
    classpath 'com. Afirez. Spi: spi - gradle - plugin: 1.0.1'/ /}}in module build.gradle
apply plugin: 'com.android.application'
apply plugin: 'spi'
// or apply plugin: 'com.afirez.spi'
Copy the code

Add spi to desired submodules


implementation "Com. Afirez. Spi: spi: 1.0.1." "
Copy the code

How to use SPI

1. Start with SPI AppLike

Implement the AppLike interface and add the @spi annotation. The implementer App will be discovered and registered with ExtensionLoader at compile time, as shown below:

The AppDelegate can then use the ExtensionLoader to get all of the AppLike implementors and distribute the Application lifecycle. As shown below:

Finally, remember to call the corresponding AppDelegate methods (attachBaseContext, onCreate, onTerminate, etc.) in your project’s App so that other modules have an Application lifecycle.

2, SPI Actiivty

Activities annotated by @spi are discovered at compile time and registered with ExtensionLoader, which can then fetch the Activity class at the specified path. (If @spi does not have a value for path, path will be the package name of the annotated class plus the class name.)

3, SPI fragments

A Fragment annotated by @spi is discovered at compile time and registered with ExtensionLoader, which can then fetch the Fragment class at the specified path. (If @spi does not have a value for path, path will be the package name of the annotated class plus the class name.)

4, SPI Provider

SpiKotlinProviderImpl, annotated by @spi, is also discovered at compile time and registered with ExtensionLoader, You can then get the SpiKotlinProvider implementation class for the specified path through ExtensionLoader. (If @spi does not have a value for path, path will be the package name of the annotated class plus the class name.)

5. Do more with SPI

SPI is a convenient interface aggregation and distribution tool, many interface extension scenarios need to be used, let our code follow the open closed principle! For example, AppLike implements componentization of the Application life cycle. For details, see use cases such as AppLike.

SPI already has a routing table, which can specify a path for the implementation classes of interfaces or classes (classes that support activities and fragments), so that we can get the implementation classes of the specified path. If you want to implement componentized routing yourself, you can do it based on SPI.

SPI focuses on interface discovery and registration and routing table, and only realizes Android componentalization based on SPI. Module decoupling is very thorough, but it lacks some functions of routing. In the future, we will consider opening an open source routing project, please look forward to it!

Go to SPI Github to download the source code and try it out for more details.

Thank you

  • ASM
  • hunter
  • KnightTransform