android - 安装location 2.3.5包时Flutter编译报错

标签 android flutter dart androidx

我安装了:flutter package - location 2.3.5 https://pub.dev/packages/location

当我尝试运行该应用程序时出现编译错误:

* What went wrong:
Execution failed for task ':location:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

这是完整的错误

Launching lib\main.dart on Android SDK built for x86 in debug mode...

C:\flutter\.pub-cache\hosted\pub.dartlang.org\location-2.3.5\android\src\main\java\com\lyokone\location\LocationPlugin.java:15: error: cannot find symbol
import androidx.annotation.MainThread;

                          ^
  symbol:   class MainThread
  location: package androidx.annotation

C:\flutter\.pub-cache\hosted\pub.dartlang.org\location-2.3.5\android\src\main\java\com\lyokone\location\LocationPlugin.java:16: error: cannot find symbol
import androidx.annotation.NonNull;
                          ^
  symbol:   class NonNull

  location: package androidx.annotation
C:\flutter\.pub-cache\hosted\pub.dartlang.org\location-2.3.5\android\src\main\java\com\lyokone\location\LocationPlugin.java:17: error: cannot find symbol
import androidx.annotation.Nullable;

                          ^
  symbol:   class Nullable
  location: package androidx.annotation

C:\flutter\.pub-cache\hosted\pub.dartlang.org\location-2.3.5\android\src\main\java\com\lyokone\location\LocationPlugin.java:18: error: package androidx.core.app does not exist
import androidx.core.app.ActivityCompat;
                        ^

C:\flutter\.pub-cache\hosted\pub.dartlang.org\location-2.3.5\android\src\main\java\com\lyokone\location\LocationPlugin.java:19: error: package androidx.core.content does not exist
import androidx.core.content.ContextCompat;
                            ^

C:\flutter\.pub-cache\hosted\pub.dartlang.org\location-2.3.5\android\src\main\java\com\lyokone\location\LocationPlugin.java:357: error: cannot find symbol
        this.locationPermissionState = ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION);
                                       ^
  symbol:   variable ActivityCompat

  location: class LocationPlugin

C:\flutter\.pub-cache\hosted\pub.dartlang.org\location-2.3.5\android\src\main\java\com\lyokone\location\LocationPlugin.java:362: error: cannot find symbol
        ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},

        ^
  symbol:   variable ActivityCompat
  location: class LocationPlugin

C:\flutter\.pub-cache\hosted\pub.dartlang.org\location-2.3.5\android\src\main\java\com\lyokone\location\LocationPlugin.java:367: error: cannot find symbol
        return ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.ACCESS_FINE_LOCATION);
               ^
  symbol:   variable ActivityCompat
  location: class LocationPlugin

