Your First Android Libraries
Tags: android, kotlin, java, beginners
Published: 2019-07-22 17:37:04

---
title: Your First Android Libraries
published: true
description: The starter apps come packaged with bare-bone dependencies. A list of the first libraries you should add.
tags: android, kotlin, java, beginner
cover_image: https://images.unsplash.com/photo-1472715148636-92e348325950?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1000&q=80
---

When you start a brand new app in Android Studio, you're dropped into a bare-bones project that likely just has a single, empty Activity.

If you're trying to build out a project beyond "`Hello World!`", especially if you plan to follow some architectural best practices like using MVVM, you'll quickly run into some limitations with the basic Android libraries included in every project.

After creating a few basic starter applications, I thought I'd share what I think are the best "first step" dependencies to add to your Android project.

## TL;DR - `app/build.gradle`

Before I go into more detail, here's the basic `dependencies` list I put together for my last starter app.

```groovy
dependencies {

    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.31"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    // enable ViewModelProviders
    implementation "androidx.lifecycle:lifecycle-extensions:2.0.0"
    // enable TextInputLayout
    implementation 'com.google.android.material:material:1.0.0'

    testImplementation 'junit:junit:4.12'
    // enable Mocking with Mockito
    testImplementation 'org.mockito:mockito-core:2.19.0'
    // enable InstantTaskExecutorRule
    testImplementation 'androidx.arch.core:core-testing:2.0.1'

    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    // non-deprecated AndroidJUnit4
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'

}
```

The lines I commented are the ones I added to what was included with the basic Android blank-Activity starter app in Android Studio 3.4. 

Let's take a closer look at what these libraries are and why we need them.

## Prod Code

The `implementation` keyword specifies dependencies in your actual, running, production code.

### Lifecycle Extensions

Enable some handy extensions for Android [Lifecycle components](https://developer.android.com/topic/libraries/architecture/lifecycle). Namely, the ability to use [`ViewModelProviders`](https://developer.android.com/reference/android/arch/lifecycle/ViewModelProviders).

```groovy
implementation "androidx.lifecycle:lifecycle-extensions:2.0.0"
```

Any Android project needs an architectural pattern to follow. [Google recommends using MVVM](https://developer.android.com/jetpack/docs/guide) with the Android [ViewModel](https://developer.android.com/topic/libraries/architecture/viewmodel). `ViewModelProviders` enables you to quickly setup `ViewModel`s in your Activities and Fragments in a Google-approved way.

### Material

You can get by without many of the [Material Design components](https://material.io/develop/android/), but it's easier to use them. They support some very useful UI elements that Android users are used to seeing.

```groovy
implementation 'com.google.android.material:material:1.0.0'
```

For me, this library allowed me to use [`TextInputLayout`](https://developer.android.com/reference/com/google/android/material/textfield/TextInputLayout) for my text fields. This enables built in error fields, animated hints, and a Material-first design that I wanted in my app.

## Unit Tests

Using `testImplementation` specifies dependencies for your Unit Tests. JUnit is included by default.

### Mockito

The included `ExampleUnitTest` in any Android beginner project doesn't use [Mockito](https://site.mockito.org/) to test. However, as projects grow, you'll quickly realize the benefits of a full-fledged mocking library.

```groovy
testImplementation 'org.mockito:mockito-core:2.19.0'
```

Mockito enables you to use the `MockitoJUnitRunner` and to mock data and function calls in your test. It really helps to reduce boiler plate and focus on the right functionality in your unit tests.


### Lifecycle Testing

If you're doing the right thing and following MVVM with the Android Architecture Components, then you'll probably want to test your `ViewModel`s and `LiveData`.

```groovy
testImplementation 'androidx.arch.core:core-testing:2.0.1'
```

The core-testing library enables you to test `LiveData` in your JUnit tests using the `InstrumentationTestRegistry` rule.

```kotlin
/**
 * Enables Architecture Components testing in JUnit tests.
 * (Include this at the top of your Unit Test class body.)
 */
@get:Rule
var task = InstantTaskExecutorRule()
```

## UI Testing

The `androidTestImplementation` keyword refers to UI/Instrumentation test dependencies.

### Android JUnit

While you don't need this to use `AndroidJUnit4`, this dependency includes the latest, non-deprecated version.

```groovy
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
```

## What's next?

Hopefully this guide has helped you identify some foundational dependencies you'll need for almost any Android project.

It's impossible to be prescriptive about what dependencies you'll need to explore the infinite possibilities available to you in your next Android project.  What's next is up to you.

<small><i>This article is republished from my blog, [AJ.dev](https://ajkueterman.dev) - [read it there](https://ajkueterman.dev/posts/your-first-android-libraries/)<i/>.</small>