You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Flutter安卓项目集成native_camera_sound包时Gradle构建失败的解决求助

Flutter安卓项目集成native_camera_sound包时Gradle构建失败的解决求助

我最近在Flutter安卓项目里想实现播放原生相机快门声音的功能,于是安装了native_camera_sound: ^0.0.4包,但真机运行时直接触发了Gradle构建失败,换用audioplayers包也遇到了同样的核心错误,实在卡壳了,来求助各位大佬!

完整报错信息

FAILURE: Build completed with 2 failures.

1: Task failed with an exception.
-----------
* Where:
Build file 'C:\Users\jstua\AppData\Local\Pub\Cache\hosted\pub.dev\native_camera_sound-0.0.4\android\build.gradle' line: 25

* What went wrong:
A problem occurred evaluating project ':native_camera_sound'.
> Failed to apply plugin 'kotlin-android'.
   > Cannot add extension with name 'kotlin', as there is an extension already registered with that name.

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to generate a Build Scan (Powered by Develocity).
> Get more help at https://help.gradle.org.
==============================================================================

2: Task failed with an exception.
-----------
* What went wrong:
A problem occurred configuring project ':native_camera_sound'.
> Failed to notify project evaluation listener.
   > java.lang.NullPointerException (no error message)
   > Android Gradle Plugin: project ':native_camera_sound' does not specify `compileSdk` in build.gradle (C:\Users\jstua\AppData\Local\Pub\Cache\hosted\pub.dev\native_camera_sound-0.0.4\android\build.gradle).

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to generate a Build Scan (Powered by Develocity).
> Get more help at https://help.gradle.org.
==============================================================================

BUILD FAILED in 9s

┌─ Flutter Fix ────────────────────────────────────────────────────────────────────────────────────┐
│ [!] Starting AGP 9+, only the new DSL interface will be read. This results in a build failure    │
│ when applying the Flutter Gradle plugin at                                                       │
│ C:\WorkingFolder\Projects\Desytec\Development\Desytec-Security-Platform\Mobile\DCoreSecurity\dco │
│ re_security\android\app\build.gradle.kts.                                                        │
│                                                                                                  │
│ To resolve this update flutter or opt out of `android.newDsl`. For instructions on how to opt    │
│ out, see: https://docs.flutter.dev/release/breaking-changes/migrate-to-agp-9                     │
│                                                                                                  │
│ If you are not upgrading to AGP 9+, run `flutter analyze --suggestions` to check for             │
│ incompatible dependencies.                                                                       │
└──────────────────────────────────────────────────────────────────────────────────────────────────┘
Error: Gradle task assembleDebug failed with exit code 1

我已经尝试过的操作

  • 卸载重装native_camera_soundaudioplayers
  • 执行flutter clean + flutter pub get清理缓存并重新拉取依赖
  • 检查项目的Kotlin和Gradle版本,确保和当前Flutter版本匹配

但问题依然没有解决,现在想请教各位:

具体解决思路和疑问

  1. AGP9+兼容性问题
    从Flutter Fix的提示来看,项目用了AGP9+,但旧插件不兼容新DSL。如果我在android/gradle.properties里添加android.newDsl=false临时禁用新DSL,会不会对后续项目的构建或升级有潜在影响?

  2. 手动修复第三方包的Gradle配置
    我发现native_camera_sound-0.0.4build.gradle确实没指定compileSdk,还重复应用了Kotlin插件。手动修改本地缓存的这个文件(添加compileSdk 34、注释掉apply plugin: 'kotlin-android')能临时解决,但清理Pub缓存后就会失效,有没有更持久的解决方式?

  3. 替代方案推荐
    native_camera_sound停更很久了,有没有维护更活跃的替代包?或者有没有直接用Flutter调用安卓原生API播放相机声音的简单方法?

另外,为什么换用audioplayers也会触发同样的Kotlin扩展重复注册错误?是不是我的项目全局Kotlin配置哪里有问题?

火山引擎 最新活动