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

Flutter项目Kotlin代码导入TensorFlow包报'Unresolved reference: tensorflow'错误求助

Flutter项目中Kotlin代码导入TensorFlow Lite包出现'Unresolved reference: tensorflow'的解决方法

问题描述

在Flutter项目的Android模块中使用Kotlin代码集成TensorFlow Lite时,导入包出现Unresolved reference: tensorflow错误,对应的Gradle配置如下:

plugins {
    id "com.android.application"
    id "kotlin-android"
    // The Flutter Gradle Plugin must be applied after the Android and Kotlin Gradle plugins.
    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 = "x.x.xxx"
    compileSdk = flutter.compileSdkVersion
    ndkVersion = flutter.ndkVersion

    dependencies {

        implementation 'org.tensorflow:tensorflow-lite:2.16.1'
        implementation 'org.tensorflow:tensorflow-lite-gpu:2.16.1'
        implementation 'com.google.android.gms:play-services-tflite-java:16.1.0'
        implementation 'com.google.android.gms:play-services-tflite-gpu:16.2.0'
        implementation 'androidx.core:core-ktx:1.13.1'
        implementation 'com.google.android.material:material:1.12.0'
        implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
        implementation 'com.google.android.material:material:1.12.0'
        implementation 'org.tensorflow:tensorflow-lite-support:0.4.4'
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

    aaptOptions {
        noCompress 'tflite'
        noCompress 'lite'
    }

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId = "x.x.xxx"
        // 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.
        minSdk = flutter.minSdkVersion
        targetSdk = 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 = "../.."
}

解决步骤

1. 修正包导入路径

TensorFlow Lite在Kotlin中的正确包路径为org.tensorflow.lite,而非tensorflow。确保Kotlin代码中的导入语句符合规范,示例如下:

// 导入核心解释器
import org.tensorflow.lite.Interpreter
// 导入GPU加速代理
import org.tensorflow.lite.gpu.GpuDelegate
// 导入支持库中的张量缓冲区
import org.tensorflow.lite.support.tensorbuffer.TensorBuffer

2. 同步Gradle并清理项目

  • 执行File > Sync Project with Gradle Files,确保所有依赖被正确下载并引入项目
  • 清理并重建项目:依次执行Build > Clean Project和Build > Rebuild Project,清除编译缓存导致的依赖识别问题

3. 统一依赖版本兼容性

当前配置中play-services-tflite-java(16.1.0)与play-services-tflite-gpu(16.2.0)版本不一致,且可能与TensorFlow Lite核心版本(2.16.1)存在兼容冲突。建议统一相关依赖版本,修改后的依赖配置如下:

implementation 'com.google.android.gms:play-services-tflite-java:16.2.0'
implementation 'com.google.android.gms:play-services-tflite-gpu:16.2.0'

4. 确认namespace与Kotlin文件包名匹配

检查android块中的namespace = "x.x.xxx"是否与Kotlin文件的包声明一致。若不一致,需调整Kotlin文件的包名,或使用完整包路径导入TensorFlow Lite类。

5. 验证依赖引入状态

打开Android Studio的Project视图,导航到External Libraries,确认org.tensorflow:tensorflow-lite:2.16.1及相关依赖已存在。若未找到,说明依赖未成功下载,可删除项目根目录下的.gradle/caches文件夹后重新同步Gradle。


内容的提问来源于stack exchange,提问作者the elephant

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 12:45:00