.gitignore for Android

*.iml .gradle/ local.properties build/ !**/src/**/build/ .cxx/ *.apk *.aab

An Android .gitignore ignores Gradle’s build/ folders and .gradle/ cache, local.properties with its path to your SDK, Android Studio’s *.iml module files, NDK output in .cxx/, and built *.apk and *.aab packages. Those are the key lines of the tool’s Android template, which has 12.

What the blocks ignore

  • Build output: build/ in every module, so app/build/ goes; !**/src/**/build/ keeps a package such as com.acme.build, which lives in a source folder called build.
  • Machine state: .gradle/ and local.properties, which holds sdk.dir for one computer.
  • IDE files: *.iml, regenerated from the Gradle files on sync.
  • Native and packaged output: .externalNativeBuild/, .cxx/, *.apk, *.aab, *.ap_ and *.dex.
  • Emulator captures: captures/.

In a scratch repository with this file, git check-ignore -v -n --no-index names the line that matched each path, or prints :: when none did:

.gitignore:7:build/	app/build/outputs/apk/debug/app-debug.apk
.gitignore:6:local.properties	local.properties
.gitignore:3:*.iml	app/app.iml
.gitignore:9:!**/src/**/build/	app/src/main/java/com/acme/build
.gitignore:14:.cxx/	app/.cxx/x
::	gradle/wrapper/gradle-wrapper.jar
::	gradlew
::	upload.jks
::	app/google-services.json

Keep the wrapper, keep signing keys out

Commit gradlew, gradlew.bat and gradle/wrapper/, jar included: without the jar, ./gradlew can’t start on a fresh clone. This template has no *.jar line, so the jar is safe; with the Java (Gradle) template, a !gradle/wrapper/gradle-wrapper.jar line keeps it.

The template doesn’t ignore keystores, so upload.jks above would be committed. Add your signing files as custom lines:

.gitignore:22:*.jks	upload.jks
.gitignore:23:*.keystore	release.keystore
.gitignore:24:keystore.properties	keystore.properties

Keeping one APK

*.apk also catches a test fixture. A later ! line brings it back, because its folder isn’t excluded:

.gitignore:25:!**/src/**/*.apk	app/src/androidTest/assets/sample.apk
.gitignore:16:*.apk	app/release/app-release.apk

Inside build/ that can’t work: Git never looks into an excluded directory, so no ! line keeps a file there.

Already committed build/ or local.properties?

.gitignore only affects untracked files. Untrack them, keeping them on disk, and commit:

git rm -r --cached app/build .gradle local.properties
git commit -m "Stop tracking build output and local settings"

Android Studio and OS files

Android Studio keeps per-user state in .idea/, such as workspace.xml. Pick the JetBrains template for those, and keep .DS_Store and Thumbs.db in a global ignore file:

git config --global core.excludesFile ~/.gitignore_global

Try it in the Gitignore Generator

In the file: Android

.gitignore

1 template · 12 pattern lines
# Android
# Module files, regenerated from Gradle
*.iml
.gradle/
# Machine-specific path to the Android SDK
local.properties
build/
# A source folder called build, such as the package com.example.build, is kept
!**/src/**/build/
# Screenshots and videos taken from the emulator
captures/
# Intermediate output from the NDK
.externalNativeBuild/
.cxx/
# Packages; the output of a release build
*.apk
*.aab
*.ap_
*.dex
  • ✗Ignored by "local.properties" (Android).

Lines are applied top to bottom, and the last one that matches wins — which is why a line of your own can overrule a template. This one was read as a file.

Runs in your browser. Nothing you enter is sent anywhere.