Flutter module集成到Android原生项目报错

使用AAR方案集成遇到两个诡异错误

环境:

  • Flutter (Channel stable, 3.3.10, on macOS 13.5.2 22G91 darwin-x64, locale
    zh-Hans-CN)
  • Android Studio (version 2022.3)
  • Android toolchain - develop for Android devices (Android SDK version 34.0.0)

参考文档en
参考文档zh

  1. 生成flutter module aar
flutter build aar
  1. 按照终端提示操作集成
    ...
    同步失败
报错:Build was configured to prefer settings repositories over project repositories but repository 'maven' was added by build file 'app/build.gradle'

错误原因:
在settings.gradle里面定了repositories和maven,又在build.gradle里面定义repositorie导致冲突

解决方法:
不在build.gradle里定义repositorie,仅在settings.gradle定义

// settings.gradle
dependencyResolutionManagement {
        repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
        repositories {
            google()
            mavenCentral()
            maven {
                url 'path/to/your/flutter_module/build/host/outputs/repo'
                // This is relative to the location of the build.gradle file
                // if using a relative path.
            }
            maven {
                url 'https://storage.googleapis.com/download.flutter.io'
            }
        }
}

修正上面错误后sync成功,运行项目失败

错误:Could not find flutter-1.0-debug.jar

错误原因:
没找到之前输出打包的aar

解决方法:

dependencies {
    debugImplementation 'com.example.flutter_module:flutter:1.0:debug'
    profileImplementation 'com.example.flutter_module:flutter:1.0:profile'
    releaseImplementation 'com.example.flutter_module:flutter:1.0:release'
}
// 修正为
dependencies {
    debugImplementation 'com.example.flutter_module:flutter_debug:1.0'
    profileImplementation 'com.example.flutter_module:flutter_profile:1.0'
    releaseImplementation 'com.example.flutter_module:flutter_release:1.0'
}

修改后可以正确加载打包的aar依赖并成功运行
基本这两个错误都是flutter的文档过时了...没点安卓原生功底解决起来真的麻烦

posted @ 2023-10-10 15:59  痴人指路  阅读(213)  评论(0编辑  收藏  举报