如何从Git版本控制向Android Studio添加第二个项目
将本地模块改为从Git仓库获取的方法
下面是几种可行的实现方式,可根据你的需求选择:
方法一:Git子模块(推荐用于需要本地修改并同步仓库的场景)
- 在主项目根目录执行Git命令,添加目标项目作为子模块:
其中git submodule add https://github.com/你的用户名/other-project.git modules/othermodules/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ó
相关产品推荐
相关产品推荐

