What should be in my .gitignore for an Android Stu

2019-01-19 09:33发布

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楼-- · 2019-01-19 10:12

I know this is an old topic and there are certainly a lot of options, but I really prefer gibo by Simon Whitaker. It's super simple to use, cross-platform (mac, *nix, and windows), and uses the github gitignore repo so it is (basically) always up to date.

Make sure your local cache is up to date:

    $ gibo --upgrade
    From https://github.com/github/gitignore
     * branch            master     -> FETCH_HEAD
    Current branch master is up to date.

Search for the language/technology you need:

    $ gibo --search android
    Android

Display the .gitignore file:

    $ gibo Android
    ### 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

Now, append it to your project's .gitignore file:

    $ gibo Android >> .gitignore

(Make sure you use >> to append to your project's .gitignore file; one > will overwrite it - as I've done many times on accident!)

I know this isn't answering the OP's exact question, but using gibo makes it so you pretty much don't have to think about 'the question' anymore! .. it's nice! ;)

查看更多
3楼-- · 2019-01-19 10:14

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

查看更多
叛逆
4楼-- · 2019-01-19 10:14

This official documentation from JetBrains Support says the following should be included:

All files under .idea directory except workspace.xml and tasks.xml because
    they store specific user settings
All the *.iml files that can be located in different module directories

It also gives other recommendations of things to be careful about.

查看更多
乱世女痞
5楼-- · 2019-01-19 10:15

There is NO NEED to add to the source control any of the following:

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

So you can configure hgignore or gitignore accordingly.

The first time a developer clones the source control can go:

  1. Open Android Studio
  2. Import Project
  3. Browse for the build.gradle within the cloned repository and open it

That's all

PS: Android Studio will then, through maven, get the gradle plugin assuming that your build.gradle looks similar to this:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.12.2'
    }
}

allprojects {
    repositories {
        mavenCentral()
    }
}

Android studio will generate the content of .idea folder (including the workspace.xml, which shouldn't be in source control because it is generated) and the .gradle folder.

This approach is Eclipse-friendly in the way that the source control does not really know anything about Android Studio. Android Studio just needs the build.gradle to import a project and generate the rest.

查看更多
我命由我不由天
6楼-- · 2019-01-19 10:15

Depends on how your project format is maintained:

You have two options:

  1. Directory-based format (You will have a .idea folder which contains the project specific files)
  2. File-based format (configuration files are .iws and .ipr)

Ref: http://www.jetbrains.com/idea/webhelp/project.html

Files committed to version control depends on the above:

  1. Include .idea folder to version control, exclude workspace.xml and tasks.xml
  2. Version control .ipr file and all the .iml module files, exclude the .iws file as it stores user specific settings.

Ref: https://intellij-support.jetbrains.com/entries/23393067

查看更多
成全新的幸福
7楼-- · 2019-01-19 10:16

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

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

So, here's our gitignore file:

#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:

*.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:


/*/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 *.iml and build.gradle files. 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 source and @Skela suggestions

查看更多
登录 后发表回答