DEV Community

Cover image for Your First Android Libraries
A.J. Kueterman
A.J. Kueterman

Posted on

Your First Android Libraries

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.

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'

}
Enter fullscreen mode Exit fullscreen mode

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. Namely, the ability to use ViewModelProviders.

implementation "androidx.lifecycle:lifecycle-extensions:2.0.0"
Enter fullscreen mode Exit fullscreen mode

Any Android project needs an architectural pattern to follow. Google recommends using MVVM with the Android ViewModel. ViewModelProviders enables you to quickly setup ViewModels in your Activities and Fragments in a Google-approved way.

Material

You can get by without many of the Material Design components, but it's easier to use them. They support some very useful UI elements that Android users are used to seeing.

implementation 'com.google.android.material:material:1.0.0'
Enter fullscreen mode Exit fullscreen mode

For me, this library allowed me to use 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 to test. However, as projects grow, you'll quickly realize the benefits of a full-fledged mocking library.

testImplementation 'org.mockito:mockito-core:2.19.0'
Enter fullscreen mode Exit fullscreen mode

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 ViewModels and LiveData.

testImplementation 'androidx.arch.core:core-testing:2.0.1'
Enter fullscreen mode Exit fullscreen mode

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

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

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.

androidTestImplementation 'androidx.test.ext:junit:1.1.1'
Enter fullscreen mode Exit fullscreen mode

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.

This article is republished from my blog, AJ.dev - read it there.

Top comments (0)