错误:迁移到AndroidX后找不到符号导入com.google.firebase.iid.InstanceIdResult

时间:2019-05-07 06:51:05

标签: gradle flutter androidx

我有一个流畅的应用程序运行良好,然后我想将所有插件升级到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:错误:找不到符号                   公共无效onComplete(@NonNull任务任务){                                                        ^     符号:类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:错误:找不到符号                   公共无效onComplete(@NonNull任务任务){                                                        ^     符号:类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

3 个答案:

答案 0 :(得分:2)

以下更改解决了我的问题

来自:

ext {
 firebaseMessagingVersion = "+" // default: "+"
}

到:

ext {
 firebaseMessagingVersion = "21.1.0"
}

答案 1 :(得分: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 :(得分:0)

我能够使用此确切版本的gradle和google-services版本号组合来解决此问题,希望它能对您有帮助

package2