Skip to content
master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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()

Extra

If you want to access any of this information:

DeviceInfo.getDeviceName()         // Pixel 3 XL
DeviceInfo.currentAndroidName()    // Pie
DeviceInfo.androidReleaseNumber()  // 9.0

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.1.0'
}

Created by

Cesar Ferreira

License

MIT © Cesar Ferreira

About

Easily add app context to your android API calls

Resources

Packages

No packages published