Lesson 1 - Creating and managing a project in Android Studio

This lesson shows you how to create new Android Project with Android Studio, add new libraries, run it and manage VSC through Android's IDE interface.

Activity Type Lesson
Expected duration 30 minutes
Topic Creating and managing a project in Android Studio
Objective Create an Android Studio project in a Gitlab repository

1. Create an Android Project

As seen on developer.android.com

  1. In the Welcome to Android Studio window, click Start a new Android Studio project.
  2. In the Create New Project window, enter the following values:

    1. Application Name: "My First App"
    2. Company Domain: "example.com"

    You might want to change the project location. Also, if you want to write a Kotlin app, check theInclude Kotlin support checkbox. Leave the other options as they are.

  3. Click Next.

  4. In the Target Android Devices screen, keep the default values and click Next.

  5. In the Add an Activity to Mobil screen, select Empty Activity and click Next.

In the Configure Activit screen, keep the default values and click Finish.

After some processing, Android Studio opens the IDE.Now take a moment to review the most important files.

First, be sure the Project window is open (select View > Tool Windows > Project) and the Android view is selected from the drop-down list at the top of that window. You can then see the following files:

app > java > com.example.myfirstapp > MainActivity

This is the main activity (the entry point for your app). When you build and run the app, the system launches an instance of this Activity and loads its layout.

app > res > layout > activity_main.xml

This XML file defines the layout for the activity's UI. It contains a TextView element with the text "Hello world!".

app > manifests > AndroidManifest.xml

The manifest file describes the fundamental characteristics of the app and defines each of its components.

Gradle Scripts > build.gradle

You'll see two files with this name: one for the project and one for the "app" module. Each module has its own build.gradle file, but this project currently has just one module. You'll mostly work with the module's build.gradle file to configure how the Gradle tools compile and build your app.


2. Add useful libraries to your new project

Android libraries are used to speed up development time and implement functionalities which are not provided by the default API.

Aside from the libraries developed by Google itself, there are many third-party libraries for Android but several of them are "must have" libraries that are extremely popular and often used in almost any Android project. Some of those major libraries are:

Name Description
Retrofit A type-safe REST client for Android which intelligently map an API into a client interface using annotations.
Glide A powerful image downloading and caching library for Android.
ButterKnife Using Java annotations, makes Android development better by simplifying common tasks. Might Be replaced by Kotlin Synthetic Variables of Kotlin Extensions
Parceler Android Parcelable made easy through code generation
IcePick Android Instance State made easy
LeakCanary Catch memory leaks in your apps
Espresso Powerful DSL for Android integration testing
Robolectric Efficient unit testing for Android

When using libraries, you must keep in count that Android has some restrictions on its method count (65535 method limit). So, unless you are really going to squeeze the functionalities out of a library, you should not use it and instead build the functionality by yourself.

Let's say we want to load an image in our "Hello World" app using Glide. As with most dependencies, pulling Glide into a Gradle project is a single liner into your build.gradle file:

dependencies {
    ...
    implementation 'com.github.bumptech.glide:glide:4.6.1'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.6.1'
}

Make sure you use the key "implementation" instead of "compile", which was the rule before Android Studio 3.0.

After the change has been made, the IDE will then ask you to sync your project:

Give it a moment and get ready to use your new library, read Glide's documentation on how to do so.

That's how easy it is to implement an external library to your project. Be sure to carefully read the documentation and specs of a library before using it, and have fun experimenting with them.


3. Run your project

As seen on developer.android.com

  • Run it on a real device

Set up your device as follows:

  1. Connect your device to your development machine with a USB cable. If you're developing on Windows, you might need to install the appropriate USB driver for your device.
  2. Enable USB debugging in the Developer options as follows.
    First, you must enable the developer options:

    1. Open the Settings app.
    2. (Only on Android 8.0 or higher) Select System.
    3. Scroll to the bottom and select About phone.
    4. Scroll to the bottom and tap Build number 7 times.
    5. Return to the previous screen to find Developer options near the bottom. Open Developer options, and then scroll down to find and enable USB debugging.

