How to use Git correctly in Android Studio?

2020-05-13 02:18发布

I know this is a much-discussed topic on stackoverflow, but I just can't figure out how to make this work. I would like to:

  1. create an Android Studio project
  2. check the project in with git
  3. push the project to bitbucket
  4. pull the project on a different computer

This is the .gitignore I'm using (basically the Android Studio created one, where I removed the *.iml files - cause that didn't work)

.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures

But somehow, it just won't work. Android Studio won't recognise the Project, no matter what my gitignore file looks like.

So actual question: how do I push an Android Studio project to git so I can simply pull it into another client to continue working?

EDIT:

So here’s how I do things, step-by-step:

  1. create project GitTest in Android Studio
  2. create a git repository on Bitbucket
  3. in Android Studio: select “enable version control integration” - pick “git”
  4. browse to the gitignore files and change it to match Mauker’s
  5. under “project on the left” select “Project” then under Git “Add”

    (screenshot)

  6. commit/push the changes (define the remote repo in the process)

at this point, the project is on bitbucket. Next comes the "import project" part:

  1. open android studio
  2. select “Check out project from Version Control”
  3. Import Project from Gradle (using the default grade wrapper as recommended)
  4. Unregistered VCS root detected -> add root

The project loads, I get some sort of NullPointerException

(screenshot)

2条回答
Explosion°爆炸
2楼-- · 2020-05-13 02:40

On the other computer you could try to import the project, instead of opening it.

Actually, you don't have to commit project specific files to your git repo. Android Studio is smart enough to import a project from many different sources.

With IntelliJ IDEA you can not only create an Android project from scratch, but also import an existing project developed using other tools. One of the most common scenarios is importing an existing Android-Gradle project. However, you can also import a Maven, Eclipse or Flash Builder project, or even build a new project from a bunch of source files.

Just like this:

enter image description here

And here's a nice and more detailed tutorial on how to import a project on Android Studio from source and other places.

For your .gitignore file I strongly recommend you to get one using gitignore.io website.

Also, if you already commited files that are not supposed to be there, check this question and see how you can remove them.

Here's an example of a .gitignore file that I use on one of my projects.

# Created by https://www.gitignore.io/api/android,osx,windows,linux,intellij,java

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

# Android Studio Navigation editor temp files
.navigation/

### Android Patch ###
gen-external-apklibs


### OSX ###
.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

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


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


### Linux ###
*~

# KDE directory preferences
.directory

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


### Intellij ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio

*.iml

## Directory-based project format:
.idea/
# if you remove the above rule, at least ignore the following:

# User-specific stuff:
# .idea/workspace.xml
# .idea/tasks.xml
# .idea/dictionaries

# Sensitive or high-churn files:
# .idea/dataSources.ids
# .idea/dataSources.xml
# .idea/sqlDataSources.xml
# .idea/dynamic.xml
# .idea/uiDesigner.xml

# Gradle:
# .idea/gradle.xml
# .idea/libraries

# Mongo Explorer plugin:
# .idea/mongoSettings.xml

## File-based project format:
*.ipr
*.iws

## Plugin-specific files:

# IntelliJ
/out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

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

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.ear
查看更多
Juvenile、少年°
3楼-- · 2020-05-13 02:57

I've already found this pretty good article about what you're searching for:

https://www.londonappdeveloper.com/how-to-use-git-hub-with-android-studio/

You can also visit CodePath tutorials to find more like:

https://github.com/codepath/android_guides/wiki/Using-Android-Studio

Hope it help

查看更多
登录 后发表回答