简体   繁体   English

我可以将使用 android 支持的库与 Androidx 项目一起使用吗?

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

I know, androidx and support dependency causing multidex error We can not use androidx and android support at a same time.我知道, androidx 和 support 依赖导致 multidex 错误我们不能同时使用 androidx 和 android support。 So I totally migrate to androidx.所以我完全迁移到androidx。 but one of my dependency lib used android support "lottie".但是我的一个依赖库使用了 android 支持“lottie”。

What can we do in above situation?在上述情况下我们能做什么? Should I remove 'lottie' from my project.我应该从我的项目中删除“lottie”吗?

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

You can enable Jetifier on your project, which will basically exchange the Android Support Library dependencies in your project dependencies with AndroidX -ones.您可以在您的项目上启用Jetifier ,它基本上会将您的项目依赖项中的Android Support Library依赖项与AndroidX (eg Your Lottie dependencies will be changed from Support to AnroidX) (例如,您的 Lottie 依赖项将从 Support 更改为 AnroidX)

From the Android Studio Documentation ( https://developer.android.com/studio/preview/features/ ):从 Android Studio 文档( 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 Gradle 插件提供了以下全局标志,您可以在 gradle.properties 文件中设置它们:

  • android.useAndroidX: When set to true, this flag indicates that you want to start using AndroidX from now on. android.useAndroidX:当设置为true时,这个标志表示你想从现在开始使用AndroidX。 If the flag is absent, Android Studio behaves as if the flag were set to false.如果该标志不存在,Android Studio 的行为就好像该标志设置为 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. android.enableJetifier:设置为 true 时,此标志表示您希望工具支持(来自 Android Gradle 插件)自动转换现有的第三方库,就好像它们是为 AndroidX 编写的一样。 If the flag is absent, Android Studio behaves as if the flag were set to false.如果该标志不存在,Android Studio 的行为就好像该标志设置为 false。

Precondition for Jetifier: Jetifier 的先决条件:

  • you have to use at least Android Studio 3.2你必须至少使用Android Studio 3.2

To enable jetifier, add those two lines to your gradle.properties file:要启用 jetifier,请将这两行添加到您的gradle.properties文件中:

android.useAndroidX=true
android.enableJetifier=true

Finally, please check the release notes of AndroidX, because jetifier has still some problems with some libraries (eg Dagger Android): https://developer.android.com/topic/libraries/support-library/androidx-rn最后,请查看 AndroidX 的发行说明,因为jetifier对某些库(例如 Dagger Android)还有一些问题: https ://developer.android.com/topic/libraries/support-library/androidx-rn

Manually adding android.useAndroidX=true and android.enableJetifier=true giving me hard time.手动添加android.useAndroidX=trueandroid.enableJetifier=true让我很难过。 Because it's throw some error or Suggestion: add 'tools:replace="android:appComponentFactory"' to <application>因为它抛出了一些错误或Suggestion: add 'tools:replace="android:appComponentFactory"' to <application>

To Enable Jet-fire in project there is option in android Studio要在项目中启用 Jet-fire,在 android Studio 中有一个选项

Select Your Project ---> Right Click选择您的项目--->右键单击

app----> Refactor ----> Migrate to AndroidX应用---->重构---->迁移到AndroidX

Shown in below image:-如下图所示:-

在此处输入图像描述

After click on Migrate to AndroidX .点击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 .进行重构后检查你的 gradle.properties 有android.useAndroidX=trueandroid.enableJetifier=true If they are not then add these two lines to your gradle.properties file:如果不是,则将这两行添加到您的 gradle.properties 文件中:

android.useAndroidX=true
android.enableJetifier=true

Note:- Upgrading using Android Studio, this option works if you have android studio 3.2 and onward.注意:- 使用 Android Studio 升级,如果您有 android studio 3.2 及更高版本,则此选项有效。 Check this检查这个

You need not to worry你不必担心

Just enable Jetifier in your projet.只需在您的项目中启用Jetifier

  • Update Android Studio to 3.2.0 or newer.Android Studio 更新到 3.2.0 或更高版本。
  • Open gradle.properties and add below two lines.打开gradle.properties并添加以下两行。

     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).它会在运行时将您依赖的所有支持库转换为 AndroidX(您可能有编译时错误,但应用程序会运行)。

Add the lines in the gradle.properties filegradle.properties文件中添加行

android.useAndroidX=true
android.enableJetifier=true

在此处输入图像描述 在此处输入图像描述 Refer also https://developer.android.com/jetpack/androidx另请参阅https://developer.android.com/jetpack/androidx

I added below two lines in gradle.properties file我在 gradle.properties 文件中添加了以下两行

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.并且从 AppCompactActivity 扩展的类添加了以下行。 (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) (对于这些错误,您只需在 android studio 中按 alt+enter 即可为您导入正确的库。这样您可以解决所有错误)

