Gitignore not ignoring some build files in Android library

In my case the .gitignore file was placed inside app directory instead of being in root of project directory.

I figured this out by:

  1. Open Project View in Android Studio
  2. Right-click on build folder
  3. Select Git
  4. Add to .gitignore

You must remove the first / in your lower level gitignore before build, it will work then. Also, in top level, you only need this: build/ and then no lower level gitignore will be needed.


Add .gitignore file in your project, and set below lines:

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

enter image description here


This answer on Stack Overflow helped me solve my issue.

Here is part of that answer:

First commit any outstanding code changes, and then, run this command:

git rm -r --cached .

This removes any changed files from the index(staging area), then just run:

git add .

Commit it:

git commit -m ".gitignore is now working"