C:\flutter\.pub-cache\hosted\pub.dartlang.org\location-2.3.5\android\src\main\java\com\lyokone\location\LocationPlugin.java:405: error: cannot find symbol
                public void onFailure(@NonNull Exception e) {
                                       ^
  symbol: class NonNull
C:\flutter\.pub-cache\hosted\pub.dartlang.org\location-2.3.5\android\src\main\java\com\lyokone\location\LocationPlugin.java:438: error: cannot find symbol

                    public void onFailure(@NonNull Exception e) {
                                           ^
  symbol: class NonNull
10 errors



FAILURE: Build failed with an exception.


* What went wrong:
Execution failed for task ':location:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.


* 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

*******************************************************************************************

The Gradle failure may have been because of AndroidX incompatibilities in this Flutter app.
See for more information on the problem and how to fix it.
*******************************************************************************************

Gradle task assembleDebug failed with exit code 1

Exited (sigterm)

  • 我完成了安装的所有步骤。
  • 我用这个位置包创建了一个新项目
  • 我运行:flutter clear
  • 我把 flutter 更新到了最新版本
  • 我把android studio更新到最新版本了
  • 我更新了android SDK

没有帮助

这是我的 android/build.gradle 文件:

buildscript {
    repositories {
        google()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.0'
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.50'        
        classpath 'com.google.gms:google-services:4.2.0'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

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

task clean(type: Delete) {
    delete rootProject.buildDir
}

这是我的 android/gradle.properties 文件:

org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true
android.enableJetifier=true

这是我的 android/app/src/build.gradle 文件:

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

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

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

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

apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion 28

    lintOptions {
        disable 'InvalidPackage'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.example.flutter_course"
        minSdkVersion 16
        targetSdkVersion 28
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    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
        }
    }
    configurations.all {
        resolutionStrategy.force 'com.android.support:support-compat:27.1.1'
        resolutionStrategy.force 'com.android.support:appcompat-v7:27.1.1'
        resolutionStrategy.force 'com.android.support:support-v4:27.1.1'
    }
}

flutter {
    source '../..'
}

dependencies {
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    implementation "com.android.support:support-v4:27.1.1"
}

这是我的 root/pubspec.yaml 文件:

name: flutter_course
description: A new Flutter project.

dependencies:
  flutter:
    sdk: flutter

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^0.1.0
  scoped_model: ^1.0.1
  http: ^0.12.0+2
  shared_preferences: ^0.5.2+1
  rxdart: ^0.22.0
  map_view: ^0.0.14
  location: ^2.3.5

dev_dependencies:
  flutter_test:
    sdk: flutter


# For information on the generic Dart part of this file, see the
# following page: https://www.dartlang.org/tools/pub/pubspec

# The following section is specific to Flutter.
flutter:

  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true

  # To add assets to your application, add an assets section, like this:
  assets:
    - assets/food.jpg
    - assets/background.jpg
  #  - images/a_dot_ham.jpeg

  # An image asset can refer to one or more resolution-specific "variants", see
  # https://flutter.io/assets-and-images/#resolution-aware.

  # For details regarding adding assets from package dependencies, see
  # https://flutter.io/assets-and-images/#from-packages

  # To add custom fonts to your application, add a fonts section here,
  # in this "flutter" section. Each entry in this list should have a
  # "family" key with the font family name, and a "fonts" key with a
  # list giving the asset and other descriptors for the font. For
  # example:
  fonts:
    - family: Oswald
      fonts:
        - asset: assets/Oswald-Bold.ttf
          weight: 700
  #       - asset: fonts/Schyler-Italic.ttf
  #         style: italic
  #   - family: Trajan Pro
  #     fonts:
  #       - asset: fonts/TrajanPro.ttf
  #       - asset: fonts/TrajanPro_Bold.ttf
  #         weight: 700
  #
  # For details regarding fonts from package dependencies, 
  # see https://flutter.io/custom-fonts/#from-packages

可能是什么问题?

我该如何解决?

最佳答案

回复第一期报告“找不到符号 import androidx.annotation.NonNull;"不仅仅是与未找到非空符号相关的任何其他问题。这可能是与您机器中安装的 java 版本相关的问题。当您使用最新的 java 版本(例如 11)时,可能会发生这种情况或者您正在使用非常旧的依赖项。

我正在回复关于在 flutter 中添加新依赖项和编译相关问题的一般问题,而不是定位。希望能帮助到你。

关于android - 安装location 2.3.5包时Flutter编译报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56321639/

相关文章:

android-emulator - 将 Android 设备连接到本地主机上的 Web 服务

android - Android 中用于蓝牙的 API 查找我个人资料中的即时警报服务

flutter - Flutter Stateful小部件无法从提供程序包中自动更新

flutter - Flutter Provider,在此处放置函数调用以从Firestore数据库加载数据

android - 是否可以在凸起的按钮和文本字段之间创建连接?

dart - Dart 和可观察的属性

android - 当我尝试在 qemu 上运行 Android 时没有显示消息 :"Guest has not initialized the display (yet)."

android - 如何在 Activity 中从 RecyclerView 上的 onClick 开始 Activity

dart - Flutter 能否免去使用 mac 来创建 IOS 应用程序?

flutter - ThemeData-不建议使用的标题参数