Does it work?

Quickly setup Android Studio with Git and Bitbucket

Hello everyone,

today I will share my experience about creating a Git repository for a new Android Studio project.

This post dates back to 2016 and is migrated from an older blog.
My goal is to allow a fellow developer to checkout (Clone in Git words) the project from server and have it fully functional and ready to be run or to work on it and commit changes.

I had to find tiny bits of information sparse around the web ’cause it seems most people consider cool to have any developer configure locally their own project once they first checkout.

Well that is not the only way. Here is what I did.

First of all I chose Bitbucket and not Github because Bitbucket offers free private repository for small teams.
In this way you can share code with your fellow developers even if your project is not intended to go open source.

Setting up a Bitbucket repository

I first created my Bitbucket account signing in with my google account and providing a username and a password.

Once logged in you can select the Repositories menu and create your new repository.

Install Bitbucket plugin for Android Studio

I opened Android Studio and opened the SDK manager.
I then selected the plugins section and chose: Browse repositories…
I searched for bitbucket, selected the first result and installed it.
Please be advised that the plugin won’t work on your project until you have done your first commit via command line!!

 

Create the new project you want to add to VCS

In Android Studio create a new project with an empty activity.
In the main menu select: VCS –> Enable Version Control Integration…
In the next window select Git as versioning system so that the procedure will create a local working copy with a local Git repository.

Mark file as added to Git

If you right click on the project item in the ‘Project‘ panel: with  ‘Android‘ visualization selected, you will be able to choose: Git –> Add
In this way the relevant project files will be marked as added and, at the next commit, they will be committed to your local repository. At the first Push they will be pushed to the central repository once configured correctly (see following paragraphs).

The Gradle build files are not added at this stage but it is essential to add them in order to enable other developers to open a freshly downloaded copy and use it.Change the visualization to: ‘Project

Expand the Project node (in this image is the ‘Test’ node), expand the Gradle folder and select the following items (.gitignore file and gradle files):

Now Right click and select: Git –> Add for this files too.

Don’t try to do any other operations via the Bitbucket integration…they won’t work at the moment. Even the username and password requests will always fail at this stage.

First Commit via Command Line

You need to configure the connection between your local repository and the central (remote one) via command line, set up the account and execute the first push command all via command line.
At that point everything will start to work also in the GUI integration.
Navigate to your bitbucket project with your browser and copy the repository Url. You can find it at the top right corner:
At this pint you can open the Android Terminal
and issue the first command:
git remote add origin https://yourtesturepository.git
you can now commit but when pushing you will be prompted about your email and username so I suggest to do set up those first:
git config –global user.email “test@dummy.com”
git config –global user.name “test name”

You can now issue your first commit and then push the content to the central repository you have just set up:

git commit -m “Initial Commit”
git push -u origin master

From now on you will be able to commit and push via the GUI integration.
You will always be prompted for the password. Provide it and click enter.

From now on each time you commit via GUI you will se a window like the following, in which you can add a message and specify the author of the commit:

When clicking on commit you still can decide if to commit or ‘commit and push’. In case you choose the second option you will be prompted with a password request.

Saving your credentials

If you do not want to be asked for the password again write the following command and the password will be stored in plain text in the file: .git-credentials

git config credential.helper store

Please be aware that this command creates the file in the user folder and not in the working copy folder.
Then I assume this is not repository specific.
If you want to store it in the global Windows credential management system for better safety
specify another credential configuration method:

git config --global credential.helper wincred

 

First Checkout by another developer

When opening Android Studio choose: “Checkout project from version control”

 

Note that you don’t have to specify Bitbucket (even if you do, nothing will happen).

Select Git instead and you will be prompted with the following window for repository cloning:

 

Select remote repository, local path and local repository name and then click: Clone.
The local Working Copy and Repository will be created and you will be prompted with the following window:



Your answer must be: NO!!! Even Bitbucket tutorial specifies it.
You will find yourself back in the options menu and you will have to select:
Import project, browse to the folder in which you downloaded the project and select the file:
settings.gradle

Unregistered node root detected

When you will reopen your git versioned project (even if you are the owner who first added to version control), you probably will be prompted with the message:
‘unregistered node root detected’
and Android Studio will appear not to be aware of the fact that the project is already versioned.
You will be given the option to click on: ‘configure’. Do it and you will be prompted with a ‘Settings‘ modal window.
In such window select your project (it should be the only option) and click on the plus icon on the top right corner. The final result will be the following:
Then click Apply and then Ok
The Version Control panel will be available.

Leave a Reply

One thought on “Quickly setup Android Studio with Git and Bitbucket”

%d bloggers like this: