Android Studio中.gitignore的配置

Android Studio 項目目錄結構

在項目目錄下找到.gitignore文件(一般有兩個地方存在該文件:一是根目錄下,一是app目錄下), 在git中如果想忽略掉某個文件,不讓這個文件提交到版本庫中,可以通過配置.gitignore文件讓Git不跟蹤配置的文件。

這裏寫圖片描述

其中:

  • .gradle 是gradle 運行以後生成的緩存文件夾。
  • .idea 是android studio / Intellij IDEA 工程打開以後生成的工作環境配置文件夾。
  • app 文件夾是你的application module,其中包含你的源碼。
  • build 文件夾爲編譯時的緩存文件夾,每次運行時都會生成,同時你在運行了gradle clean 的任務以後它會被刪除清理掉。
  • gradle 文件夾中包含的是gradle-wrapper.jar 文件,通過配置其中的gradle-wrapper.properties 中的distributionUrl 可以給你的項目指定需要使用的gradle 版本。
  • .gitignore 文件爲git 版本控制的忽略清單。
  • gradle.build 爲project 全局的配置。
  • gradle.properties 爲 gradle 的參數配置。
  • *.iml 文件爲Android Studio / Intellij IDEA 爲每一個module 生成的配置文件
  • gradlew gradlew.bat 是gradle 任務的腳本命令。
  • local.properties 中配置個人電腦環境中的配置,這不能提供給別人使用。
  • settings.gradle 文件中可指定project 目錄中的任何一個文件夾爲gradle 的module

Android 開發 .gitignore 的配置

如果你使用git 作版本控制工具,那麼你可以輕鬆的使用android studio / Intellij IDEA 的.gitignore 插件來生成一份可以將這些文件排除在外的.gitignore 過濾清單。這樣在你使用git 分發代碼時,這些不必要的文件將不會被提交到git server 中去。

.gitignore插件

這裏寫圖片描述

打開項目目錄中的.gitignore,然後使用快捷鍵Ctrl+N打開Generate

這裏寫圖片描述

自定義的.gitignore模板

這裏寫圖片描述

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

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# Android Studio Navigation editor temp files
.navigation/

# Android Studio captures folder
captures/

# Keystore files
*.jks

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

###macOS###
*.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

###Linux###
*~

# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*

# KDE directory preferences
.directory

# Linux trash folder which might appear on any partition or disk
.Trash-*

# .nfs files are created when an open file is removed but is still being accessed
.nfs*

###Windows###
# Windows image file caches
Thumbs.db
ehthumbs.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msm
*.msp

# Windows shortcuts
*.lnk
###IntelliJ###
*.iml
*.ipr
*.iws
.idea/
app/libs/

.gitignore配置不生效的解決辦法

git rm -r --cached .
git add .
git commit -m "update .gitignore"

就是先把本地緩存刪除(改變成未track狀態),然後再提交。

其他.gitignore的配置

GitHub 有一個十分詳細的針對數十種項目及語言的 .gitignore 文件列表,你可以在 https://github.com/github/gitignore 找到它。各種配置文件,只需要組合一下就可以使用了。

參考文獻

[1] https://www.zhihu.com/question/33048493

[2] http://blog.csdn.net/watermusicyes/article/details/50348967

[3] https://git-scm.com/book/zh/v2/Git-基礎-記錄每次更新到倉庫

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章