我可以将使用 android 支持的库用于 Androidx 项目吗?
Can I use library that used android support with Androidx projects.
我知道,androidx and support dependency causing multidex error
我们不能同时使用 androidx 和 android 支持。所以我完全迁移到 androidx。但是我的一个依赖库使用 android 支持 "lottie".
遇到上述情况我们可以做什么?我应该从我的项目中删除 'lottie' 吗?
下面是我的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"
}
您可以在您的项目上启用 Jetifier
,这基本上会将项目依赖项中的 Android Support Library
依赖项与 AndroidX
-ones 交换。 (例如,您的 Lottie 依赖项将从 Support 更改为 AnroidX)
来自 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.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.
Jetifier 的先决条件:
- 你必须至少使用
Android Studio 3.2
要启用 jetifier,请将这两行添加到您的 gradle.properties
文件中:
android.useAndroidX=true
android.enableJetifier=true
最后,请查看 AndroidX 的发行说明,因为 jetifier
与某些库(例如 Dagger Android)仍然存在一些问题:https://developer.android.com/topic/libraries/support-library/androidx-rn
你不用担心
只需在您的项目中启用 Jetifier。
- Update Android Studio 到 3.2.0 或更新版本。
打开gradle.properties
并添加下面两行。
android.enableJetifier=true
android.useAndroidX=true
它会在 运行 时将你依赖的所有支持库转换为 AndroidX(你可能有编译时错误,但应用程序会 运行)。
手动添加 android.useAndroidX=true
和 android.enableJetifier=true
让我很难受。因为它抛出一些错误或 Suggestion: add 'tools:replace="android:appComponentFactory"' to <application>
要在项目中启用 Jet-fire,android Studio 中有一个选项
Select 您的项目 ---> 右击
app----> 重构----> 迁移到 AndroidX
如下图所示:-
点击Migrate to AndroidX后。
它将要求确认并备份您的项目。
最后一步它会要求您进行重构。
重构后检查你的 gradle.properties 有 android.useAndroidX=true
和 android.enableJetifier=true
。如果不是,则将这两行添加到您的 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. Check this
我在 manifest.xml 的应用程序标签中使用了这两行代码并且它起作用了。
tools:replace="android:appComponentFactory"
android:appComponentFactory="whateverString"
来源:https://github.com/android/android-ktx/issues/576#issuecomment-437145192
API 29.+ 使用 AndroidX 库。如果您使用的是 API 29.+,则无法删除这些。如果您想删除 AndroidX,则需要从您的 SDK 中删除整个 29.+ API:
这会很好用。
在 gradle.properties
中评论这一行
android.useAndroidX=true
在gradle.properties文件中添加行
android.useAndroidX=true
android.enableJetifier=true
如果您的项目不是 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 扩展名然后它终于工作了。
我在 gradle.properties 文件中添加了以下两行
android.useAndroidX=true
android.enableJetifier=true
然后我得到以下错误
error: package android.support.v7.app does not exist
import android.support.v7.app.AlertDialog;
^
我删除了导入并添加到下面一行
import static android.app.AlertDialog.*;
和从 AppCompactActivity 扩展而来的 类,添加了以下行。 (对于这些错误,您只需要在 android studio 中按 alt+enter,它将为您导入正确的库。这样您就可以解决所有错误)
import androidx.appcompat.app.AppCompatActivity;
在您的 xml 文件中,如果您使用过
<android.support.v7.widget.Toolbar
替换为androidx.appcompat.widget.Toolbar
然后在您的 java 代码中
import androidx.appcompat.widget.Toolbar;
在项目文件夹中,在build.gradle(Module:Application)
- 修复compileSdkVersion为28
- 如果定义了targetSdkVersion,将其改为28
- 这样修改后,如果点击顶部的立即同步,会出现错误。
在“重构”菜单中,单击“迁移到 AndroidX”以继续迁移。
之后,如果您在项目文件夹中打开gradle.properties,您将看到如下定义
android.enableJetifier=真
android.useAndroidX=真
打开 gradle.properties 并在下面添加两行。
android.enableJetifier=true
android.useAndroidX=true
您将面临新的问题,这里是解决方案。
在 android.intent.category.LAUNCHER Activity[= 上添加 android:exported="true" 后我的问题得到解决12=]
<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>
在gradle.properties
文件中添加行
android.useAndroidX=true
android.enableJetifier=true
如果您在构建 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>
我知道,androidx and support dependency causing multidex error 我们不能同时使用 androidx 和 android 支持。所以我完全迁移到 androidx。但是我的一个依赖库使用 android 支持 "lottie".
遇到上述情况我们可以做什么?我应该从我的项目中删除 'lottie' 吗?
下面是我的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"
}
您可以在您的项目上启用 Jetifier
,这基本上会将项目依赖项中的 Android Support Library
依赖项与 AndroidX
-ones 交换。 (例如,您的 Lottie 依赖项将从 Support 更改为 AnroidX)
来自 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.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.
Jetifier 的先决条件:
- 你必须至少使用
Android Studio 3.2
要启用 jetifier,请将这两行添加到您的 gradle.properties
文件中:
android.useAndroidX=true
android.enableJetifier=true
最后,请查看 AndroidX 的发行说明,因为 jetifier
与某些库(例如 Dagger Android)仍然存在一些问题:https://developer.android.com/topic/libraries/support-library/androidx-rn
你不用担心
只需在您的项目中启用 Jetifier。
- Update Android Studio 到 3.2.0 或更新版本。
打开
gradle.properties
并添加下面两行。android.enableJetifier=true android.useAndroidX=true
它会在 运行 时将你依赖的所有支持库转换为 AndroidX(你可能有编译时错误,但应用程序会 运行)。
手动添加 android.useAndroidX=true
和 android.enableJetifier=true
让我很难受。因为它抛出一些错误或 Suggestion: add 'tools:replace="android:appComponentFactory"' to <application>
要在项目中启用 Jet-fire,android Studio 中有一个选项
Select 您的项目 ---> 右击
app----> 重构----> 迁移到 AndroidX
如下图所示:-
点击Migrate to AndroidX后。
它将要求确认并备份您的项目。
最后一步它会要求您进行重构。
重构后检查你的 gradle.properties 有 android.useAndroidX=true
和 android.enableJetifier=true
。如果不是,则将这两行添加到您的 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. Check this
我在 manifest.xml 的应用程序标签中使用了这两行代码并且它起作用了。
tools:replace="android:appComponentFactory"
android:appComponentFactory="whateverString"
来源:https://github.com/android/android-ktx/issues/576#issuecomment-437145192
API 29.+ 使用 AndroidX 库。如果您使用的是 API 29.+,则无法删除这些。如果您想删除 AndroidX,则需要从您的 SDK 中删除整个 29.+ API:
这会很好用。
在 gradle.properties
android.useAndroidX=true
在gradle.properties文件中添加行
android.useAndroidX=true
android.enableJetifier=true
如果您的项目不是 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 扩展名然后它终于工作了。
我在 gradle.properties 文件中添加了以下两行
android.useAndroidX=true
android.enableJetifier=true
然后我得到以下错误
error: package android.support.v7.app does not exist
import android.support.v7.app.AlertDialog;
^
我删除了导入并添加到下面一行
import static android.app.AlertDialog.*;
和从 AppCompactActivity 扩展而来的 类,添加了以下行。 (对于这些错误,您只需要在 android studio 中按 alt+enter,它将为您导入正确的库。这样您就可以解决所有错误)
import androidx.appcompat.app.AppCompatActivity;
在您的 xml 文件中,如果您使用过
<android.support.v7.widget.Toolbar
替换为androidx.appcompat.widget.Toolbar
然后在您的 java 代码中
import androidx.appcompat.widget.Toolbar;
在项目文件夹中,在build.gradle(Module:Application)
- 修复compileSdkVersion为28
- 如果定义了targetSdkVersion,将其改为28
- 这样修改后,如果点击顶部的立即同步,会出现错误。
在“重构”菜单中,单击“迁移到 AndroidX”以继续迁移。
之后,如果您在项目文件夹中打开gradle.properties,您将看到如下定义
android.enableJetifier=真
android.useAndroidX=真
打开 gradle.properties 并在下面添加两行。
android.enableJetifier=true
android.useAndroidX=true
您将面临新的问题,这里是解决方案。 在 android.intent.category.LAUNCHER Activity[= 上添加 android:exported="true" 后我的问题得到解决12=]
<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>
在
gradle.properties
文件中添加行android.useAndroidX=true android.enableJetifier=true
如果您在构建 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>