Modular Learning (1)

When the project is large to a certain extent, the business of each module is coupled with each other, and it is very difficult to maintain. In addition, when the project is large to a certain extent, the compilation speed is also very slow, so it is necessary to modularize at this time. There are many explanations on the Internet, so I won't say more here.

Generally, after the project modules are divided, each module must be able to be debugged separately and switched as a dependency, so simple dynamic configuration is required.

Modules as application and library

My personal understanding is that the application can be started and run, but the library cannot run as a dependent library.
So in order to be able to dynamically configure, we need to have a variable to control. Add the following in gradle.properties
:

//来控制模块是否作为一个 Library
isLibrary = true

Then under build.gradle of each module except App:

if (isLibrary.toBoolean()) {
    //true 的时候作为一个库
    apply plugin: 'com.android.library'
} else {
//true 的时候作为一个程序入口
    apply plugin: 'com.android.application'
}

Note: In the case of .application and library, their manifest files are different. The manifest file as the program entry is to configure the activated activity, but it is not needed as a library, so it can be dynamically configured and loaded. manifest folder.
In modules other than app, create two new folders, debug and release, in the main folder:
8BQ9%_~6O4G@$DM)L7~(YC5.png

debug:

 <application
        android:allowBackup="true"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar"
        >
        <activity android:name=".ui.activity.RegisterActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

release

    <application
        android:allowBackup="true"
        android:label="@string/app_name"
        android:supportsRtl="true"
        >
        <activity android:name=".ui.activity.RegisterActivity"/>
    </application>

Then go to write the dynamically loaded manifest file, in the android domain in the build.gradle under the module

 sourceSets{
        main{
            if(isLibrary.toBoolean()){
                manifest.srcFile 'src/main/release/AndroidManifest.xml'
            }else {
                manifest.srcFile 'src/main/debug/AndroidManifest.xml'
            }
        }
    }

Finally, when adding dependencies under the app module, you also need to judge

 if (isLibrary.toBoolean()){
        implementation project(':userlibrary')
    }

Then the modularization is done separately as an entry and as a library.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324530410&siteId=291194637