Identify important app information on the
User-agentheader
Important part:
User-Agent: DeliveryApp/1.0.0 (Android Pie 9.0; PIXEL3 XL)
val userAgent = UserAgentForAndroid(
appName = context.getString(R.string.app_name), // DeliveryApp
appVersion = BuildConfig.VERSION_NAME // 1.0.0
)val loggingInterceptor = HttpLoggingInterceptor().apply {
level = HttpLoggingInterceptor.Level.HEADERS
}Add both interceptors:
val okHttpClient = OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.addInterceptor(userAgent)
.build()val retrofit = Retrofit.Builder()
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.baseUrl(BASE_URL)
.build()If you want to access any of this information:
DeviceInfo.getDeviceName() // Pixel 3 XL
DeviceInfo.currentAndroidName() // Pie
DeviceInfo.androidReleaseNumber() // 9.0Just 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'
}MIT © Cesar Ferreira
