Series Android components of the module and the switching between the configuration application

problem

Often more than the development of collaborative development, each person is responsible for different functions, first build the framework of the project by the main person in charge of the project, after everyone downloads by git, and then began to develop their own capabilities. Some independent functional modules self-contained module, such as camera roll, two-dimensional code and so on, this can increase the reusability of the project, you need to use a different module module can directly jump up. While coming out of a separate module, but how to put this module into an independent application easier we compile, debug and develop it? The need to develop the entire project is not a compile-time, this compilation speed greatly reduced, such as our project, you need to compile a good few minutes, this can not be tolerated. This time we can create a separate Application, just put your Application Module as a final merger, housing project jump directly to your Module it. In this way only in the development process can compile your own application, we can greatly improve our development efficiency. No need to compile the entire project, because the functional modules in the form of Application in the development process. When the integration of business functions only in the form of the Library.
So Module created yourself how to become a Application it? Or How to switch easily between Module and Application debugging? Detailed below

See complete project

Detailed

Step one: To make a unified database version of each module, we create a new config.gradle configuration file in the root directory of the project,

config.gradle content is to configure some basic parameters, whether to switch to the application's parameters isModule in this file, follow us change this value can be completed with the application of switching library.

ext {  //extend
    // false: 组件模式
    // true :集成模式
    isModule = false
    android = [
            compileSdkVersion: 28,
            minSdkVersion    : 15,
            targetSdkVersion : 28,
            versionCode      : 1,
            versionName      : "1.0"
    ]

    appId = ["app"  : "com.example.dn_component",
             "module1": "com.example.module1",
             "module2" : "com.example.module2" ]

    supportLibrary = "28.0.0"
    dependencies = [
            "appcompat-v7"     : "com.android.support:appcompat-v7:${supportLibrary}",
    ]
}

Step two: introducing config.gradle build.gradle root, and use the other module:

(1) root directory of the introduction of this config.gradle file:

//相当于引入头文件 将 config中的内容引入进来
apply from: "config.gradle"

(2) at the respective module build and works as the master, i.e. each module of the config file in gradle unified configuration variables:

//赋值与引用
def cfg = rootProject.ext.android
def appId = rootProject.ext.appId

android {
    compileSdkVersion cfg.compileSdkVersion

    defaultConfig {
        minSdkVersion cfg.minSdkVersion
        targetSdkVersion cfg.targetSdkVersion
        versionCode cfg.versionCode
        versionName cfg.versionName

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        consumerProguardFiles 'consumer-rules.pro'

        javaCompileOptions {
            annotationProcessorOptions {
                arguments = [ moduleName : project.getName() ]
            }
        }

    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

}

Step three: As module2 need to be configured to form module and application can be switched to each other, of course, require a separate application to start activity, but in fact is an ordinary Android project, you need to add application activity own entrance as well as the manifest file, configured directory structure :

From the figure above we can see that there are two AndroidManifest.xml, a catalog in the app, in a Module , mainly what to do with it? The following look at some android {} in the configuration of module2 gradle profile:

// Resource Allocation
        sourceSets {
            main {
                // use different components in the manifest file mode
                IF (! IsModule) {
                    manifest.srcFile 'the src / main / Module1 / the AndroidManifest.xml'
                    java.srcDirs' the src / main / Module1 / Java ',' src / main / java '

                }else{
                    manifest.srcFile 'src/main/AndroidManifest.xml'
                }
            }
        }

 The main role of isModule variables used to configure the user center module is not integrated into the main needs of APP to go, if it is true it is integrated into the main APP, this module will become a library database, if it is false, then it can as APP installed on the user's mobile phone. This is because the addition of such a code in build.gradle file module2 to control this library is library or APP:

if (isModule) { apply plugin: 'com.android.library' } else { apply plugin: 'com.android.application' }

(1) module2 gradle profile is as follows (or library is judged Application config parameters in accordance isModule):

//apply plugin: 'com.android.library'
//根据isModule标签动态的切换 集成/组件模式
if (isModule){
    apply plugin: 'com.android.library'
}else{
    apply plugin: 'com.android.application'
}

def cfg = rootProject.ext.android
def appId = rootProject.ext.appId

android {
    compileSdkVersion cfg.compileSdkVersion

    defaultConfig {
        minSdkVersion cfg.minSdkVersion
        targetSdkVersion cfg.targetSdkVersion
        versionCode cfg.versionCode
        versionName cfg.versionName

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        consumerProguardFiles 'consumer-rules.pro'

        javaCompileOptions {
            annotationProcessorOptions {
                arguments = [ moduleName : project.getName() ]
            }
        }

        //添加一条 boolean类型的变量
        buildConfigField("boolean","isModule",String.valueOf(isModule))

        //组件模式下
        if (!isModule){
            applicationId appId['module2']
        }

        //资源配置
        sourceSets{
            main{
                //在组件模式下 使用不同的manifest文件
                if(!isModule){
                    manifest.srcFile 'src/main/module/AndroidManifest.xml'
                    java.srcDirs 'src/main/module/java','src/main/java'

                }else{
                    manifest.srcFile 'src/main/AndroidManifest.xml'
                }
            }
        }

    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.android.support:appcompat-v7:28.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'


    implementation project(':base')

}

Here is our new ordinary Android module, so add a few directories behind his own hand, module system is created and no application layout, default activity of these steps are as follows:

(2) the new activity, and layout files, manifest file, application

These are the new space, but you need to add manifest inside the entrance to the activity, where the need to maintain two sets of manifest.xml file:

  • New application of activity
public class Module2Activity extends Activity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_module2);

    }
}
  • New application manifest file: 
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dn_alan.module2">

    <application android:name=".Module2Application">
        <activity android:name="com.example.module2.Module2Activity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
  •  The original module manifest file entry is not required activity,
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.module2" />

 So we just need to change the parameters of variables isModule in config.gradle profile of what you can achieve user pluggable modules form an integrated and stand-alone.

After configuring the above steps, here we have two alternative compiled application,

Of course, when the development is completed, you need to compile module2 to the main project, the isModule set config.gradle file to true can be.

Complete Project Address: https://github.com/buder-cp/DesignPattern/tree/master/buder_DN_component

Guess you like

Origin blog.csdn.net/cpcpcp123/article/details/103844376