Introduction to GitHub

Recently I’ve moved to using GitHub as opposed to Git on a server. Not much difference but I thought it would be useful to create a basic flow for GitHub and share it. So, here you go!

Create a new repo

1)      Ensure an appropriate gitignore file has been created

2)     From within the local directory type ‘git init’ to initialise this as a git repo

3)     Add the files that need to be committed with ‘git add’

4)     Commit these files with an appropriate message – ‘git commit –m “Initial commit of this repository”

5)     From within the github website, create a new repository

6)     Copy the remote repository URL from ‘Quick Setup’

7)     From the command line enter, ‘git remote add origin <copied remote repository URL>” – This sets the remote URL

8)     “git remote –v” can then be used to verify the new remote URL

9)     PUSH your changes with ‘git push origin master’

Clone an Existing Repo

1)      Create a local directory on your computer

2)     From within that directory enter ‘git clone https://githubcom/username/repo.git .’ – to clone the directory

Checkout a different remote branch

1)      After having cloned the project.

2)     “git fetch”

3)      “git branch -a” – Displays a list of all branches

4)     “git checkout <branchname>

5)     “git branch -a” – This can be used as a validation to show that you’re now on the expected branch.

Create a new Branch

1)      It’s not safe to work on the main branch, create your own working branch. Create one each time you have a block of work that you need to do

2)     “git checkout –b dev-my-branch-name”

Adding and Committing files

1)      Modify some code.

2)     You will then want to ‘add’ the files to the staging area, ‘git add .’ – to add all files (or ‘git add <filename>’ for specific ones)

3)     ‘git status’ will then show you which of your files are added to the staging area.

4)     To reset a file use ‘git rest HEAD <filename>’ – This removes your file from the staging area but does not delete or modify your file.

5)     A chunk of files that you have to fix a small piece of code is a commit. Once you’ve done some work you’re happy with you can commit.

6)     ‘git commit –m “Some great and useful message”’ is used to create a LOCAL commit. You can then continue working if you want or move onto pushing.

Pushing to the remote repo

1)      Type ‘git push’ it will fail as you haven’t specified where the file should go… i.e. the remote branch.

2)     Follow the prompt to push to the appropriate repo, i.e. master if you have cloned directly, branch if you created a branch.

3)     Type ‘git push – -set-upstream origin master’ (Note.. that’s 2 dashes before the word set) (FOR MASTER)

4)     Type ‘git push – -set-upstream origin the-name-of-my-branch’ (Note.. that’s 2 dashes before the word set) (FOR BRANCH)

5)     Enter your username and personal authentication key

Merging from the branch back to the main branch

1)      Eventually you will hopefully want to merge code from the repo to the main branch. This can be done with git merge but ingithub, ‘pull’ requests are used. This is where you ‘request’ the main branch <owner> to pull your code into the main repo.

2)     From within the github website, locate your branch and create a new ‘pull request’

3)     Add a detailed description. Use @username to specify an individual.

4)     Review the code from within github’s UI.

5)     Add comments etc… (LGTM if it’s all good.. )

6)     The main branch owner can then ‘Merge Pull Request’ from within github’s UI to integrate these changes.

(NOTE.. this completely ignores the testing phase which would occur between pushing the repo and merging back from the main branch.

Generally you should add TestScripts for any code that you write and commit these with any new files.)

Android Studio – Using a library Project

In the last post, we created our own Android library. The purpose of this was to create reusable code that could be used in any of our projects. If you haven’t yet read that, click here to read it first.

In this post we will now take that library and use it.

A quick note: The purpose of this app is to show you how to use the library  you created and, as such we don’t take care of ensuring it’s ‘safe’, i.e. we won’t worry about ensuring the text boxes only contain numbers etc…

1) As always, launch Android Studio, then create a new project. Choose ‘Empty Activity

2) Create three EditText views and three buttons. The first two of these views will be used to enter numbers and the third to display a result. The three buttons will be calling the maths functions that we created in our library to add, subtract and multiply. (Yes, that’s right.. there’s no divide function) 🙂

3) The library we created in the previous post (which you should have sensibly renamed to something like mathxlib.arr) should be added to the application\apps\libs folder.

4) In the appication’s build.gradle file, add the following:

repositories{
        flatDir{
            dirs 'libs'
        }
    }

5) In the dependencies section add your library (edit as appropriate):

compile(name:'mathxlib', ext:'aar')

6) Now, in your main activity, instantiate your library, i.e. in my case:

MathX mathX = new MathX();

7) Create code to handle the onClick event of your buttons. In each case call the appropriate method in your library.

int x = Integer.parseInt(editTextInputOne.getText().toString());
int y = Integer.parseInt(editTextInputTwo.getText().toString());

int result = mathX.addX(x, y);

textResult.setText(String.valueOf(result));

 

8) Done. You’ve now create an application that uses your library.

 

Sample code for this blog post can be found on my GitHub:

UseLibrary_MathApplication

Android Studio – Creating a library Project

Recently I have been developing some Android projects. Each application has had the requirement to retrieve data from an on-line web server and, since I have been using the Volley library, I’ve had to copy my wrapper code into each project. Additionally I’ve had to copy the same GSON code to parse this data. This is of course time consuming and not a good approach to programming.

Another more sensible approach is to create a library that contains the required methods.

So, in this post let’s look at how to do that:

1) Launch Android Studio and create a new project, give it a sensible name, such as ‘MathsLibrary’ and choose ‘No Activity’ when prompted.

2) We now have a standard Android application project. This is no good as that’s what we’d use to generate an Android Application. We however wish to generate an Android library or, in fact an Android Archive (AAR).

3) To change this to a library:

  • Open the app’s build.gradle file and change the line

from:

  • apply plugin: 'com.android.application'

to:

  • apply plugin: 'com.android.library'

4) Delete the applicationId line from the DefaultConfig section of build.gradle

5) Click ‘Sync Now

You’ve now created an empty library 🙂

6) Navigate to your src directory and then add a ‘Math’ class.

7) Add functions to add, subtract and multiply.

8) Click build and your library should build without any errors.

9) Now you’ve built a ‘debug’ library but it’s a ‘release’ library you should be building

10) Click on Build and then change the Build Variant to release.

11) Rebuild the project.

12) Now choose Build APK.

13) Since we aren’t building an app, Android Studio won’t generate an APK, it will instead generate a file entitled app-release.aar which can be found in the ‘app\build\outputs\aar‘ directory.

If you want to know how you can use this library in a project, click to read the next post.

Sample code for this blog post can be found on my GitHub:

UseLibrary_MathLibrary