Fixing some Android Jetpack backwards-compatibility Issues
Android Jetpack is a suite of libraries, tools, and guidance to help developers follow best practices, reduce boilerplate code, and write code that works consistently across Android versions and devices. Today, 84% of the top 1000 apps on Google Play rely on Jetpack.
?I used Android Jetpack in my last applications, which allows me to quickly bring our apps to life with less code. I was targeting Android API level 22 and higher (minSdkVersion 22), but our customer had new requirement, he would like to support previous Android version (minSdkVersion 17), doing this, I faced many issues and fixed them, if you have?the same requirement maybe this post can help you.
Enable multidex: modify?project/android/app/build.gradle?file to enable multidex and add the multidex library as a dependency, as shown here :
android {
? ? defaultConfig {
? ? ? ? ...
? ? ? ? multiDexEnabled true
? ? }
? ? ...
}
dependencies {
? ?implementation 'androidx.multidex:multidex:2.0.1'?
}
Your Application class must extends MultiDexApplication.
Add startup library as a dependency:
implementation "androidx.startup:startup-runtime:1.0.0"
You need to implement the?Configuration.Provider?interface in your Application class to provide a custom configuration using WorkManager's :
class MyApplication extends MultiDexApplication implements Configuration.Provider{
@Override
public Configuration getWorkManagerConfiguration() {
return new Configuration.Builder()
.setMinimumLoggingLevel(android.util.Log.DEBUG)
.build();
}
}
Add this to you AndroidManifest.xml:
? ?<!-- ...? -->
? <application
? ? ? ? android:allowBackup="true"
? ? ? ? android:icon="@mipmap/ic_launcher"
? ? ? ? android:label="@string/app_name"
? ? ? ? android:theme="@style/Theme.MyApplication"
? ? ? ? android:name=".MyApplication"
? ? ? ? >
? <!-- ...? -->
? ? ? ? ?<provider
? ? ? ? ? ? android:name="androidx.startup.InitializationProvider"
? ? ? ? ? ? android:authorities="${applicationId}.androidx-startup"
? ? ? ? ? ? tools:node="remove" />
? ? ??
? </application>
If you're using retorofit, to prevent your app crashing, you must force retrofit to use okhttp3 version 3.12.8 :
领英推荐
//retrofit
implementation 'com.google.code.gson:gson:2.8.8'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation("com.squareup.okhttp3:okhttp:3.12.8") {
force = true
}
implementation "com.squareup.okhttp3:logging-interceptor:3.12.8"
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
Modify?project/build.gradle?file, as shown here :
buildscript {
repositories {
google()
jcenter()
mavenLocal()
maven {
url "https://oss.jfrog.org/libs-snapshot"
}
maven { url 'https://storage.googleapis.com/r8-releases/raw' }
}
dependencies {
classpath 'com.android.tools:r8:3.0.73' // Must be before the //Gradle Plugin for Android.
classpath "com.android.tools.build:gradle:4.2.2"
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.38.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
If you have any questions then please feel free to drop me a line.