What am I trying to achieve

I want to setup a new project on Android Studio and sync it with a repote repo setup on GitLab, using SSH.

In the previous post, Using Gitlab with Android Studio, I was accessing the remote repository on GitLab using an https link (like https://gitlab.com/group-name/androidlab-lifecycle.git). Then I turned on 2FA in the settings, and the old mechanism of cloning broke. To their credit, GitLab provided a very instructive error message instead of some garbage error code ala Microhard.

At this point, I could have set up an access token on GitLab, instead I though it might be more secure to setup SSH instead.

Step 1: Setup on GitLab

I set up a project on GitLab called androidlab-async-task, and selected the option to use SSH for accessing it. This took the form [email protected]:group-name/androidlab-async-task.git

Step 2: SSH key configuration

Then I ran the Git Gui, clicked on Help > Show SSH Key, which threw up a window as follows:

In my case, there was no key in the .ssh folder under “Users”, so it didnt show anything here. I clicked on Generate Key, created a suitable passphrase to protect the private key. It takes a second for the key to be generated. Then I clicked on “Copy to Clipboard” and pasted the same on GitLab under SSH Keys > Add new key.

The GUI can be closed at this point.

Step 3: Clone the remote repo

In my case, I’ve created an empty remote repo on GitLab, and using the Git GUI to clone it fails for reasons unknown. The Git Bash shell works just fine.

So I fired up the Git Bash shell, and ran the clone command, which takes the form git clone [email protected]:group-name/androidlab-async-task.git

The following messages were generated:

Cloning into ‘androidlab-async-task’…
Enter passphrase for key ‘xyz’:
warning: You appear to have cloned an empty repository.

And the remote repo was cloned just fine. At this point, I had an directory called androidlab-async-task under C:\Users\xxx\AndroidStudioProjects, with the .git directory created under it, which was what was expected.

Step 4: Configuring project in Android Studio

Next, I started Android Studio, and clicked on “Start a new Android Studio project” and selected the same directory as the project location. Note that the warning being shown is correct and shows up that things are working as expected 🙂

Next after going through various new project settings (API level, activity, etc), another warning showed up about “Unregistered VCS root detected”, which is also correct and as expected. I clicked on “Add root” to continue. This step is important since it it auto creates all the .gitignore files which are very useful.

Under “Version Control”, Studio showed me a bunch of files as “unversioned”. I added all the files VCS. All files shown will be relevant at this point, since the .gitignore has taken care of non-relevant files.

Next I performed a commit and push, and got another prompt to confirm SSH Server Key, followed by the password.

That’s pretty much it. Mission successful. I cross checked on the GitLab project dashboard and the new commit, with comment, etc were all visible.