You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter集成tflite包编译报错:找不到compile()方法

实时目标检测Flutter应用构建失败:tflite包依赖错误

问题描述

使用tflite(1.1.2)和camera(0.10.3)开发Flutter实时目标检测应用,执行构建时失败,核心错误为:
A problem occurred evaluating project ':tflite'.
> Could not find method compile() for arguments [org.tensorflow:tensorflow-lite:+] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

完整错误日志

FAILURE: Build failed with an exception.

* Where:
Build file 'C:\Users\dlprw\AppData\Local\Pub\Cache\hosted\pub.dev\tflite-1.1.2\android\build.gradle' line: 36

* What went wrong:
A problem occurred evaluating project ':tflite'.
> Could not find method compile() for arguments [org.tensorflow:tensorflow-lite:+] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

* 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 get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 2s

配置文件信息

pubspec.yaml

name: captureit
description: A new Flutter project.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev


version: 1.0.0+1

environment:
  sdk: '>=3.1.5 <4.0.0'


dependencies:
  flutter:
    sdk: flutter
  gallery_saver: ^2.3.2
  image_picker: ^0.8.7+1
  loading_animation_widget: ^1.2.0+4
  path_provider: ^2.0.14
  tflite: ^1.1.2
  camera: ^0.10.3
  debounce_throttle: ^2.0.0
  cupertino_icons: ^1.0.2

dev_dependencies:
  flutter_test:
    sdk: flutter


  flutter_lints: ^2.0.0

flutter:


  uses-material-design: true

android/app/build.gradle

plugins {
    id "com.android.application"
    id "kotlin-android"
    id "dev.flutter.flutter-gradle-plugin"
}

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

android {
    namespace "com.example.captureit"
    compileSdkVersion flutter.compileSdkVersion
    ndkVersion flutter.ndkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.example.captureit"
        // You can update the following values to match your application needs.
        // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
        minSdkVersion flutter.minSdkVersion
        targetSdkVersion flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {

    implementation 'org.tensorflow:tensorflow-lite:+'
    implementation 'org.tensorflow:tensorflow-lite-gpu:+'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'org.tensorflow:tensorflow-lite:+'
}

android/build.gradle

buildscript {
    ext.kotlin_version = '1.7.10'
    repositories {
        google()
        mavenCentral()
    }

    dependencies {

        classpath 'com.android.tools.build:gradle:7.3.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(':app')
}

tasks.register("clean", Delete) {
    delete rootProject.buildDir
}

已尝试的操作

  • 在android/app/build.gradle中添加tensorflow-lite依赖而非在android/build.gradle中配置
  • 调整build.gradle中dependencies的位置,确保不在buildscript块内添加依赖

解决方案

方案1:替换为维护活跃的tflite_flutter包

原tflite包(1.1.2)已停止维护,其Android配置使用了Gradle 7.x已废弃的compile()方法。建议替换为官方维护的tflite_flutter包:

  1. 修改pubspec.yaml,移除tflite: ^1.1.2,添加:
tflite_flutter: ^0.10.4
tflite_flutter_helper: ^0.4.0
  1. 执行flutter pub get更新依赖
  2. 适配新包的API调整检测代码

方案2:修改缓存中tflite包的build.gradle

如果坚持使用原tflite包,可手动修改缓存里的配置文件:

  1. 打开路径 C:\Users\dlprw\AppData\Local\Pub\Cache\hosted\pub.dev\tflite-1.1.2\android\build.gradle
  2. 找到第36行的compile 'org.tensorflow:tensorflow-lite:+',将compile替换为implementation
  3. 重新执行flutter run

方案3:添加Gradle兼容性配置

在项目根目录的android/build.gradle中添加兼容性规则,自动将compile映射为implementation:

  1. 修改android/build.gradle,在subprojects块中添加:
subprojects {
    project.configurations.all {
        dependencies.metaClass.compile = { args ->
            implementation(args)
        }
    }
}
  1. 清理构建缓存:flutter clean,然后重新构建

内容的提问来源于stack exchange,提问作者Adil Parwez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 19:14:56