Can I use library that used android support with Androidx projects.

偶尔善良 提交于 2019-11-26 12:15:30

问题


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\"
    }

回答1:


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




回答2:


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



回答3:


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




回答4:


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




回答5:


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.



来源:https://stackoverflow.com/questions/52033810/can-i-use-library-that-used-android-support-with-androidx-projects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!