Android Kotlin Web Services With Retrofit PART 2
Retrofit Library
All External libraries or third party libraries(Retrofit) are like extensions to the core Android APIs. They are mostly open source, community-developed, and maintained by the collective contributions from the huge android community around the world. This enables Android developers like you to build better apps.
Retrofit library will communicate with the backend. It creates URI's for the web service based on the parameters we pass to it. You will see more on this in later sections.
Add Retrofit dependencies
Android Gradle allows you to add external libraries to your project. In addition to the library dependency, you should also include the repository where the library is hosted. The Google libraries such as ViewModel and LiveData from the Jetpack library are hosted in the Google repository. The majority of community libraries are hosted on JCenter like Retrofit.
- Open the project's top-level level
build.gradle(Project)file. Notice the repositories listed under therepositoriesblock. You should see two repositories,google(),mavenCentral().
- Open module level gradle file,
build.gradle (Module: MarsPhots.app). - In the
dependenciessection, add these lines for the Retrofit libraries:
The first dependency is for the Retrofit2 library itself, and the second dependency is for the Retrofit scalar converter. This converter enables Retrofit to return the JSON result as a String. The two libraries work together.
- Click Sync Now to rebuild the project with the new dependencies.
Add support for Java 8 language features
- To use the built-in features, you need the following code in your module's
build.gradlefile. This step is already done for you, make sure the following code is present in yourbuild.gradle(Module: MarsPhotos.app).
android { ...
compileOptions { sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility targetCompatibility JavaVersion.VERSION_1_8
} kotlinOptions { jvmTarget = '1.8' }}Many third party libraries including Retrofit2 use Java 8 language features. The Android Gradle plugin provides built-in support for using certain Java 8 language features.