You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从Git版本控制向Android Studio添加第二个项目

将本地模块改为从Git仓库获取的方法

下面是几种可行的实现方式,可根据你的需求选择:

方法一:Git子模块(推荐用于需要本地修改并同步仓库的场景)

  • 在主项目根目录执行Git命令,添加目标项目作为子模块:
    git submodule add https://github.com/你的用户名/other-project.git modules/other
    
    其中modules/other是子模块在本地的存放路径,可根据实际需求调整
  • 修改settings.gradle的配置,指向子模块的对应目录:
    include ':app', ':other'
    project(':other').projectDir = new File(settingsDir, "./modules/other/app")
    
  • 后续如需拉取子模块的最新代码,执行:
    git submodule update --remote
    

方法二:Gradle直接拉取Git仓库(适合无需本地修改、仅依赖特定版本的场景)

修改settings.gradle,使用includeBuild直接关联Git仓库:

include ':app'
includeBuild('https://github.com/你的用户名/other-project.git') {
    dependencySubstitution {
        substitute module('com.example:other') using project(':app')
    }
}

注意替换com.example:other为目标项目的模块坐标,project(':app')为目标仓库中对应模块的路径。

方法三:手动克隆后引用(临时测试场景适用)

  • 先将Git仓库克隆到本地指定路径:
    git clone https://github.com/你的用户名/other-project.git /本地路径/other-project
    
  • 修改settings.gradle中的路径配置:
    include ':app', ':other'
    project(':other').projectDir = new File('/本地路径/other-project/app')
    

内容的提问来源于stack exchange,提问作者André Santaló

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 09:15:30