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, soapp/build/goes;!**/src/**/build/keeps a package such ascom.acme.build, which lives in a source folder calledbuild. - Machine state:
.gradle/andlocal.properties, which holdssdk.dirfor 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