git 对于 Android Studio 项目,我的 .gitignore 中应该包含哪些内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16736856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 16:13:51  来源:igfitidea点击:

What should be in my .gitignore for an Android Studio project?

gitandroid-studiogitignore

提问by respectTheCode

What files should be in my .gitignorefor an Android Studio project?

.gitignore的 Android Studio 项目中应该包含哪些文件?

I've seen several examples that all include .imlbut IntelliJ docs say that .imlmust be included in your source control.

我已经看到了几个示例,它们都包含在内,.iml但 IntelliJ 文档说.iml必须包含在您的源代码管理中。

回答by Lior Iluz

Updated to Android Studio 3.0Please share missing items in comments.

更新到 Android Studio 3.0请在评论中分享缺失的项目。

A late answer but none of the answers here and herewas right on the money for us...

一个迟到的答案,但这里和这里的答案都不适合我们的钱......

So, here's our gitignore file:

所以,这是我们的 gitignore 文件:

#built application files
*.apk
*.ap_

# files for the dex VM
*.dex

# Java class files
*.class

# generated files
bin/
gen/

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

# Windows thumbnail db
Thumbs.db

# OSX files
.DS_Store

# Android Studio
*.iml
.idea
#.idea/workspace.xml - remove # and delete .idea if it better suit your needs.
.gradle
build/
.navigation
captures/
output.json 

#NDK
obj/
.externalNativeBuild

Since Android Studio 2.2 and up to 3.0, new projects are created with this gitignore file:

从 Android Studio 2.2 到 3.0,新项目是用这个 gitignore 文件创建的:

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

Deprecated- for older project format, add this section to your gitignore file:

已弃用- 对于较旧的项目格式,将此部分添加到您的 gitignore 文件中:


/*/out
/*/*/build
/*/*/production
*.iws
*.ipr
*~
*.swp

This file should be located in the project's root folder and not inside the project's module folder.

此文件应位于项目的根文件夹中,而不是在项目的模块文件夹中。

Edit Notes:

编辑注释:

  1. Since version 0.3+ it seems you can commit and push *.imland build.gradlefiles. If your project is based on Gradle: in the new open/import dialog, you should check the "use auto import"checkbox and mark the "use default gradle wrapper (recommended)"radio button. All paths are now relative as @George suggested.

  2. Updated answer according to @128KB attached sourceand @Skela suggestions

  1. 从 0.3+ 版本开始,您似乎可以提交和推送*.imlbuild.gradle文件。如果您的项目基于 Gradle:在新的打开/导入对话框中,您应该选中"use auto import"复选框并标记"use default gradle wrapper (recommended)"单选按钮。正如@George 建议的那样,所有路径现在都是相对的。

  2. 根据@128KB附加来源和@Skela 建议更新答案

回答by Phil

Building on my normal Android .gitignore, and after reading through documentation on the Intellij IDEA website and reading posts on StackOverflow, I have constructed the following file:

基于我的普通 Android .gitignore,在阅读 Intellij IDEA 网站上的文档并阅读 StackOverflow 上的帖子后,我构建了以下文件:

# built application files
*.apk
*.ap_

# files for the dex VM
*.dex

# Java class files
*.class

# built native files (uncomment if you build your own)
# *.o
# *.so

# generated files
bin/
gen/

# Ignore gradle files
.gradle/
build/

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

# Proguard folder generated by Eclipse
proguard/

# Eclipse Metadata
.metadata/

# Mac OS X clutter
*.DS_Store

# Windows clutter
Thumbs.db

# Intellij IDEA (see https://intellij-support.jetbrains.com/entries/23393067)
.idea/workspace.xml
.idea/tasks.xml
.idea/datasources.xml
.idea/dataSources.ids

Also note that as pointed out, the built native filessection is primarily useful when you are building your own native code with the Android NDK. If, on the other hand, you are using a third party library that includes these files, you may wish to remove these lines (*.o and *.so) from your .gitignore.

另请注意,正如所指出的,构建本机文件部分在您使用 Android NDK 构建自己的本机代码时主要有用。另一方面,如果您使用包含这些文件的第三方库,您可能希望从 .gitignore 中删除这些行(*.o 和 *.so)。

回答by Sky Kelsey

Updated 7/2015:

7/2015 更新:

Here is the definitive source from JetBrains

这是JetBrains 的权威来源



Directory based project format (.idea directory)

基于目录的项目格式(.idea 目录)

This format is used by all the recent IDE versions by default. Here is what you need to share:

默认情况下,所有最近的 IDE 版本都使用此格式。以下是您需要分享的内容:

  • All the files under .ideadirectory in the project root exceptthe workspace.xmland tasks.xmlfiles which store user specific settings
  • All the .imlmodule files that can be located in different module directories (applies to IntelliJ IDEA)
  • .idea项目根目录下的所有文件,除了存储用户特定设置的workspace.xmltasks.xml文件
  • .iml可以位于不同模块目录中的所有模块文件(适用于 IntelliJ IDEA)

