Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Android Studio includes Gradle, a build system for Android projects. Gradle includes dependency management, which allows us to easily include 3rd party code like OkHttp.
Related Links
-
0:00
We left off with the general knowledge of the OkHttp API.
-
0:04
We've been looking at the project page on GitHub.
-
0:06
But let's turn our attention to the project's website shown here.
-
0:12
I've included links for this in the Teachers Notes as well.
-
0:15
The project's site provides additional information about the library and
-
0:18
has some examples.
-
0:20
One thing to note that is shown here is the minimum requirements for using OkHttp.
-
0:28
We need to be using Android 2.3 and Java 1.7.
-
0:32
There are newer versions of both of these at this point, but
-
0:36
it is something to check and consider.
-
0:38
We're using Gradle for our project, and
-
0:40
if we look here in the download section, We see a Gradle statement we can use.
-
0:45
It's also listed over in their GitHub repository.
-
0:54
So let's copy this line, and head back to Android Studio.
-
1:01
In the project pane we need to expand Gradle scripts,
-
1:09
and the build.gradle file for our app.
-
1:15
This one here, we don't want the Stormy project file, we want the app file.
-
1:21
Android Studio has a main module,
-
1:23
typically called app, it has application-specific code.
-
1:27
This is the file in which we can paste our line of code from OkHttp.
-
1:32
Looking in here, though, where should It go?
-
1:34
This Gradle file doesn't look at all like Java code, right?
-
1:38
Well, it isn't.
-
1:40
Gradle is an automated build system used in Android development, and elsewhere.
-
1:45
It allows us to configure a few things, and in exchange, it does a lot of work for
-
1:50
us automatically behind the scenes.
-
1:52
It's a huge time saver, and once it is setup for
-
1:55
a specific project, we don't have to do much else.
-
1:59
Another productivity enhancement, very cool.
-
2:02
When using an outside resource, such as OkHttp,
-
2:06
our Stormy app depends on that library, Gradle excels at managing dependencies.
-
2:11
And hey, look at this,
-
2:13
there is a section in our build.gradle file called dependencies.
-
2:17
That's where we'll paste our line of code from OkHttp.
-
2:22
So we can go right down here under this last implementation,
-
2:25
and we'll paste in our line from OkHttp.
-
2:30
Let's quickly talk about this build file.
-
2:32
This Android section up here at the top provides settings for
-
2:35
things like the SDK version, and our default configuration.
-
2:40
The dependencies section, where we just added our code,
-
2:43
tells Gradle about external dependencies we'd like it to manage.
-
2:47
We can see that there is a file tree where Gradle watches for Java archive files, or
-
2:51
JAR files, in the Libs directory.
-
2:53
Some third-party libraries are still used via JAR files, and
-
2:57
Gradle can keep track of those.
-
2:59
There are some additional dependencies in here relating to testing and
-
3:02
compatibility features.
-
3:04
The format, though, is the same.
-
3:06
In our case, the com.squareup.okhttp3 tells Gradle where to find the file,
-
3:14
that OkHttp is the library to get, and that 3.10.0 is the version to use.
-
3:22
When a new version comes out, you can come back here to this build.gradle file,
-
3:26
update the version number and synchronize the new changes.
-
3:30
Which reminds me, anytime we make changes in this file, we need to sync the changes.
-
3:35
This allows Gradle to update the project, and
-
3:38
get any additional files our project may need.
-
3:41
You'll notice that Android Studio displays a warning up here about this as well.
-
3:46
We can either click the Sync Now link, Or
-
3:51
click on this button to perform a project sync.
-
3:58
After Gradle all goes and rebuilds the project,
-
4:00
which depending on your system and network can take a few moments,
-
4:03
we see a message that Gradle build finished, down here at the bottom.
-
4:08
Once we see that message, we're all set.
-
4:11
If, however,
-
4:12
you get an error, try using your Google skills to find out what happened.
-
4:16
Check the Teachers Notes to see if there are any updates, or
-
4:19
ask questions in the Treehouse forum.
-
4:22
With OkHttp installed and all set to go, we're ready to use it.
-
4:26
We head back to the OkHttp website here, We can look for the examples.
-
4:33
There's also other links to resources on this page as well,
-
4:38
such us Javadocs, there Wiki, and StackOverflow.
-
4:42
The OkHttp Wiki has some decent usage examples, and
-
4:46
I encourage you to have a look around before we come back together and
-
4:50
make our first http get request from Stormy.
You need to sign up for Treehouse in order to download course files.
Sign up