android 组件化和模块化开发模式切换

一.名词解释

    模块化:模块是APP的组成部分,所有的模块组装起来便是一个完整的APP

    组件化:使模块能够单独运行为组件化

二.模块化与组件化的差别

    (1) 类型不同

    模块:com.android.libray

    组件:com.android.application

    (2)有无applicationId

    模块:无

    组件:有

  (3)有无启动页

    模块:无

    组件:有

三.代码实现

思想思路,通过一个变量进行他们之间的差异化处理

a.创建配置文件

在于项目级build.gradle同级下下创建config.gradle

ext {

    //是否为模块模式.
// isModule=true 模块模式  
//isModule=false 组件模式
    isModule = false;
    android = [
            compileSdkVersion: 26,
            buildToolsVersion: "26.0.2",
            minSdkVersion    : 19,
            targetSdkVersion : 26,
            versionCode      : 1,
            versionName      : "1.0"
    ]//
    //模块名称 添加你的模块,用于模块和组件切换
    appId = [
            "app"         : "yourAPPId",
           ]
//其他配置
}

b.引用config.gradle

在项目级别的build.gradle引用

apply from: "config.gradle"

c.差异处理

在模块级别的build.gradle中使用isModule变量进行控制

def appId = rootProject.ext.appId
def android = rootProject.ext.android
if (isModule) {
    apply plugin: 'com.android.library'
} else {
    apply plugin: 'com.android.application'
}


android {
    compileSdkVersion android.compileSdkVersion
    buildToolsVersion android.buildToolsVersion

    defaultConfig {
        minSdkVersion android.minSdkVersion
        targetSdkVersion android.targetSdkVersion
        versionCode android.versionCode
        versionName android.versionName

        if (!isModule) {
            applicationId appId["activerepair"]
            multiDexEnabled true //分包配置
            ndk { abiFilters "armeabi" }//ndk配置
            signingConfigs {
                debug {
                    //测试keystore配置
                }
                release {
                    //正式
                    storeFile file("key地址")
                    storePassword '密码'
                    keyAlias '名称'
                    keyPassword '密码'

                }
            }
        }

        sourceSets {
            main {
                if (isModule) {
                    manifest.srcFile 'src/main/AndroidManifest.xml'
                } else {
                    //组件下特有的文件 
                    manifest.srcFile 'src/main/assembly/AndroidManifest.xml' //配置启动Activity
                    java.srcDirs 'src/main/assembly/java', 'src/main/java' //模块化下不需要打包的文件
                }
            }

        }

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"


    }

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

}

dependencies {
    //your dependencies
}













猜你喜欢

转载自blog.csdn.net/u013209460/article/details/80834333