我安装了:flutter软件包-位置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
没有帮助
这是我的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
可能是什么问题?
该如何解决?
答案 0 :(得分:0)
回复第一期报道“找不到符号 import androidx.annotation.NonNull;“不仅如此,还没有找到与非null符号有关的任何其他问题。这可能是与您计算机中安装的Java版本有关的问题。当您使用最新的Java版本(例如11)时可能会发生或者您正在使用非常老的依赖项。
我回答的问题是添加新的依赖项,并在未定位的情况下编译相关问题。希望能帮助到你。
答案 1 :(得分:0)
我有一个非常相似的问题。 Flutter不会停止使用我的软件包之一的过时版本。 我尝试过:
flutter clean
flutter pub cache repair
但是flutter仍在使用旧软件包,并且仍然出现编译错误。
运行flutter run -v
时,我可以看到版本号
软件包错误,并且与pubspec不同步。
我终于通过更改频道(再次下载SDK)使它正常工作
所以尝试:
flutter channel beta
flutter upgrade
flutter channel stable
flutter run -v
那应该可以解决问题。