Can I use library that used android support with Androidx projects.
AndroidAndroid JetpackAndroid Problem Overview
I know, androidx and support dependency causing multidex error We can not use androidx and android support at a same time. So I totally migrate to androidx. but one of my dependency lib used android support "lottie".
What can we do in above situation? Should I remove 'lottie' from my project.
below is my gradle
defaultConfig {
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
multiDexEnabled true
}
ext{
lottieVersion = "2.5.4"
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
def androidx = "1.0.0-rc01"
api "androidx.constraintlayout:constraintlayout:1.1.2"
api "androidx.appcompat:appcompat:$androidx"
api "androidx.recyclerview:recyclerview:$androidx"
api "androidx.cardview:cardview:$androidx"
api "androidx.core:core-ktx:$androidx"
api "com.google.android.material:material:1.0.0-rc01"
implementation "com.google.code.gson:gson:2.8.5"
implementation "androidx.multidex:multidex:2.0.0"
implementation "com.airbnb.android:lottie:$lottieVersion"
}
Android Solutions
Solution 1 - Android
You can enable Jetifier
on your project, which will basically exchange the Android Support Library
dependencies in your project dependencies with AndroidX
-ones. (e.g. Your Lottie dependencies will be changed from Support to AnroidX)
From the Android Studio Documentation (https://developer.android.com/studio/preview/features/):
> The Android Gradle plugin provides the following global flags that you > can set in your gradle.properties file: > > - android.useAndroidX: When set to true, this flag indicates that you want to start using AndroidX from now on. If the flag is absent, > Android Studio behaves as if the flag were set to false. > - android.enableJetifier: When set to true, this flag indicates that you want to have tool support (from the Android Gradle plugin) to > automatically convert existing third-party libraries as if they were > written for AndroidX. If the flag is absent, Android Studio behaves as > if the flag were set to false.
Precondition for Jetifier:
- you have to use at least
Android Studio 3.2
To enable jetifier, add those two lines to your gradle.properties
file:
android.useAndroidX=true
android.enableJetifier=true
Finally, please check the release notes of AndroidX, because jetifier
has still some problems with some libraries (e.g. Dagger Android): https://developer.android.com/topic/libraries/support-library/androidx-rn
Solution 2 - Android
Manually adding android.useAndroidX=true
and android.enableJetifier=true
giving me hard time. Because it's throw some error or Suggestion: add 'tools:replace="android:appComponentFactory"' to <application>
To Enable Jet-fire in project there is option in android Studio
Select Your Project ---> Right Click
app----> Refactor ----> Migrate to AndroidX
Shown in below image:-
After click on Migrate to AndroidX.
It will ask for confirmation and back up for your project.
And last step it will ask you for to do refactor.
After doing Refactor check your gradle.properties have android.useAndroidX=true
and android.enableJetifier=true
. If they are not then add these two lines to your gradle.properties file:
android.useAndroidX=true
android.enableJetifier=true
> Note:- Upgrading using Android Studio, this option works if you have > android studio 3.2 and onward. Check this
Solution 3 - Android
You need not to worry
Just enable Jetifier in your projet.
-
Update Android Studio to 3.2.0 or newer.
-
Open
gradle.properties
and add below two lines.android.enableJetifier=true android.useAndroidX=true
It will convert all support libraries of your dependency to AndroidX at run time (you may have compile time errors, but app will run).
Solution 4 - Android
Add the lines in the gradle.properties file
android.useAndroidX=true
android.enableJetifier=true
Solution 5 - Android
I added below two lines in gradle.properties file
android.useAndroidX=true
android.enableJetifier=true
then I got the following error
error: package android.support.v7.app does not exist
import android.support.v7.app.AlertDialog;
^
I have removed the imports and added below line
import static android.app.AlertDialog.*;
And the classes which are extended from AppCompactActivity, added the below line. (For these errors you just need to press alt+enter in android studio which will import the correct library for you. Like this you can resolve all the errors)
import androidx.appcompat.app.AppCompatActivity;
In your xml file if you have used any
<android.support.v7.widget.Toolbar
replace it with androidx.appcompat.widget.Toolbar
then in your java code
import androidx.appcompat.widget.Toolbar;
Solution 6 - Android
Open gradle.properties and add below two lines.
android.enableJetifier=true
android.useAndroidX=true
And you will face a new issues, here is the solution. My problem was solved after adding android:exported="true" on android.intent.category.LAUNCHER Activity
<activity
android:name=".MainActivity"
android:exported="true"> // Add this line
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Solution 7 - Android
I used these two lines of code in application tag in manifest.xml and it worked.
tools:replace="android:appComponentFactory"
android:appComponentFactory="whateverString"
Source: https://github.com/android/android-ktx/issues/576#issuecomment-437145192
Solution 8 - Android
API 29.+ usage AndroidX libraries. If you are using API 29.+, then you cannot remove these. If you want to remove AndroidX, then you need to remove the entire 29.+ API from your SDK:
This will work fine.
Solution 9 - Android
If your project is not AndroidX (mean Appcompat) and got this error, try to downgrade dependencies versions that triggers this error, in my case play-services-location ("implementation 'com.google.android.gms:play-services-location:17.0.0'") , I solved the problem by downgrading to com.google.android.gms:play-services-location:16.0.0'
Solution 10 - Android
I had a problem like this before, it was the gradle.properties file doesn't exist, only the gradle.properties.txt , so i went to my project folder and i copied & pasted the gradle.properties.txt file but without .txt extension then it finally worked.
Solution 11 - Android
-
In the project folder, in build.gradle(Module:Application)
- Fix compileSdkVersion to 28
- If targetSdkVersion is defined, change this to 28
- After modifying like that, if you click Sync Now at the top, an error will appear.
-
In the Refactor menu, click Migration to AndroidX to proceed with the migration.
-
After that, if you open gradle.properties in the Project folder, you will see the definition as below
android.enableJetifier=true
android.useAndroidX=true
Solution 12 - Android
-
Add the lines in the
gradle.properties
fileandroid.useAndroidX=true android.enableJetifier=true
-
If you are getting any errors while building the apk then you have to export the activities by adding
android:exported="true"
tag in all activities onAndroidManifest.xml
file.<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.examplerealtime"> <uses-permission android:name="android.permission.INTERNET"/> <application ... > <activity android:name=".MainActivity" ... android:exported="true"> ... </activity> </application> </manifest>
Solution 13 - Android
Comment This Line in gradle.properties
> android.useAndroidX=true