What should be in my .gitignore for an Android Stu

2018-12-31 08:06发布

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

I've seen several examples that all include .iml but IntelliJ docs say that .iml must be included in your source control.

30条回答
听够珍惜
2楼-- · 2018-12-31 08:39

I support the committing of .idea folder (excluding workspace.xml and tasks.xml). But I am starting to come to the conclusion that .iml files should be ignored.

Here is the issue:

Open a project in a directory named "foo" for example and you will get foo.iml and that all seems well and good. The problem is that if I simply rename the directory to foo2 (or clone it into another directory name) when you try to open the project in Android Studio you will get three things:

  • A new iml file named foo2.iml
  • The iml file for your Android project will be changed to point now to foo2 as its parent
  • .idea/modules.xml will have a line added for foo2.iml so it has both the old iml file and the one for the new directory

I can find no way to prevent Android Studio from doing this iml file generation when the project is stored in a different directory. Adding them to source control is going to cause problems. Therefore I think perhaps we should ignore *.iml files and .idea/modules.xml

查看更多
梦醉为红颜
3楼-- · 2018-12-31 08:40

Updated 7/2015:

Here is the definitive source from JetBrains


Directory based project format (.idea directory)

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

  • All the files under .idea directory in the project root except the workspace.xml and tasks.xml files which store user specific settings
  • All the .iml module files that can be located in different module directories (applies to IntelliJ IDEA)

Be careful about sharing the following:

  • Android artifacts that produce a signed build (will contain keystore passwords)
  • In IDEA 13 and earlier dataSources.ids, datasources.xml can contain database passwords. IDEA 14 solves this problem.

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/libraries in case they are generated from Gradle project

Legacy project format (.ipr/.iml/.iws files)

  • Share the project .ipr file and all the .iml module files, don't share the .iws file as it stores user specific settings

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


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

# Android Studio / IntelliJ IDEA 
*.iws
.idea/libraries
.idea/tasks.xml
.idea/vcs.xml
.idea/workspace.xml
查看更多
妖精总统
4楼-- · 2018-12-31 08:42

To circumvent the import of all files, where Android Studio ignores the "Ignored Files" list, but still leverage Android Studio VCS, I did the following: This will use the "Ignored Files" list from Android Studio (after import! not during) AND avoid having to use the cumbersome way Tortoise SVN sets the svn:ignore list.

  1. Use the Tortoise SVN repository browser to create a new project folder directly in the repository.
  2. Use Tortoise SVN to checkout the new folder over the top of the folder you want to import. You will get a warning that the local folder is not empty. Ignore the warning. Now you have a versioned top level folder with unversioned content.
  3. Open your project from the local working directory. VCS should now be enabled automatically
  4. Set your file exceptions in File -> Settings -> Version Control -> Ignored Files
  5. Add files to SVN from Android Studio: select 'App' in Project Structure -> VCS -> Add to VCS (this will add all files, except "Ignored Files")
  6. Commit Changes

Going forward, "Ignored Files" will be ignored and you can still manage VCS from Android Studio.

Cheers, -Joost

查看更多
查无此人
5楼-- · 2018-12-31 08:44

Github maintains useful gitignore items for various kinds of projects. Here is the list of useful gitignore items for android projects.

# Built application files
*.apk
*.ap_

# Files for the ART/Dalvik VM
*.dex

# Java class files
*.class

# Generated files
bin/
gen/
out/

# 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 Studio captures folder
captures/

# Intellij
*.iml
.idea/workspace.xml
.idea/tasks.xml
.idea/gradle.xml
.idea/libraries

# Keystore files
*.jks

# External native build folder generated in Android Studio 2.2 and later
.externalNativeBuild

# Google Services (e.g. APIs or Firebase)
google-services.json

# Freeline
freeline.py
freeline/
freeline_project_description.json

Android Gitignore in github

查看更多
泪湿衣
6楼-- · 2018-12-31 08:46

I had problems with ignoring build files, but this seems to work :-)

# 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

# Eclipse project files
.classpath
.project

# Android Studio
.idea/
.gradle
/*/local.properties
/*/out
/*/*/build
/*/*/production
*.iml
*.iws
*.ipr
*~
*.swp

*/build
*/production
*/local.properties
*/out
查看更多
梦该遗忘
7楼-- · 2018-12-31 08:48

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.

I got sick of constantly seeing messages about the various library.xml files that Gradle creates or deletes in /.idea. The build.gradle will 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 /.idea when a developer creates a project using Check out from Version Control, so why does anything in that folder need to be versioned?

If the *.iml is 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?

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

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

Edit 2: Added .DS_Store now that I am using Mac. This folder is Mac specific and should not be versioned.

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 
查看更多
登录 后发表回答