带有 --androidx 的新 Flutter 项目出现 AndroidX 不兼容错误

Posted

技术标签:

【中文标题】带有 --androidx 的新 Flutter 项目出现 AndroidX 不兼容错误【英文标题】:New Flutter project with --androidx has AndroidX incompatibilities error 【发布时间】:2020-05-03 09:45:55 【问题描述】:

我正在创建一个新的颤振项目:

flutter create --androidx project_name

创建的项目显示AndroidX 支持。仍然在使用很多插件时,包括官方插件。像这个颤动的camera 插件一样,我收到以下错误:

BUILD FAILED in 24s
The built failed likely due to AndroidX incompatibilities in a plugin. The tool is about to try using Jetfier to solve the incompatibility.
Building plugin camera...

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'camera'.
> SDK location not found. Define location with sdk.dir in the local.properties file or with an ANDROID_HOME environment variable.

* 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 plugin camera could not be built due to the issue above.

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

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.example.pot_fix"
        minSdkVersion 21
        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
        
    


flutter 
    source '../..'


dependencies 
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'

build.gradle:

buildscript 
    ext.kotlin_version = '1.3.50'
    repositories 
        google()
        jcenter()
    

    dependencies 
        classpath 'com.android.tools.build:gradle:3.5.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    


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

此外,错误不是连续的。有时应用程序运行时没有任何错误,有时它会给出上述错误。并非所有插件都会发生这种情况。一些插件工作正常,但其他插件会出现此错误。尽管所有这些插件都是针对 androidX 的。我正在使用最新版本,颤振医生没有给出任何错误。另外,我在模拟器和真机上都测试过,都报同样的错误。

【问题讨论】:

添加你的 build.gradle 现在,我添加了 build.gradle。 【参考方案1】:

在 android/app/build.gradle 中添加以下行:

multidexEnabled true

【讨论】:

以上是关于带有 --androidx 的新 Flutter 项目出现 AndroidX 不兼容错误的主要内容,如果未能解决你的问题,请参考以下文章

Flutter & AndroidX 不兼容如何手动设置依赖

无法将 Flutter 项目迁移到 AndroidX

错误:flutter中不存在包androidx.lifecycle

Flutter 无法在设备导入 androidx.annotation.Nullable 上运行应用程序

使用flutter_local_notifications和位置包的flutter错误“Android依赖'androidx.core:core'有不同的版本”

Android Flutter AndroidX incompatibilities报错处理