Gradle Library Module的复用机制

本文介绍在Android Studio中使用Gradle高效复用模块的方法,包括单个Library Module的复用及多个Library Module的统一管理,有助于提升多应用开发效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

FBI Warning:欢迎转载,但请标明出处:http://blog.csdn.net/codezjx/article/details/49531887,未经本人同意请勿用于商业用途,感谢支持!

考虑如下情况

这里有两个AS项目,分别为projectA和projectB,结构分别如下:

projectA/
    ----build.gradle
    ----settings.gradle
    ----bluewhale/
    ----krill/

projectAsettings.gradle内容:include 'bluewhale', 'krill'

projectB/
    ----build.gradle
    ----settings.gradle
    ----hello/
    ----krill/

projectBsettings.gradle内容:include 'hello', 'krill'

你可以注意到projectAprojectB均包含相同的modulekrill,实际上他是一个相同的Library Project,那么问题就来了:如何高效的复用现有的module?实际开发中这个问题应该比较常见,特别针对于同时需要开发多个应用的场景,解决方法如下:

单个Library Module复用

  • 假设当前Project为GradleProject,在其settings.gralde中include项目的时候指定projectDir目录位置,如:
include ':LibraryA'
project(':LibraryA').projectDir = new File('../LibraryProject/LibraryA')
include ':LibraryB'
project(':LibraryB').projectDir = new File('../LibraryProject/LibraryB')
  • 然后在某个App Module中添加dependencies 即可:
dependencies {
    ...
    compile project(':LibraryA')
    compile project(':LibraryB')
}
  • 最后的情况如下截图:
    pic1

多个Library Module统一管理

上面的方式会有一个问题,依赖的library module都是以Project的形式导入的,如果引用越来越多library,外面的Project列表就会越来越长,这个时候我们可以把library module都放在一个Project中统一管理起来。

  • 首先新建一个LibraryProject目录,将用到的library module放进去,如LibraryA、LibraryB…

  • 拷贝一个默认的build.gradle过去:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
  • settings.gradle内加入:
include ':LibraryA', ':LibraryB'
  • 最终目录结构如下
LibraryProject/
    ----build.gradle
    ----settings.gradle
    ----LibraryA/
    ----LibraryB/
  • 最后修改一下GradleProject中的settings.gralde
...
// Root project of common library
include ':LibraryProject'
project(':LibraryProject').projectDir = new File('../LibraryProject/')
// Include library module we need
include ':LibraryProject:LibraryA'
include ':LibraryProject:LibraryB'
  • 最后的最后在GradleProject中的某个App Module中添加dependencies 即可(注意要添加:LibraryProject路径):
dependencies {
    ...
    compile project(':LibraryProject:LibraryA')
    compile project(':LibraryProject:LibraryB')
}
  • 最后的情况如下截图:
    pic2

(注意GradleProject和LibraryProject在同一个目录中哦!)

参考:

http://www.philosophicalhacker.com/2014/10/02/an-alternative-multiproject-setup-for-android-studio/
https://docs.gradle.org/current/userguide/build_lifecycle.html#sec:multi_project_builds

下面这个是博主在SO上面的提问&回答,觉得OK请点个up vote哦
http://stackoverflow.com/questions/31910947/how-to-reuse-the-submodule-in-gradle

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值