[英]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 的先决条件:
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=true
和android.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=true
和android.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 。
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 file在gradle.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.Toolbar
用androidx.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 Activity在android.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
如果您的项目不是 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 扩展名,然后它终于起作用了。
In the project folder, in build.gradle(Module:Application)在项目文件夹中,在 build.gradle(Module:Application)
In the Refactor menu, click Migration to AndroidX to proceed with the migration.在 Refactor 菜单中,单击 Migration to AndroidX 以继续迁移。
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
file在gradle.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>
Comment This Line in gradle.properties
在gradle.properties
中评论这一行
android.useAndroidX=true android.useAndroidX=true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.