android - 如何在 Android Studio 中正确使用 Git?

标签 android git android-studio version-control

我知道这是关于 stackoverflow 的一个备受讨论的话题,但我就是不知道如何使这项工作发挥作用。我想:

  1. 创建一个 Android Studio 项目
  2. 使用 git checkin 项目
  3. 将项目推送到 bitbucket
  4. 将项目 pull 到另一台计算机上

这是我正在使用的 .gitignore(基本上是 Android Studio 创建的,我在其中删除了 *.iml 文件 - 因为它不起作用)

.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures

但不知何故,它就是行不通。无论我的 gitignore 文件是什么样的,Android Studio 都无法识别该项目。

那么实际的问题:如何将 Android Studio 项目推送到 git,以便我可以简单地将其 pull 入另一个客户端继续工作?

编辑:

以下是我做事的步骤,一步一步:

  1. 在 Android Studio 中创建项目 GitTest
  2. 在 Bitbucket 上创建一个 git 存储库
  3. 在 Android Studio 中:选择“启用版本控制集成” - 选择“git”
  4. 浏览 gitignore 文件并将其更改为匹配 Mauker 的
  5. 在“左侧项目”下选择“项目”,然后在 Git 下“添加”

    (screenshot)

  6. 提交/推送更改(在过程中定义远程仓库)

此时,项目在 bitbucket 上。接下来是“导入项目”部分:

  1. 打开安卓工作室
  2. 选择“从版本控制中 checkout 项目”
  3. 从 Gradle 导入项目(建议使用默认成绩包装器)
  4. 检测到未注册的 VCS 根目录 -> 添加根目录

项目加载,我得到某种 NullPointerException

(screenshot)

最佳答案

在另一台计算机上,您可以尝试导入项目,而不是打开它。

实际上,您不必将项目特定文件提交到您的 git 存储库。 Android Studio 足够智能,可以从许多不同的来源导入项目。

With IntelliJ IDEA you can not only create an Android project from scratch, but also import an existing project developed using other tools. One of the most common scenarios is importing an existing Android-Gradle project. However, you can also import a Maven, Eclipse or Flash Builder project, or even build a new project from a bunch of source files.

就像这样:

enter image description here

And here's a nice and more detailed tutorial关于如何在 Android Studio 上从源代码和其他地方导入项目。

对于您的 .gitignore 文件,我强烈建议您使用 gitignore.io 获取一个网站。

另外,如果您已经提交了不应该存在的文件,请检查 this question看看如何移除它们。

这是我在一个项目中使用的 .gitignore 文件的示例。

# Created by https://www.gitignore.io/api/android,osx,windows,linux,intellij,java

### Android ###
# Built application files
*.apk
*.ap_

# Files for the Dalvik VM
*.dex

# Java class files
*.class

# Generated files
bin/
gen/

# Gradle files
.gradle/
build/

# Local configuration file (sdk path, etc)
local.properties

# Proguard folder generated by Eclipse
proguard/

# Log Files
*.log

# Android Studio Navigation editor temp files
.navigation/

### Android Patch ###
gen-external-apklibs


### OSX ###
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk


### Windows ###
# Windows image file caches
Thumbs.db
ehthumbs.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msm
*.msp

# Windows shortcuts
*.lnk


### Linux ###
*~

# KDE directory preferences
.directory

# Linux trash folder which might appear on any partition or disk
.Trash-*


### Intellij ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio

*.iml

## Directory-based project format:
.idea/
# if you remove the above rule, at least ignore the following:

# User-specific stuff:
# .idea/workspace.xml
# .idea/tasks.xml
# .idea/dictionaries

# Sensitive or high-churn files:
# .idea/dataSources.ids
# .idea/dataSources.xml
# .idea/sqlDataSources.xml
# .idea/dynamic.xml
# .idea/uiDesigner.xml

# Gradle:
# .idea/gradle.xml
# .idea/libraries

# Mongo Explorer plugin:
# .idea/mongoSettings.xml

## File-based project format:
*.ipr
*.iws

## Plugin-specific files:

# IntelliJ
/out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.ear

关于android - 如何在 Android Studio 中正确使用 Git?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34295943/

相关文章:

android - 在 Android 平板电脑版本 4.4.2 中扩展类 EditText 时出错

java - E/AndroidRuntime(29668): java. lang.RuntimeException:无法启动 Activity ComponentInfo {错误膨胀类 fragment

android - 在 React Native 中加载屏幕时如何滚动到顶部?

android - 如何检索从 Google Glass 安装的 apk 文件?

sql-server - SQL Server 版本控制与 git 集成?

android - 将项目更新为兼容 Android M

git - 在两个目录之间创建 git diff 并将补丁应用于目录

eclipse - 无法在Eclipse中提取Gradle项目

android-studio - 在 Android Studio 模块中使用不同的 gradle 构建工具

xml - 创建一个不使用androidx的新项目