Beagle gRPC for Android

Here you will find the beagle-grpc-android library to help you use gRPC in a project using Beagle in Android. This lib will hold almost all necessary configuration to add gRPC into a Beagle Android project.

Maven Central Maven Central

Requirements


Before you start to configure Beagle for your Android system, it’s important to check out if you have installed all the current versions of the following programs: ‌

  • JDK 11+ language
  • Android API level: at least 19 or higher
  • Kotlin 1.5+

Getting started


This tutorial will configure Beagle from the start.

Step 1: Add a configuration on the Gradle file in your project:

  • Add mavenCentral() into the All projects block on your Project Gradle file(build.gradle)
allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()
    }
}

Step 2: Add Kotlin Kapt plugin and 3 dependencies on your application dependency manager (gradle.gradle)

Maven Central Maven Central

  • Add the packagingOptions code block to the build.gradle file, as in the example below:

  • The version numbers listed below may have been updated. Please check the actual versions on the tags above:

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-android-extensions'
    id 'kotlin-kapt'
}

android {
    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_11
    }

    packagingOptions {
        pickFirst 'META-INF/LICENSE.md'
        pickFirst 'META-INF/LICENSE-notice.md'
    }
}

dependencies {
    kapt "br.com.zup.beagle:android-processor:${beagleVersion}"
    implementation "br.com.zup.beagle:beagle-grpc:${beagleVersion}"
}

Step 3: Create an AppHttpClientFactory

  • This class will be responsible to provide the HttpClient that will be used by Beagle to perform requests.
const val BASE_URL = "http://10.0.2.2:8080" // your gRPC host

@BeagleComponent
class AppHttpClientFactory: HttpClientFactory {
    override fun create(): HttpClient {
        return GrpcClient(BASE_URL, HttpClientDefault())
    }
}

Here, you are telling Beagle to use GrpcClient to perform network requests.

Let’s look to the constructor params of GrpcClient:

  • grpcAddress: Host of your gRPC server. Every request pointing to this host will be performed by GrpcClient.
  • customHttpClient: All requests made to another host will be performed by this HttpClient.

Therefore, a request to http://10.0.2.2:8080/foo will be performed by GrpcClient and a request to another host, for example http://127.0.1.1:5555/bar will be performed by HttpClientDefault.

The HttpClientDefault implementation can be found at Beagle-Default lib.


For more on Beagle, check out the repository.
For more on Beagle-Defaults, check out Beagle-Default section.

Last modified September 17, 2021: Reviewed Android section (#773) (e4c75e98)