Android原生项目导入flutter项目,各种问题汇集

步骤一,

使用Android Studio来创建,依次点击左上角的File --> New --> New Flutter Project,然后选择Flutter Module。

步骤二,

然后在项目根目录下的setting.gradle文件中配置:

setBinding(new Binding([gradle:this]))

//flutter目录

evaluate(new File(

settingsDir.parentFile,

'my_flutter/.android/include_flutter.groovy'

))

报错: org.gradle.api.GradleScriptException: A problem occurred evaluating settings

解决方式:网上找了一圈没看到明确的路径说明,正确的是:

include ':app'

include ':flutter_module'

setBinding(new Binding([gradle: this]))

evaluate(new File(

        settingsDir.parentFile,

        '你的主项目名称/flutterMoudle的名称/.android/include_flutter.groovy'

))

报错:Project :app doesn't exist. To custom the host app project name, set `org.gradle.project.flutter.hostAppProjectName=` in gradle.properties.. Expression: (appProject != null). Values: appProject = null

解决方式:出现这个错误的原因是:你的android项目的主module的名字不是app,修改方式:

官方解释:https://flutter.cn/docs/development/add-to-app/android/project-setup

在android项目的gradle.properties文件中设置 flutter.hostAppProjectName:

flutter.hostAppProjectName = app_name

其中app_name就是你android项目主module的名字。

运行报错:导入flutter后  Kotlin出现  'onAttach' overrides nothing

以及Type mismatch: inferred type is Context? but Context was expected

解决办法  据说是compileSdkVersion 升级到27,出现如题错误,

override fun onAttach(context: Context?) {

super.onAttach(context)

if (contextis VectorSettingsFragmentInteractionListener) {

interactionListener =context

}}

这样的就没有问题,只要将 context: Context?后边的问号去掉即可,为什么会这样,我也不清楚

运行报错:ERROR: Manifest merger failed with multiple errors, see logs。

具体查看是 This version of flutter_webview_plugin will break your Android build if it or its dependencies aren't compatible with AndroidX.

然后网上各种找答案,说什么的都有,就是不断的解决Flutter中解决AndroidX包与Support包冲突问题  

修改版本号,升级Androidx的包,

就是说把当前工程合并为AndroidX

I在 android/gradle/wrapper/gradle-wrapper.properties 修改成这样:

distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip

在I android/build.gradle, 修改成这样:

dependencies {

    classpath 'com.android.tools.build:gradle:3.3.0'

}

在I android/gradle.properties,中添加

android.enableJetifier=true

android.useAndroidX=true

修改 android/app/build.gradle:版本号28

make sure compileSdkVersion and targetSdkVersion are at least 28.

android/app/build.gradle中的dependencies里,用androidx代替android.support

将 androidTestImplementation 'com.android.support.test:runner:1.0.2'

修改为 androidTestImplementation 'androidx.test:runner:1.0.2'

尝试到最后,没有一点卵用 折腾了一上午,

回到原点,ERROR: Manifest merger failed with multiple errors, see logs。

特别感谢:https://www.huaweicloud.com/articles/73372d9fcac652ddc86178c30f99a36a.html

打开主app的AndroidManifest.xml,   以前没有注意到这里还有一个“Merged Manifest” 的 tab页。

点击后就真正的错误log就出来了。这里的Error 提示直指错误的!


Error: Attribute meta-data#android.support.FILE_PROVIDER_PATHS@resource value=(@xml/vector_provider_paths) from AndroidManifest.xml:583:17-62 is also present at AndroidManifest.xml:7:75-108 value=(@xml/filepaths). Suggestion: add 'tools:replace="android:resource"' to element at AndroidManifest.xml to override. vector app manifest, line 582

最后才知道就是 flutter_webview_plugin 中多写了配置文件,需要重新覆盖 即可,真实日了狗,

添加如图,完美运行;

你可能感兴趣的:(Android原生项目导入flutter项目,各种问题汇集)