Run the app on your device as follows:

  1. In Android Studio, click the app module in the Project window and then select Run > Run (or click Run in the toolbar).
  2. In the Select Deployment Target window, select your device, and click OK.

Android Studio installs the app on your connected device and starts it. You should now see "Hello World!" displayed in the app running on your device.

  • Run it on an emulator

Run the app on an emulator as follows:

  1. In Android Studio, click the app module in the Project window and then select Run > Run (or click Run in the toolbar).
  2. In the Select Deployment Target window, click Create New Virtual Device.
  3. In the Select Hardware screen, select a phone device, such as Pixel, and then click Next.
  4. In the System Image screen, select the version with the highest API level. If you don't have that version installed, a Download link is shown, so click that and complete the download.
  5. Click Next.
  6. On the Android Virtual Device (AVD) screen, leave all the settings alone and click Finish.
  7. Back in the Select Deployment Target dialog, select the device you just created and click OK.

Android Studio installs the app on the emulator and starts it. You should now see "Hello World!" displayed in the app running on the emulator.


4. GIT and VCS

For the purpose of this course, we are going to use GitLab as our online repository manager. GitLab is a great way to manage git repositories on a centralized server. Here are some useful and important terms you should keep in mind while relying on a VCS:

As seen on about.gitlab.com

  • Cloud Based Services

What is a cloud based service? It’s a service or resource that is opposed to services that are hosted on the servers inside a company, which is the traditional way of doing it. It helps people and companies lower their costs and be more efficient while helping with different functions such as trannings, storage, etc. GitLab.com is a cloud based service because it can be hosted both in house and in the cloud.

  • Source control or revision control software

What is source control? It’s a system that records and manages changes to projects, files and documents. It helps you recall specific versions later. It also makes it easier to collaborate, because it shows who has changed what and helps you combine contributions.

  • Continuous Integration (CI)

What is continuous integration? It’s the system of continuously incorporating the work advances with a shared mainline in a project. Git and GitLab together make continuous integration happen.

  • Continuous Deployment (CD)

What is continuous deployment? It means that whenever there is a change to the code, it is deployed or made live immediately. This is in contrast to continuous integration, where code is continuously being merged in the mainline and is always ready to be deployed, rather than actually deployed. When people talk about CI and CD what they usually mean to say is that they are constantly and automatically testing their code against their tests using a tool such as GitLab CI and upon passing to a certain action. That action could be merging the code into a branch (master, production, etc), deploying it to a server or building a package / piece of software out of it. Non-continuous integration would be everyone working on something and only integrating all the work as the very last step. Obviously, that results in many conflicts and issues, which is why CI is adopted widely nowadays.

  • Git

What is Git? Git is a system where you can create projects of different sizes with speed and efficiency. It helps you manage code, communicate and collaborate on different software projects. Git will allow you to go back to a previous status on a project or to see its entire evolution since the project was created. You could think of it as a time machine which will allow you to go back in time to whenever you’d like in your project. With Git, 3 basic issues were solved when working on projects:

  • It became easier to manage large projects.
  • It helps you avoid overwriting the team’s advances and work.
  • With git, you just pull the entire code and history to your machine, so you can calmly work in your own little space without interference or boundaries. It's much simpler and much more light-weight.

  • Repository

What is a repository? The place where the history of your work is stored.

  • Remote repository

What is a remote repository? It's a repository that is not-on-your-machine, so it's anything that is not your computer. Usually, it is online, GitLab.com for instance. The main remote repository is usually called “Origin”.

  • Commit

What is a commit? It’s the way you call the latest changes of source code that you made on a repository. When changes are tracked, commits mark the changes on a document.

  • Master