Be carefulabout sharing the following:

分享以下内容时要小心

  • Android artifacts that produce a signed build (will contain keystore passwords)
  • In IDEA 13 and earlier dataSources.ids, datasources.xmlcan contain database passwords. IDEA 14 solves this problem.
  • 生成签名构建的 Android 工件(将包含密钥库密码)
  • 在 IDEA 13 及更早版本中dataSources.idsdatasources.xml可以包含数据库密码。IDEA 14解决了这个问题

You may consider not to share the following:

您可以考虑不分享以下内容:

  • gradle.xml file, see this discussion
  • user dictionaries folder (to avoid conflicts if other developer has the same name)
  • XML files under .idea/librariesin case they are generated from Gradleproject
  • gradle.xml 文件,请参阅此讨论
  • 用户字典文件夹(以避免其他开发人员具有相同名称时发生冲突)
  • XML 文件.idea/libraries,以防它们是从 Gradle项目生成的

Legacy project format (.ipr/.iml/.iwsfiles)

传统项目格式(.ipr/ .iml/.iws文件)

  • Share the project .iprfile and all the .imlmodule files, don't sharethe .iwsfile as it stores user specific settings
  • 分享项目.ipr文件和所有的.iml模块文件,不同意.iws,因为它存储用户特定的设置文件

While these instructions are for IntelliJ IDEA, they hold true 100% for Android Studio.

虽然这些说明适用于 IntelliJ IDEA,但它们 100% 适用于 Android Studio。



Here is a .gitignoresnippet that incorporates all of the above rules:

这是一个.gitignore包含上述所有规则的片段:

# Android Studio / IntelliJ IDEA 
*.iws
.idea/libraries
.idea/tasks.xml
.idea/vcs.xml
.idea/workspace.xml

回答by theblang

I disagree with all of these answers. The following configuration is working great for our organization's app.

我不同意所有这些答案。以下配置非常适合我们组织的应用程序。

I ignore:

