最近使用flutter 做了一些简单的东西在练手,因为导入了一个库,爆出了:

Manifest merger failed :‘tools:replace=“android:appComponentFactory”’ to element at AndroidManifest.xml(具体错误在下面)
或者是这个错误
[!] Your app isn’t using AndroidX.
To avoid potential build failures, you can quickly migrate your app by following the steps on https://goo.gl/CP92wY.

Manifest merger failed : Attribute application@appComponentFactory value=(android.support.v4.app.CoreComponentFactory) from [com.android.support:support-compat:28.0.0] AndroidManifest.xml:22:18-91
    is also present at [androidx.core:core:1.0.0] AndroidManifest.xml:22:18-86 value=(androidx.core.app.CoreComponentFactory).
    Suggestion: add 'tools:replace="android:appComponentFactory"' to <application> element at AndroidManifest.xml:36:5-364:19 to override.

其实就是项目导入的库使用了androidx,androidx和supper冲突了,一般情况的话,按照谷歌的文档就能搞定。但是android studio却弹出No Usages Found in the Project这个提示。看来自动搞不定了,手动搞一下吧。

首先在android的src目录下的build.gradle添加androidx的依赖(你也可以使用as新建支持androidx的新项目,使用新的版本)

   implementation 'androidx.appcompat:appcompat:1.1.0'
   implementation 'androidx.core:core-ktx:1.1.0'
   androidTestImplementation 'androidx.test.ext:junit:1.1.0'
   androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'

然后在android目录下的gradle.properties文件下添加

android.enableJetifier=true
android.useAndroidX=true

第三步,点击Tools ->Flutter->Flutter Clean。然后跑一下项目应该是可以跑起来了。

但是我的项目之前是用dio网络框架。又报出了这个错误
Observatory server failed to start after 1 tries
[ERROR:flutter/lib/ui/ui_dart_state(148)] Unhandled Exception: DioError [DioErrorType.DEFAULT]: SocketException: Failed host

 [ERROR:flutter/lib/ui/ui_dart_state(148)] Unhandled Exception: DioError [DioErrorType.DEFAULT]: SocketException: Failed host 

这种情况在AndroidManifest申请权限网络就好了

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

有一点疑惑,为啥之前网络权限没在AndroidManifest申请,可以能访问网络。现在升级到adnroidx,就不行了。

更多推荐

flutter 升级androidX的坑记录