Android MVVM usage document

Time:2022-5-10

Project dependencies

Android MVVM usage document

7JY62U9%8${5$%57Y_H$WAN.png

Association relationship

1. The application layer is the host of the project
2. The component layer is the business component in the project
3. The base layer is some public lib packages

Host dependent business components

dependencies {
   //Determine whether the component needs to run independently in the host project
    if (isBuildModule.toBoolean()) {
        //When components run independently, the host project directly depends on the basic library to avoid compilation errors
        implementation project(':library-base')
    } else {
        //Main business module
        implementation project(':module-main')
        //Login module
        implementation project(':module-sign')
        //Home page module
        implementation project(':module-home')
        //User module
        implementation project(':module-user')
        //My module
        implementation project(':module-mine')
    }
}

Business components depend on Library base

dependencies {
   //Component dependent base library
    api project(':library-base')
    //Component relies on Ali routing compilation framework
    annotationProcessor rootProject.ext.dependencies["arouter_compiler"]
    //Rely on third-party components on demand
}

Library base relies on public libraries

dependencies {
    //Public support related libraries depend on in the base
    api rootProject.ext.support["design"]
    api rootProject.ext.support["appcompat-v7"]
    api rootProject.ext.support["constraint-layout"]
    api rootProject.ext.support["recyclerview-v7"]
    //Public resources are dependent in the base
    api project(':library-soure')
    api project(':network_processor')
    api project(':library-dialog')
    api project(':third-party-pay-login-share')
    //Core basic library (since this is an example, remote dependency is adopted here, which can be replaced by source dependency according to the actual situation. Source address: https://github.com/goldze/MVVMHabit )
    api rootProject.ext.dependencies["MVVMHabit"]
    //Ali routing framework
    api rootProject.ext.dependencies["arouter_api"]
    api rootProject.ext.dependencies["lifecycle-extensions"]
    api rootProject.ext.dependencies["lifecycle-compiler"]
    api rootProject.ext.dependencies["immersionbar"]
    api rootProject.ext.dependencies["alibabajson"]
    api rootProject.ext.dependencies["xxpermissions"]
    api rootProject.ext.dependencies["wechat"]
    //Other public libraries, such as image selection, sharing, push, etc
}

Open databinding

Android MVVM mode is inseparable from databinding. Each component needs to be enabled, including the host app

android {
    //Open databinding
    dataBinding {
        enabled true
    }
}

Manifest configuration

1. The component is in its own androidmanifest XML respectively configures the activity, and the application tag does not need to add attributes or specify the intent filter of the activity. When merging and packaging, gradle will merge the androidmanifest of each component into the host app.
When a component is associated to a host, androidmanifest XML example:
When the program of our program needs to jump to the mainactivity in the component module main, it is in the androidmanifest of the component module main Declaration in XML

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.szy.pinhole.main">

    <application>
        <!-- Register activity in your own component -- >
        <activity
            android:name=".activity.MainActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:exported="true"
            android:launchMode="singleTask"
            android:screenOrientation="portrait" />
        <service
            android:name=".service.ReportRecordDataService"
            android:enabled="true"
            android:exported="false" />
    </application>
</manifest>

2. When the component runs independently, you need a separate androidmanifest XML as a debugging tool. You can create an alone / androidmanifest. Exe in the Src / main folder xml。 Configure the application tag property and specify the activity to start. This is similar to the Android manifest of the host app The XML configuration structure is the same

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.goldze.main">
    <application
        ...
        >
        <activity 
            ...
            >
            <intent-filter>
                ...
            </intent-filter>
        </activity>
    </application>
</manifest>

build. Configuration in gradle

android {
    sourceSets {
        main {
            ...
            if (isBuildModule.toBoolean()) {
                //Independent operation
                manifest.srcFile 'src/main/alone/AndroidManifest.xml'
            } else {
                //Merge to host
                manifest.srcFile 'src/main/AndroidManifest.xml'
                resources {
                    //In the official version, all debugging files in the alone folder are excluded
                    exclude 'src/main/alone/*'
                }
            }
        }
    }
}

Jump and value transfer between activities

1. Add the annotation @ route in the activity (path = routeractivitypath. Scan. Page_scan_wifi_result)
2. Execute routeractivitypath through Ali's arouter call Scan. PAGER_ SCAN_ WIFI_ RESULT
//Example code:

ARouter.getInstance().build(RouterActivityPath.Scan.PAGER_SCAN_WIFI_RESULT)
                    .withParcelableArrayList("ipList", ipList).navigation();

Activity receive value

1. The oncreate method needs to be added again
 ARouter.getInstance().inject(this);
2. Get the value by declaring the get property
@Autowired(name = "ipList")
ArrayList<ScanDeviceEntity> ipList;

Arouter passes an ID of type int, which is the same as that of type string

ARouter.getInstance().build(RouterActivityPath.Strategy.PAGER_ARTICLE_ACTIVITY)
                                .withInt("id", videoEntity.getId()).navigation();

Activity receives an ID value of type int, and the same is true for type string

1. The oncreate method in the activity that needs to receive the value is added
ARouter.getInstance().inject(this);

@Autowired()
int id;