错误:迁移到 AndroidX 后找不到符号导入 com.google.firebase.iid.InstanceIdResult
Posted
技术标签:
【中文标题】错误:迁移到 AndroidX 后找不到符号导入 com.google.firebase.iid.InstanceIdResult【英文标题】:error: cannot find symbol import com.google.firebase.iid.InstanceIdResult after migrating to AndroidX 【发布时间】:2019-09-24 18:54:31 【问题描述】:我有一个 Flutter 应用程序运行良好,然后我想我会将我所有的插件升级到 androidX,并且在我决定添加 firebase_messaging 之前一切正常,我已经尝试了 4,5 版,没有一个工作,继续抛出那个错误,好像我在错误的版本上。我也试过flutter packages pub cache repair
不走运。 github上有很多相关的问题,但是他们都被关闭了,人们仍然抱怨修复不起作用。
这是我的 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 = '3'
def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null)
flutterVersionName = '1.0.1'
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
apply plugin: 'com.google.gms.google-services'
def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists())
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
android
compileSdkVersion 28
sourceSets
main.java.srcDirs += 'src/main/kotlin'
lintOptions
disable 'InvalidPackage'
defaultConfig
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.sggo.uni"
minSdkVersion 21
targetSdkVersion 28
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
// ndk
// abiFilters 'x86', 'armeabi-v7a'
//
compileOptions
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
signingConfigs
release
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
storeFile file(keystoreProperties['storeFile'])
storePassword keystoreProperties['storePassword']
buildTypes
release
signingConfig signingConfigs.release
flutter
source '../..'
dependencies
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
buildscript
ext.kotlin_version = '1.3.31'
repositories
google()
jcenter()
dependencies
classpath 'com.android.tools.build:gradle:3.4.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.gms:google-services:4.0.2'
allprojects
configurations.all
resolutionStrategy.eachDependency DependencyResolveDetails details ->
def requested = details.requested
if (requested.group == 'com.google.firebase')
details.useVersion "15.+"
repositories
google()
jcenter()
rootProject.buildDir = '../build'
subprojects
project.buildDir = "$rootProject.buildDir/$project.name"
subprojects
project.evaluationDependsOn(':app')
task clean(type: Delete)
delete rootProject.buildDir
导入 com.google.firebase.iid.InstanceIdResult; ^ 符号:类 InstanceIdResult 位置:包 com.google.firebase.iid /Users/test/Desktop/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_messaging-5.0.0/android/src/main/java/io/flutter/plugins/firebasemessaging/FirebaseMessagingPlugin.java:106:错误:找不到符号 新的 OnCompleteListener() ^ 符号:类 InstanceIdResult 位置:类 FirebaseMessagingPlugin /Users/test/Desktop/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_messaging-5.0.0/android/src/main/java/io/flutter/plugins/firebasemessaging/FirebaseMessagingPlugin.java:108:错误:找不到符号 public void onComplete(@NonNull Task task) ^ 符号:类 InstanceIdResult /Users/test/Desktop/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_messaging-5.0.0/android/src/main/java/io/flutter/plugins/firebasemessaging/FirebaseMessagingPlugin.java:104:错误:找不到符号 .getInstanceId() ^ 符号:方法 getInstanceId() 位置:类 FirebaseInstanceId /Users/test/Desktop/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_messaging-5.0.0/android/src/main/java/io/flutter/plugins/firebasemessaging/FirebaseMessagingPlugin.java:133:错误:找不到符号 新的 OnCompleteListener() ^ 符号:类 InstanceIdResult 位置:类 FirebaseMessagingPlugin /Users/test/Desktop/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_messaging-5.0.0/android/src/main/java/io/flutter/plugins/firebasemessaging/FirebaseMessagingPlugin.java:135:错误:找不到符号 public void onComplete(@NonNull Task task) ^ 符号:类 InstanceIdResult /Users/test/Desktop/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_messaging-5.0.0/android/src/main/java/io/flutter/plugins/firebasemessaging/FirebaseMessagingPlugin.java:131:错误:找不到符号 .getInstanceId() ^ 符号:方法 getInstanceId() 位置:类 FirebaseInstanceId /Users/test/Desktop/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_messaging-5.0.0/android/src/main/java/io/flutter/plugins/firebasemessaging/FlutterFirebaseMessagingService.java:39:错误:方法没有覆盖或实现超类型中的方法 @Override
【问题讨论】:
【参考方案1】:是的,因为 com.google.firebase.iid.InstanceIdResult 已弃用且不再可用。 因此,为了从 firebase 获取令牌,您现在必须使用。
FirebaseMessaging.getInstance().getToken()
.addOnCompleteListener(new OnCompleteListener<String>()
@Override
public void onComplete(@NonNull Task<String> task)
if (!task.isSuccessful())
Log.w(TAG, "Fetching FCM registration token failed", task.getException());
return;
// Get new FCM registration token
String token = task.getResult();
// Log and toast
String msg = getString(R.string.msg_token_fmt, token);
Log.d(TAG, msg);
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
);
【讨论】:
【参考方案2】:以下更改解决了我的问题
来自:
ext
firebaseMessagingVersion = "+" // default: "+"
到:
ext
firebaseMessagingVersion = "21.1.0"
【讨论】:
谢谢你拯救了我的一天! 我们在哪里添加上述内容? @Lord【参考方案3】:我能够通过使用这个exactbuild gradle 和google-services 版本号组合来解决这个问题,希望它对你有所帮助
buildscript
ext.kotlin_version = '1.3.31'
repositories
google()
jcenter()
dependencies
classpath 'com.android.tools.build:gradle:3.3.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.gms:google-services:4.2.0'
allprojects
repositories
google()
jcenter()
【讨论】:
以上是关于错误:迁移到 AndroidX 后找不到符号导入 com.google.firebase.iid.InstanceIdResult的主要内容,如果未能解决你的问题,请参考以下文章
FirebaseAuthPlugin.java:9:错误:找不到符号导入 androidx.annotation.NonNull;
将 React Native 升级到 0.60-RC2 后找不到库“libjsc.so”
PHP 致命错误:在我修复错误 Laravel 迁移后找不到类“App\Providers\ServiceProvider”:唯一键太长,
在迁移到 viewpager2 androidx 时发现此错误“无法解析方法 'super(androidx.fragment.app.FragmentManager)'”