What is a master? It’s how you call the main and definitive branch (the independent line of development of a project).

  • Branch

What is a branch? It’s an independent line of development. They are a brand new working directory, staging area, and project history. New commits are recorded in the history for the current branch, which results in taking the source from someone's repository (the place where the history of your work is stored) at certain point in time, and apply your own changes to it in the history of the project.

  • Fork

What is a fork? It’s a copy of an original repository (the place where the history of your work is stored) that you can put somewhere else or where you can experiment and apply changes that you can later decide if publishing or not, without affecting your original project.

  • Git Clone

What is a clone? It’s to get a copy of a git project to look at or to use the code.

  • Git Merge

What is to merge? It’s integrating separate changes that you made to a project, on different branches.

  • Push a repository

What is to push a repository? It’s to incorporate a local branch (the independent line of development of a project) to a remote repository (online version of your project).W

Feel free to read more about Git versioning on about.gitlab.com.


5. Import your project into GitLab

For the sake of integrating Android Studio to GitLab, we are going to download GitLab Projects' plugin and install it. Keep in mind that you need to create an account in GitLab in order to complete this tutorial.

  1. In Android Studio, go to Preferences and select Plugins on the left-hand pane.
  2. Click Browse repositories...
  3. Type in "Gitlab" and select the GitLab Projects plugin, press Install.
  4. Click Restart Android Studio and wait for the IDE to restart.
  5. Open the VCS menu, select Import into Version Control and click Share on GitLab.
  6. Use https://gitlab.com as your server url, and click on the world icon besides the Access Token input to create a new Access Token. This should take you to https://gitlab.com/profile/personal_access_tokens.
  7. Follow the on screen instructions to create a new Access Token, pick a name and an expiration date, then make sure to check api and read user at least.
  8. Click Create personal access token.
  9. Your token will be on the top of the page, make sure to keep it safe, as you'll not be able to get it again.
  10. Go back to Android Studio and paste the token in the Personal Access Token input.
  11. You'll be prompted to insert the new Project name, the group or organization it belongs, an initial commit message, the auth type and the visibility settings. Select HTTP for a new project, and under "Group" you should either select your username or a group you create to share the project with your teammates. We also recommend setting the visibility to "Private".
  12. Your project should now be pushed to GitLab. Go to your account and make sure it is listed there. Now, let's make a simple change and commit & push it.
  13. Go to the project's root folder in Finder or your system explorer and open the .GitIgnore file. Add this lines:
    *.iml
    .gradle
    /local.properties
    /.idea/workspace.xml
    /.idea/libraries
    .DS_Store
    /build
    /captures
    .externalNativeBuild

    These files and folders should not be included in your repository, as they will make the commit changes unreadable and are totally unnecessary unless you want to include build logs and files, workspace properties and library installations on the repo.

  14. Open Android Studio, go to activity_main.xml and make some changes with the graphic layout editor. To open the visual editor you need to open the file and click Design at the bottom. Modify existing views by clicking on them and changing its settings on the right-hand pane Attributes.

  15. Insert new views by dragging them from the left-hand pane Palette into your canvas.

  16. After you've made some changes, go ahead and click on VCS menu, click Commit Changes... add a commit message and review the changes with the integrated version control view embedded below.

  17. To commit and push hover your mouse on top of the Commit button and click Commit and Push...

  18. Pay attention to the on-screen instructions and warnings that may help you correct some errors. If you check Reformat Code, Rearrange Code, Optimize Imports and Cleanup, the IDE will help you organize your files for the commit and keep your project clean and tidy.

6. Work on your repository from GitLab

Now that you have your project published on GitLab, it's time to make some adjustments to the repository and take advantage of some of the tools that it offers. In the following video you'll get a glance of how to do it, feel free to explore and incorporate milestones, issue tracking and invite your team-members to join you.


Version Author Date
1.0 Juan Santiago Acevedo February 2, 2018

results matching ""

    No results matching ""