The previous article [Android Componentized Development Framework] analyzed the technical principles of building a componentized framework as a whole. A brief analysis of componentization is given.

Stitch is a framework that I organized by combining previous theories and perfecting them during the project practice. It has completed the basic functions of page routing, component life cycle and data routing. The source code of the framework also contains examples of script optimization that may be needed to build componentalization.

Rely on

Add the CLASspath dependency to the build.gradle file in the project root directory

buildscript {
    repositories {
        google()
        jcenter()// Stitch is published in this
    }
    dependencies {
        ...
        classpath 'bamboo.com ponents. Stitch, stitch - gradle - plugin: 1.0'}}Copy the code

Add the following to the build.gradle file for the module that needs stitch:

//apply plugin: 'com.android.application'
//apply plugin: 'com.android.library'
// Need to be placed after android plugin
apply plugin: 'stitch.plugin'
Copy the code

Do the first two steps and we’re ready to use Stitch.

@ Component, ComponentLife

The ComponentLife class is the Component lifecycle proxy base class, and we inject our Component into Stitch by inheriting the ComponentLife class and marking it with @Component.

Only one @Component annotation class is allowed per Module, and if there are more than one annotation class, only one will be valid.

Specific use method:

1. Custom ComponentLife subclasses

If the Module does not need to be initialized during the onCreate, attachBaseContext, and other life cycles of the Application, you do not need to customize the ComponentLife class.

@Component
public class TestComponetLife extends ComponentLife {

    /* * Proxy Application OnCreate method */
    public void onCreate(a){
        //TODO performs the required initialization
    }
    
    // The Application object can be obtained by TestComponetLife
    TestComponetLife  testComponent = StitcherHelper.searchComponentApplication(TestComponetLife.class);
    Application application = testComponent.getApplication();
}
Copy the code

2. Pass the Application lifecycle

In Application, we need to actively transmit the Application life cycle to Stitch, and Stitch provides two implementations:

StitcherApplication is inherited from Application, but stitch’s lifecycle call is added

2. Invoke the component lifecycle through StitcherHelper. Call StitcherHelper.oncreate (), attachBaseContext(), etc. Stitcherhelper.oncreate ()) For details, please refer to the implementation of StitcherApplication.

@ Exported, ActivityPage

The @exported annotation is used to open activities in a Module to other Modules to call.

ActivityPage is used to associate with open activities and implements the Serializable interface, so it can also be used as a wrapper class for Activity data delivery.

Before we can actually use it, we need to create a new common Module as a routing Module. Here we assume that all of our modules depend on the Module named Router, and we develop the Module named ModuleA and the other modules named ModuleB

1. Use

Each Activity that needs to be shared with other modules needs to be associated with an ActivityPage that allows other modules to interact with the Activity.

//1. Create testPage.java on the Router
public class TestPage extends ActivityPage{
    public final String text;
    public TestPage(Context context,String text) {
        super(context);
        this.text = text; }}//2. Start TestPage in ModuleB. The developers of ModuleB do not need to know which page TestPage corresponds to
StitcherHelper.start(new TestPage(context,"text test"));

//3. Create TestActivity in the ModuleA and associate it with TestPage
@Exported(TestPage.class)
public class TestActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState) {...// Receive the parameters passed when the page is started
       TestPage infoPage = (TestPage) getIntent().getSerializableExtra("TestPage"); mTestTextView.setText(infoPage.text); }}Copy the code

2. Set the Intent Flag

During page interactions, we sometimes need to set flags for intEnts. In this case, we can pass them through ActivityPage’s targetIntent. When launching TestActivity, Stitch clone all parameters of the targetIntent.

public void onActionTest(View view) {
    ActionTestPage page = new ActionTestPage(context);
    Intent targetIntent = new Intent();
    targetIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
    page.setTargetIntent(targetIntent);
    StitcherHelper.start(page);
}
Copy the code

3. Parameter transfer during the Activity jump

In page interaction, sometimes we need to pass parameters to the next page. As mentioned above, Serializable is also supported by Stitch: Parcelable

If TestPage implements the Parcelable interface, Stitch automatically switches to Parcelable for parameter passing.

// Default Serializable mode
public class TestPage extends ActivityPage {...// Parameters that do not want to be passed are described as transient
    public transient final int noParamer = 100;
}

// Parcelable, need to write their own serialization method
public class TestPage extends ActivityPage implements Parcelable {... }Copy the code

@Service

The @service annotation is used to inject the Module’s exposed implementation interface into Stitch.

// 1. Create itestService.java on the Router
public interface ITestService {
    String getTestText(a);
}

// 2. Create TestServiceImp. Java in Module and implement ITestService
@Service
public class TestServiceImp implements ITestService {
    public String getTestText(a) {
        return "hello world!"; }}// 3. Use the interface in ModuleB
public class TestServiceTest {
    public void test(a) {
        ITestService testService = StitcherHelper.searchService(ITestService.class);
        TestService is null if the component is not referenced (not packaged into apK)
        String testText =  testService == null ? "": testService.getTestText(); }}Copy the code

This concludes the use of Stitch.

Componentized script configuration see: Componentized Android: build.gradle configuration

See the Basic concept of componentization: Android Componentization Framework

The source code is at Github’s open source project, Stitch

Everyone is welcome to star or submit PR.