我无视:

  • /build
  • /.idea(with possible exceptions, see comments in dalewking's answer)
  • *.iml
  • local.properties

I think almost everyone agrees about /build.

我想几乎每个人都同意/build

I got sick of constantly seeing messages about the various library.xmlfiles that Gradle creates or deletes in /.idea. The build.gradlewill run on the developers's local when they first check out the project, so why do those XML files need to be versioned? Android Studio will also generate the rest of /.ideawhen a developer creates a project using Check out from Version Control, so why does anything in that folder need to be versioned?

我厌倦了不断看到有关library.xmlGradle 在/.idea. 该build.gradle为什么做这些XML文件需要版本将在开发者的地方时,他们首先检查了项目运行,这样的吗?Android Studio 也会/.idea在开发人员使用 来创建项目时生成其余部分Check out from Version Control,那么为什么该文件夹中的任何内容都需要进行版本控制?

If the *.imlis versioned a new user will have to name the project exactly the same as it was when committed. Since this is also a generated file, why version it in the first place?

如果*.iml是版本化的,新用户必须将项目命名为与提交时完全相同的名称。既然这也是一个生成的文件,为什么首先要版本它呢?

The local.propertiesfiles points to an absolute path on the file system for the SDK, so it definitely shouldn't be versioned.

这些local.properties文件指向 SDK 文件系统上的绝对路径,因此绝对不应对其进行版本控制。

Edit 1: Added .gradleto ignore the gradle caching stuff that should not be versioned (thanks Vasily Makarov).

编辑 1:添加.gradle以忽略不应版本化的 gradle 缓存内容(感谢Vasily Makarov)。

Edit 2: Added .DS_Storenow that I am using Mac. This folder is Mac specific and should notbe versioned.

编辑 2.DS_Store现在添加,因为我使用的是 Mac。此文件夹的Mac专用,应进行版本控制。

Additional note: You probably also want to add a directory to put your signing keys in when building a release version.

附加说明:您可能还想在构建发布版本时添加一个目录来放置您的签名密钥。

For copy/paste convenience:

为了复制/粘贴方便

.gradle
/build
/.idea
*.iml
local.properties
.DS_Store 

回答by helbaroudy

I use this .gitignore. I found it at: http://th4t.net/android-studio-gitignore.html

我使用这个.gitignore。我在以下位置找到它:http: //th4t.net/android-studio-gitignore.html

*.iml
*.iws
*.ipr
.idea/
.gradle/
local.properties

*/build/

*~
*.swp

回答by Siva Velusamy

In the case of Android Studio, the only files that are required to be saved in version control are the files required to build the application from the command line using gradle. So you can ignore:

对于 Android Studio,唯一需要保存在版本控制中的文件是使用 gradle 从命令行构建应用程序所需的文件。所以你可以忽略:

  • *.iml
  • .idea
  • build
  • *.iml
  • 。主意
  • 建造

However, if you save any IDE settings, such as custom code style settings, they get saved in the .idea folder. If you want those changes in version control, then you'd save the IDEA files as well (*.iml and .idea).

但是,如果您保存任何 IDE 设置,例如自定义代码样式设置,它们将保存在 .idea 文件夹中。如果您希望在版本控制中进行这些更改,那么您还需要保存 IDEA 文件(*.iml 和 .idea)。

回答by Willi Mentzel

Android Studio 3.4

安卓工作室 3.4

If you create a Gradle project using Android Studio the .gitignorefile will contain the following:

如果您使用 Android Studio 创建 Gradle 项目,该.gitignore文件将包含以下内容:

.gitignore

.gitignore

*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild

I would recommend ignoring the complete ".idea" directory because it contains user-specific configurations, nothing important for the build process.

我建议忽略完整的“.idea”目录,因为它包含特定于用户的配置,对构建过程来说并不重要。

Gradle project folder

Gradle 项目文件夹

The only thing that should be in your (Gradle) project folder after repository cloning is this structure (at least for the use cases I encountered so far):

存储库克隆后,您的(Gradle)项目文件夹中唯一应该存在的就是这种结构(至少对于我目前遇到的用例而言):

app/
.git/
gradle/
build.gradle
.gitignore
gradle.properties
gradlew
gradle.bat
settings.gradle

It is recommended to check-in the gradle wrapper scripts (see here).

建议签入 gradle 包装器脚本(请参阅此处)。

To make the Wrapper files available to other developers and execution environments you'll need to check them into version control.

要使其他开发人员和执行环境可以使用 Wrapper 文件,您需要将它们签入版本控制。

回答by Ingo

My advise would be also to not ignore the .idea folder.

我的建议也是不要忽略 .idea 文件夹。

I've imported a Git-based Eclipse project to Android Studio and that went fine. Later, I wanted to import this project with Git (like the first time) to another machine with Android Studio, but that didn't worked. Android Studio did load all the files but wasn't able to "see" the project as a project. I only could open Git-files.

我已经将一个基于 Git 的 Eclipse 项目导入到 Android Studio 并且一切顺利。后来,我想用 Git 将这个项目(就像第一次)导入到另一台带有 Android Studio 的机器上,但是没有用。Android Studio 确实加载了所有文件,但无法将项目“视为”项目。我只能打开 Git 文件。

While importing the project for the first time (from Eclipse to Android Studio) my old .gitignore was overwritten and the new one looked like this:

在第一次导入项目时(从 Eclipse 到 Android Studio),我旧的 .gitignore 被覆盖,新的看起来像这样:

  • .idea/.name
  • .idea/compiler.xml
  • .idea/copyright/profiles_settings.xml
  • .idea/encodings.xml
  • .idea/libraries/libs.xml
  • .idea/misc.xml
  • .idea/modules.xml
  • .idea/scopes/scope_settings.xml
  • .idea/vcs.xml
  • .idea/workspace.xml
  • .idea/.name
  • .idea/compiler.xml
  • .idea/copyright/profiles_settings.xml
  • .idea/encodings.xml
  • .idea/libraries/libs.xml
  • .idea/misc.xml
  • .idea/modules.xml
  • .idea/scopes/scope_settings.xml
  • .idea/vcs.xml
  • .idea/workspace.xml

So, I tried to use an empty gitignore and now it worked. The other Android Studio could load the files and the Project. I guess some files are not important (profiles_settings.xml)for Git and importing but I am just happy it worked.

所以,我尝试使用一个空的 gitignore,现在它起作用了。另一个 Android Studio 可以加载文件和项目。我想有些文件(profiles_settings.xml)对 Git 和导入并不重要,但我很高兴它起作用了。

回答by Hymany

It's the best way to generate .gitignorevia here

这是.gitignore通过这里生成的最佳方式

回答by Menuka Ishan

Tested with Android Studio 3.0

使用 Android Studio 3.0 测试

You might need to Install .ignoreplugin.

您可能需要安装.ignore插件。

You can auto-generate the .gitignore file for Android. Right click on folder and follow

您可以为 Android 自动生成 .gitignore 文件。右键单击文件夹并按照

Add .gitignore file

添加 .gitignore 文件

Then Select Android from left panel and click Generate

然后从左侧面板中选择 Android 并单击生成

Generate .gitignore file

Generate .gitignore file

Android Studio will generate .gitignore file which contains all the file need to ignore.

Android Studio 将生成 .gitignore 文件,其中包含所有需要忽略的文件。

Taken from http://menukanows.com/how-to-add-gitignore-file-in-android-project/

取自http://menukanows.com/how-to-add-gitignore-file-in-android-project/