import androidx.appcompat.app.AppCompatActivity;

In your xml file if you have used any在您的 xml 文件中,如果您使用过任何

<android.support.v7.widget.Toolbar 

replace it with androidx.appcompat.widget.Toolbarandroidx.appcompat.widget.Toolbar替换它

then in your java code然后在你的java代码中

import androidx.appcompat.widget.Toolbar;

Open gradle.properties and add below two lines.打开 gradle.properties 并添加以下两行。

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 Activityandroid.intent.category.LAUNCHER Activity 上添加android:exported="true"后,我的问题得到了解决

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

I used these two lines of code in application tag in manifest.xml and it worked.我在 manifest.xml 中的应用程序标记中使用了这两行代码,它起作用了。

 tools:replace="android:appComponentFactory"    
 android:appComponentFactory="whateverString"

Source: https://github.com/android/android-ktx/issues/576#issuecomment-437145192来源: https ://github.com/android/android-ktx/issues/576#issuecomment-437145192

API 29.+ usage AndroidX libraries. API 29.+ 使用 AndroidX 库。 If you are using API 29.+, then you cannot remove these.如果您使用的是 API 29.+,则无法删除这些。 If you want to remove AndroidX, then you need to remove the entire 29.+ API from your SDK:如果要删除 AndroidX,则需要从 SDK 中删除整个 29.+ API:

SDK 设置

This will work fine.这将正常工作。

如果您的项目不是 AndroidX(意思是 Appcompat)并出现此错误,请尝试降级触发此错误的依赖项版本,在我的情况下为 play-services-location ("implementation 'com.google.android.gms:play-services-location :17.0.0'") ,我通过降级到 com.google.android.gms:play-services-location:16.0.0' 解决了这个问题

我之前遇到过这样的问题,它是 gradle.properties 文件不存在,只有 gradle.properties.txt ,所以我去了我的项目文件夹,我复制并粘贴了 gradle.properties.txt 文件但没有 . txt 扩展名,然后它终于起作用了。

  1. In the project folder, in build.gradle(Module:Application)在项目文件夹中,在 build.gradle(Module:Application)

    • Fix compileSdkVersion to 28将 compileSdkVersion 修复为 28
    • If targetSdkVersion is defined, change this to 28如果定义了 targetSdkVersion,则将其更改为 28
    • After modifying like that, if you click Sync Now at the top, an error will appear.像这样修改后,如果你点击顶部的立即同步,就会出现错误。
  2. In the Refactor menu, click Migration to AndroidX to proceed with the migration.在 Refactor 菜单中,单击 Migration to AndroidX 以继续迁移。

  3. After that, if you open gradle.properties in the Project folder, you will see the definition as below之后,如果您在 Project 文件夹中打开 gradle.properties,您将看到如下定义

    android.enableJetifier=true android.enableJetifier=true
    android.useAndroidX=true android.useAndroidX=true

  • Add the lines in the gradle.properties filegradle.properties文件中添加行

    android.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 on AndroidManifest.xml file.如果您在构建 apk 时遇到任何错误,则必须通过在AndroidManifest.xml文件的所有活动中添加android:exported="true"标记来导出活动。

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

Very simple.很简单。 Click on Refactor, Migrate to androidX and then follow the prompts.单击重构,迁移到 androidX,然后按照提示进行操作。 在此处输入图像描述

Comment This Line in gradle.propertiesgradle.properties中评论这一行

android.useAndroidX=true android.useAndroidX=true

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以将使用 AndroidX 的库与 android 支持项目一起使用吗? - Can I use a Library that used AndroidX with an android support project? 我可以将在我的应用程序中使用AndroidX库的库与Android支持库一起使用吗? - Can I use a library that used AndroidX libs in my App with Android-support libs? 错误:FirebasePerformancePlugin 只能用于 Android 应用程序项目。 不能在图书馆? - ERROR: FirebasePerformancePlugin must only be used with Android application projects. can't in Library? 如何使AndroidX成为新Android项目的默认库? - How can I make AndroidX the default library for new Android projects? 我可以将使用 Androidx 的库与支持库项目一起使用吗? - Can I use library that uses Androidx with support library project? 我可以将 Android 支持库从 27.1.1 更新到 AndroidX 1.0.0 吗? - Can I update Android Support Library from 27.1.1 to AndroidX 1.0.0? 当我从Android迁移到AndroidX时,在我的项目中实施Google Play服务时出错。 可能是什么解决方案? - While I migrate from Android to AndroidX , Error in implementing google play services in my projects. What might be the solution? 为什么Android Studio强制使用Android支持库中的Androidx? - Why Android Studio is forcing to use Androidx from Android support library? 在android项目和库中使用支持库(Android Studio) - Use support library in android projects and library (Android Studio) Android X Projects可以使用使用支持库依赖项的库模块吗? - Can Android X Projects use library modules that are using support-library dependencies?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM