gradle Flutter应用程序在应用fire base时启动时崩溃(java.lang.RuntimeException:无法示例化活动)

oknwwptz  于 2023-06-23  发布在  Flutter
关注(0)|答案(1)|浏览(132)

我在Flutter中开始了一个新项目,我正在将Fire Store应用到它。
在按照步骤添加我的应用程序到消防基地,它开始给予错误。
当启动应用程序时在日志猫中发现的错误是

04-04 14:54:56.061 24084-24084/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.firestoreflutterchat, PID: 24084
    java.lang.RuntimeException: Unable to get provider com.google.firebase.provider.FirebaseInitProvider: java.lang.ClassNotFoundException: Didn't find class "com.google.firebase.provider.FirebaseInitProvider" on path: DexPathList[[zip file "/data/app/com.example.firestoreflutterchat-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.firestoreflutterchat-2, /vendor/lib, /system/lib]]
        at android.app.ActivityThread.installProvider(ActivityThread.java:4869)
        at android.app.ActivityThread.installContentProviders(ActivityThread.java:4461)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4401)
        at android.app.ActivityThread.access$1500(ActivityThread.java:139)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1270)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5097)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.firebase.provider.FirebaseInitProvider" on path: DexPathList[[zip file "/data/app/com.example.firestoreflutterchat-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.firestoreflutterchat-2, /vendor/lib, /system/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
        at android.app.ActivityThread.installProvider(ActivityThread.java:4854)
        at android.app.ActivityThread.installContentProviders(ActivityThread.java:4461) 
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4401) 
        at android.app.ActivityThread.access$1500(ActivityThread.java:139) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1270) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:136) 
        at android.app.ActivityThread.main(ActivityThread.java:5097) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:515) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
        at dalvik.system.NativeStart.main(Native Method

我在Kit Kat版本设备Xt1030上运行的应用程序
为了消除这个错误,我在Gradle中做了一些事情,其他人描述的主要是Multidex
这是我的Gradle文件:

buildscript {
    ext.kotlin_version = '1.3.50'
    repositories {
        google()
        mavenCentral()
      //  maven { url 'https://github.com/yongjhih/android-gradle-plugin.m2/raw/master/' }
        jcenter()

    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.6.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        // Add this line

       // classpath 'com.google.gms:google-services:3.2.1'
        classpath 'com.google.gms:google-services:4.2.0'
        //com.google.android.gms to 11.4.2.
    }
}

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
}

gradle app:

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 FileNotFoundException("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 plugin: 'kotlin-android'
// Add this line
apply plugin: 'com.google.gms.google-services'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion 28

//    compileOptions {
//        sourceCompatibility JavaVersion.VERSION
//        targetCompatibility JavaVersion.VERSION 8
//    }
    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.example.firestoreflutterchat"
        minSdkVersion 18
        targetSdkVersion 28
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
  //      testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
//        ndk {
//            abiFilters 'armeabi-v7a'
//        }
        multiDexEnabled true


//        productFlavors {
//            dev {
//                // Enable pre-dexing to produce an APK that can be tested on
//                // Android 5.0+ without the time-consuming DEX build processes.
//                minSdkVersion 21
//                flavorDimensions "versionCode"
//            }
//            prod {
//                // The actual minSdkVersion for the production version.
//                minSdkVersion 18
//                flavorDimensions "versionCode"
//            }
//        }

    }

    buildTypes {
        release {
            multiDexKeepFile file('multidex-config.txt')

            // 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 '../..'
}


dependencies {
    // add the Firebase SDK for Google Analytics
    implementation 'com.google.firebase:firebase-analytics:17.2.2'
    implementation 'androidx.multidex:multidex:2.0.0'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

在清单文件中,它被告知更改名称:

android:name="io.flutter.app.FlutterApplication"

android:name="androidx.multidex.MultiDexApplication"

或者用另一种方式来做这件事
添加扩展flutter应用程序的类
在表演时

package com.example.firestoreflutterchat

    import android.content.Context
    import androidx.multidex.MultiDex
    import io.flutter.app.FlutterApplication

    class App : FlutterApplication() {

        override fun attachBaseContext(base: Context?) {
            super.attachBaseContext(base)
            MultiDex.install(this)
        }
    }

然后将Android清单从

android:name="io.flutter.app.FlutterApplication"

android:name=".App"

现在,在做了所有这些仍然我的应用程序崩溃,但与不同的错误

04-05 18:22:18.266 10645-10645/com.example.firestoreflutterchat E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.firestoreflutterchat, PID: 10645
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.firestoreflutterchat/com.example.firestoreflutterchat.MainActivity}: java.lang.ClassCastException: com.example.firestoreflutterchat.MainActivity cannot be cast to android.app.Activity
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2124)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
        at android.app.ActivityThread.access$800(ActivityThread.java:139)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5097)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ClassCastException: com.example.firestoreflutterchat.MainActivity cannot be cast to android.app.Activity
        at android.app.Instrumentation.newActivity(Instrumentation.java:1084)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2115)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257) 
        at android.app.ActivityThread.access$800(ActivityThread.java:139) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:136) 
        at android.app.ActivityThread.main(ActivityThread.java:5097) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:515) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
        at dalvik.system.NativeStart.main(Native Method) 
rbpvctlc

rbpvctlc1#

以前发生此错误是由于兼容性问题和multidex上设置的问题。这已经被修补的flutterfire小组提到在这张票。此外,使用flutterfire_cli,在Flutter上设置Firebase现在变得很容易

相关问题