组件化的简单实现,各module可独立编译

可参照项目《Android 组件化实战》

首先可以先学习通过gradle的配置,去实现各个Module的可独编译,具体项目中各个Module的业务拆分和依赖问题,及基础库和公众业务层的处理,还需要更多的业务实践;

可以在项目的gradle.properties进行开关配置,也可以在build.gradle文件中处理;

# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
#
#
# 每个组件是否单独编译:true为单独编译,false将作为依赖的module编译;
#
# compileSeparetelyHomeModule=true
 compileSeparetelyHomeModule=false
# compileSeparetelyChatModule=true
 compileSeparetelyChatModule=false
# compileSeparetelyRecomModule=true
 compileSeparetelyRecomModule=false
# compileSeparetelyMeModule=true
 compileSeparetelyMeModule=false

主项目的build.gradle中的配置如下:

apply plugin: 'com.android.application'

android {
    compileSdkVersion rootProject.ext.android["compileSdkVersion"]

    defaultConfig {
        applicationId "com.windfallsheng.componentsimpleexample"
        minSdkVersion rootProject.ext.android["minSdkVersion"]
        targetSdkVersion rootProject.ext.android["targetSdkVersion"]
        versionCode rootProject.ext.android["versionCode"]
        versionName rootProject.ext.android["versionName"]
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        javaCompileOptions {
            annotationProcessorOptions {
                arguments = [AROUTER_MODULE_NAME: project.getName()]
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    testImplementation rootProject.ext.dependencies["junit"]
    androidTestImplementation rootProject.ext.dependencies["test-runner"]
    androidTestImplementation rootProject.ext.dependencies["test-espresso-core"]
    implementation rootProject.ext.dependencies["appcompat-v7"]
    implementation rootProject.ext.dependencies["design"]

    if (!compileSeparetelyHomeModule.toBoolean()) {
        implementation project(':module_home')
    }
    if (!compileSeparetelyChatModule.toBoolean()) {
        implementation project(':module_chat')
    }
    if (!compileSeparetelyRecomModule.toBoolean()) {
        implementation project(':module_recom')
    }
    if (!compileSeparetelyMeModule.toBoolean()) {
        implementation project(':module_me')
    }

    implementation project(':lib_res')
    implementation project(':lib_base')
    implementation rootProject.ext.dependencies["arouter-api"]
    annotationProcessor rootProject.ext.dependencies["arouter-compiler"]
}

在一个Module的build.gradle中的配置如下:

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

android {
    compileSdkVersion rootProject.ext.android["compileSdkVersion"]

    defaultConfig {
        if (compileSeparetelyHomeModule.toBoolean()) {
            applicationId "com.windfallsheng.modulehome"
        }
        minSdkVersion rootProject.ext.android["minSdkVersion"]
        targetSdkVersion rootProject.ext.android["targetSdkVersion"]
        versionCode rootProject.ext.android["versionCode"]
        versionName rootProject.ext.android["versionName"]

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

//        这个配置只能限制xml文件里的资源,对于图片资源还是通过命名区别;
        resourcePrefix "home_"

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

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

    /*java插件引入了一个概念叫做SourceSets,通过修改SourceSets中的属性,可以指定哪些源文件
  (或文件夹下的源文件)要被编译,哪些源文件要被排除。*/
    sourceSets {
        main {
            if (compileSeparetelyHomeModule.toBoolean()) {
                manifest.srcFile 'src/main/module/AndroidManifest.xml'
            } else {
                manifest.srcFile 'src/main/AndroidManifest.xml'
            }
        }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    testImplementation rootProject.ext.dependencies["junit"]
    androidTestImplementation rootProject.ext.dependencies["test-runner"]
    androidTestImplementation rootProject.ext.dependencies["test-espresso-core"]

    implementation rootProject.ext.dependencies["appcompat-v7"]

    annotationProcessor rootProject.ext.dependencies["arouter-compiler"]
    implementation rootProject.ext.dependencies["eventbus"]

    implementation project(':lib_base')
    implementation project(':lib_res')
//    方式一:
//    if(mode_debug){
//        implementation project(':lib_debug')
//    }else{
//        implementation project(':lib_release')
//    }
//    方式二:
//    debugImplementation project(':lib_debug')
//    releaseImplementation project(':lib_release')
}

在每个Module中添加另一个独立编译时使用的AndroidManifest.xml,保证独立编译时有启动的Activity等;

阿里Arouter的使用可以参考官方的文档说明,不再赘述。

GitHub地址

由于作者水平有限,语言描述及代码实现中难免有纰漏,望各位看官多提宝贵意见!

Hello , World !

感谢所有!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

windfallsheng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值