Check out a free preview of the full Introduction to Kotlin and Android Development course

The "Adding Dependencies" Lesson is part of the full, Introduction to Kotlin and Android Development course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano demonstrates calling the web service using HTTP(s) with the RetroFit, OkHttp, and JsonConverter libraries. The AsyncImage from Coil will also be used to download images from the web.

Preview
Close

Transcript from the "Adding Dependencies" Lesson

[00:00:00]
>> So, what's the next step? Let's try to download real data from the server. So here I have bad news. The bad news is, and again, I've been an Android developer for ten years, and I always hated this part. The Android ecosystem has no automatic encloses built-in simple solution to make HTTP requests.

[00:00:30]
Everything that the OS is offering to us is really low level, such as we need to go to the network and download the responses in chunks, in bytes. So we need to create the buffer with the chunks of bytes that are coming from the TCP stack, storing that.

[00:00:46]
So it's a mess, and we always needed a library. There are a lot of libraries. There is no single library that says, this is the jQuery of Android development. No, we don't have that. There are so many. I just picked one for us to play with, and that's an external dependency.

[00:01:06]
So far, we didn't have the need for an external dependency. So it's the first time we're going to add a dependency. Where are the dependencies in our project? If you open the project explorer, there is a section here known as Gradle Scripts, remember that? On Gradle Scripts, it's funny that there are two files with the same name, build.gradle, okay?

[00:01:33]
Actually, remember that I mentioned that this is not the real file system. This is a built on file system. They are not on the same folder, actually. So one is for the project, and another one is for the module, the module is actually the real app. So if you open the one from the module, so build.gradle module, there are many settings here, such as the minimum SDK, the target SDK, remember we mentioned that before.

[00:01:58]
And if you scroll down, there is a section for dependencies. So here, this is like package.JSON on NPM, okay? Here we can add more dependencies, how? You need to add manually the term implementation and within quote the package that you want to use. For our case, for our project today, we will go to the section working with data.

[00:02:32]
No, I'm sorry, we will go to Using APIs and JSON, Adding Dependencies. And I have here for you something to copy and paste. So these are the dependencies that we're going to use. We're going to use actually four libraries, Retrofit, OkHttp, JSON Converter, and AsyncImage. AsyncImage is a composable that we can use to actually download images automatically from the server, okay?

[00:03:03]
Yeah.
>> Did you say the module is the actual?
>> The module is the app. Yeah, because actually you can have your project. And then you can have an Android app for phones. Then a wearable app for Wear OS. You can have an Android TV app, all sharing the same UI elements and business logic and then you have several projects, several apps.

[00:03:29]
So we can have more than one app in the same project, okay? So I'm going to copy this part and paste this anywhere in the dependencies' code block, like so. When you do that, there is a warning here now saying hey, Gradle files have changed since last project sync.

[00:03:51]
So what we need to do is to sync now, sync it now will actually, at the bottom you will see, will build a project and we'll start downloading all the assets. This is like NPM install, okay, when you're working with NPM, okay? After it's done, then you're ready to use these dependencies.

[00:04:18]
So if you can build your app, so you go to build and you make project. Or you just build the current project or you run it, it means that he's working. If you don't get any errors, it means that all the packages were downloaded, even if you're not using them, just because you see the app running, it means that your packages were downloaded correctly.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now