Creating an Android App for beginners — Part II

Build tools

The standard build tool is Gradle, which was built on the Groovy language. It lets you manage the project dependencies and build process by configuring project Modules. Each module has its own dependencies, tasks and outputs. An Android project contains a build.gradle which defines most of its configuration and dependencies. When you create a new project on Android Studio, it already creates a basic structure with a build.gradle file for you, so you don't have to write it from scratch.

compile 'com.example:libraryName:1.0.0'
compile files('libs/auto-moshi-0.1-SNAPSHOT.jar')

Libraries

Now we'll get back to our Elifut application mentioned in the first article. You can follow through the code on Github. We'll start by adding an initial Activity called MainActivity which will be our first screen. In this screen, the user will be able to type their name and select a country, using our API. But before jumping into the actual code, it's very important for any Android app to pick the right tools (libraries) to use. The list below consists of my personal preferences and should not by any means be considered final or absolute truth. Given the option, I tend to pick a Square library, just because they are, in general, very well know, widely used and simply are great libs overall. Here are some of my go-to, organized by category:

  • Support libraries: This is a no-brainer. If you want to support older Android versions with very little effort, always include the Android Support library in your project, which backport newer APIs so you can use them safely. There are several different support libs for different objectives, like the CardView, Palette, Design, RecyclerView, etc. Make sure you know and use them! These are all maintained by Google.
  • Networking - Retrofit: This is such an ubiquitous and great library, I just can't avoid using it. You just write a Java interface with annotated methods that define your API endpoints and Retrofit generates the implementation for you. It's super convenient and works great. Highly recommended. Another options is using Volley, which was written at Google.
  • Image Loading: Picasso: Another Square library, provides a very simple and intuitive API. Also take a look on Glide, if you want to evaluate other options.
  • Event Bus: Otto or EventBus: I don't have a strong preference here, you can pick either one, but using an event bus is a great way to prevent tight coupling between your app components.
  • JSON Parsing: There are just a lot of options here, most notably Gson and Jackson. These ones provide a higher level API with annotations support and automatic data binding to your Java classes. You could also use the plain org.json classes included in the Android SDK if you don't need anything fancy. Lastly, Square recently released Moshi, which has an API similar to Gson's. Also take a look on LoganSquare if you have time.
  • View binding: After a couple days working with Android, most people realize how tedious and cumbersome it is to do findViewById all the time. That's why you'll want to take a look on ButterKnife which simplifies that (and a lot of other view-related code). Also, Android has a new Data Binding library (still in beta) that will allow you to "embed code" in your XML views like JSP or ERB, for example.
  • Miscellaneous: I also really like using Icepick for simple Activity/Fragment state saving/restoration and Google's AutoValue. Lastly, you'll probably also want something to handle Parcelable implementation for you. For that, my favorites are AutoValue and auto-value-parcel (still under development).

First Activity

Now we're start writing our first Activity. In the Android world, activities are simply screens of your app. They can contain multiple Views and Fragments. As the official documentation says, Views are the basic building blocks for user interface components. We'll look into Fragments later. This activity will be called MainActivity and will be the entry point of our app.

@Bind(R.id.toolbar) Toolbar toolbar;
@Bind(R.id.collapsing_toolbar) CollapsingToolbarLayout ctLayout;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
toolbar.setTitle("Hello World");
}

Conclusion

In this article we went through the steps needed to create a new Android application, how the build system works, creating an Activity, a layout for it and using its Views in the Java code. Next up we'll dig deeper into the application code. Stay tuned!

--

--

Brazilian, Gaúcho and Software Engineer at Stripe

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store