Skip to content
Easily add app context to your android API calls
Branch: master
Clone or download
Latest commit e2d8d5e May 3, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
extras
gradle/wrapper
library
sample
.gitignore
README.md
build.gradle
gradle.properties
gradlew
gradlew.bat
settings.gradle

README.md

UserAgentForAndroid

Identify important app information on the User-agent header

Important part:

User-Agent: DeliveryApp/1.0.0 (Android Pie 9.0; PIXEL3 XL)

Usage:

val userAgent = UserAgentForAndroid(
    appName = context.getString(R.string.app_name), // DeliveryApp
    appVersion = BuildConfig.VERSION_NAME           // 1.0.0
)

Just add a logging interceptor

val loggingInterceptor = HttpLoggingInterceptor().apply {
    level = HttpLoggingInterceptor.Level.HEADERS
}

Add both interceptors:

val okHttpClient = OkHttpClient.Builder()
    .addInterceptor(loggingInterceptor)
    .addInterceptor(userAgent)
    .build()

Add the OkHttpClient to your retrofit builder

val retrofit = Retrofit.Builder()
  .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
  .addConverterFactory(GsonConverterFactory.create())
  .client(okHttpClient)
  .baseUrl(BASE_URL)
  .build()

Install

Just add the following dependency in your app's build.gradle

allprojects { repositories { maven { url 'https://jitpack.io' } }}
dependencies {
    implementation 'com.github.cesarferreira:UserAgentForAndroid:1.0.1'
}

Created by

Cesar Ferreira

License

MIT © Cesar Ferreira

You can’t perform that action at this time.