首页 > 解决方案 > Flutter 应用程序立即在运行 api 16 的 Android 模拟器上崩溃

问题描述

我想检查我一直在开发的 Flutter 应用程序是否可以在较旧的 Android API 上运行。当我尝试在具有 API 16 的模拟器上运行它时,模拟器立即显示“不幸的是,[我的应用程序名称] 已停止。” 信息。

从日志来看,根本原因似乎是:

Caused by: java.lang.NoClassDefFoundError: androidx.collection.ArrayMap

我在这个项目中启用了 MultiDex,并且已经将这个类添加到我的 multiDexKeepFile(称为 multidex-config.txt)中,所以我不确定为什么它仍然找不到它。

multidex-config.txt 如下:

com/google/firebase/provider/FirebaseInitProvider.class
com/google/android/gms/common/internal/Preconditions.class
com/google/firebase/FirebaseApp.class
com/google/firebase/FirebaseApp$UiExecutor.class
androidx/collection/ArrayMap.class

还有我的应用模块 build.gradle 文件:

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

def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
   keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}

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 plugin: 'kotlin-android'
apply plugin: 'com.google.firebase.crashlytics'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion 29

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    lintOptions {
        disable 'InvalidPackage'
    }

    defaultConfig {
        applicationId "my.app.id"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        multiDexEnabled true
    }

    signingConfigs {
        release {
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
            storePassword keystoreProperties['storePassword']
        }
    }
    buildTypes {
        debug {
            multiDexKeepFile file('multidex-config.txt')
        }
        release {
            signingConfig signingConfigs.release
            multiDexKeepFile file('multidex-config.txt')
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.google.firebase:firebase-analytics:17.2.2'
    implementation("androidx.multidex:multidex:2.0.1")
}

apply plugin: 'com.google.gms.google-services'

我也在 MainActivity.kt 类中安装了 MultiDex:

class MainActivity: FlutterActivity() {
    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(base)
        MultiDex.install(this)
    }
}

提前感谢任何可以提供帮助的人,现在已经坚持了几天。

标签: flutterandroidx

解决方案


推荐阅读