Using Gitlab with Android Studio

I looked around everywhere for a decent (read easy to follow for a dumb operator like me) set of instructions for setting up an EXISTING (offline) Android Studio project onto a remote GIT repository on GitLab.

I found instructions for using Android Studio to pull an existing repo off GitHub or GitLab. I also found instructions for setting up a new Android Studio project on GitHub, but nothing for GitLab.

I followed the steps shown, and since it does not have an audio or accompanying instructions, I thought I’d document the steps I’ve taken to make this work.

Step 1: Initial setup on GitLab

The first step of course is to open an account on Gitlab. While Github is more popular, it does not offer private repositories for free. If you need a free repository, it must be public, which was a deal killer for me.

Interestingly, a number of services/companies have started offering free GIT hosting, and among those I went with Gitlab.

After opening an account, I set up a project there called androidlab-lifecycle, and selected the option to use HTTPS (rather than SSH) for accessing it. This took the form https://gitlab.com/group-name/androidlab-lifecycle.git

Step 2: GIT installation on Windows

I’d read somewhere that recent versions of Android Studio came with GIT preinstalled. Perhaps not the Windows version. So I headed over to GIT – download and downloaded the relevant Windows version and installed it. Options I selected are below.

Step 3: Enabling GIT for project in Android Studio

Then I checked if GIT was working fine from inside Android Studio.

Under File > Settings > Version Control > Git, click on the “Test” button, which resulted as follows:

This step is optional: I already have a project “LifeCycle” existing in Android Studio in the path C:\Users\xxx\AndroidStudioProjects. With the project opened in Android Studio, I clicked on VCS > Enable version control integration, which threw up a selection box where I selected “GIT” as the VCS system.

However I still suggest doing the above step since it auto creates all the .gitignore files which are very important. Note that the auto-created .git directory should NOT be used.

Ideally I would have liked to associate this existing project with the androidlab-lifecycle.git I’d setup on GitLab, i.e. by importing this project into the remote repository on GitLab. Alas this is not supported. Importing into GitHub is, but thats not my usecase.

So instead I followed a round about approach.

Step 4: Syncing with the remote GIT repo

I opened a GIT bash shell under C:\Users\xxx\AndroidStudioProjects and ran the following commands given by GitLab on the project page itself.

This ended up creating an empty directory (repository) named, of course, androidlab-lifecycle

I copied all the files from the existing project dir LifeCycle, without the .git folder since this is already present in the androidlab-lifecycle directory (and of course, will get populated with the next few commands)

git add *

This gives a message about various files being ignored as given in .gitignore, which is what we want of course. And a warning about LF and CRLF.

git commit -m “First time file add”

git push -u origin master

Voila! At this point, I checked the project dashboard under GitLab, and all the directories and files had appeared as expected.

Step 5: Configuring project in Android Studio

Next I opened the newly created project (androidlab-lifecycle) in Android Studio, which showed the following warning, and I clicked on “Add root”

After this, I made a small change in some file, to test whether the VCS system worked.

Then clicked on the “VCS Commit Changes” button (the one with the green arrow key towards VCS), entered a relevant comment, and clicked on “Commit and Push”.

Then went back to GitLab project dashboard to check and the new commit, with comment, etc were all visible.

That’s it. Android Studio project has been successfully setup from grounds up to work with remote repository on GitLab. Tada. Time to reward myself, and you may have 3 guesses how I shall do that 😀