Error: Invoke-customs are only supported starting with Android O (--min-api 26)

Error: Invoke-customs are only supported starting with Android O (--min-api 26)

我最近开始学习如何通过 Android Studio 对 Android 设备进行编程。在我今天早上升级到 Android Studio 3.4 之前,我的第一个应用 运行 还不错。

我遇到以下编译错误:

Caused by: com.android.builder.dexing.DexArchiveBuilderException: Failed to process C:\Users\Technical.gradle\caches\transforms-2\files-2.1f3f8638c6a9f961dae488a0387efb6b\jars\classes.jar

Caused by: com.android.builder.dexing.DexArchiveBuilderException: Error while dexing.

Caused by: com.android.tools.r8.CompilationFailedException: Compilation failed to complete

Caused by: com.android.tools.r8.utils.AbortException: Error: Invoke-customs are only supported starting with Android O (--min-api 26)

有没有办法恢复到我以前的 Android Studio 版本?

如果不是,新版本中有什么变化导致创建 dex 文件失败?

我尝试按照建议在 gradle.properties 中添加 android.enableD8=true ,但没有成功。


编辑#1:

还绑定了将 multiDexEnabled true 添加到应用程序 build.gradle 文件中的默认配置,但仍然存在相同的编译错误。

完整的构建文件...

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "qdivision.org.qrtracker"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.github.felHR85:UsbSerial:6.0.5'
}

试试这个:

    defaultConfig {
    ...
    multiDexEnabled true
    }

尝试将以下添加到您的 app/build.gradle 以使您的 Android 项目编译与 Java 8.[=12= 兼容]

android {
    ....
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    ....
}

与此堆栈跟踪相关的是这一行:

Caused by: com.android.tools.r8.utils.AbortException: Error: Invoke-customs are only supported starting with Android O (--min-api 26)

由于您的依赖项中没有 kotlin 库,我假设您正在使用 java。我会尝试

我不得不从 buildTypes 配置中删除 useProguard true

根据 the documentation minifyEnabled true 足以用 R8 混淆您的代码。

示例:

android {
    buildTypes {
        debug {
            versionNameSuffix "-dev"
            minifyEnabled true // <- remove this line when using instant run
            useProguard true // <- remove this line
            signingConfig signingConfigs.release
            proguardFiles getDefaultProguardFile('proguard-android.txt'), "proguard-rules.pro"
        }

        release {
            shrinkResources true
            minifyEnabled true
            useProguard true // <- remove this line
            signingConfig signingConfigs.release
            proguardFiles getDefaultProguardFile('proguard-android.txt'), "proguard-rules.pro"
        }
    }
}

如果您尝试直接从 android studio 运行 apk,请尽量避免启用

minifyEnabled true

注释以下行。

//            minifyEnabled true
//            shrinkResources true

但是您可以在构建 apk 时使用上面的代码。

如果您正在使用Google云库,请尝试将版本降低至1.90

添加以下内容使您的应用程序能够被 java8 编译器编译。

android {

    .....
    
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

并禁用在

找到的即时 运行

Preferences->Build,Execution,Deployment->Debugger->Instant Run

我已将这些代码添加到所有模块中:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

但错误仍然存​​在。 在我的例子中,我的一些模块使用了类路径 'me.tatarka:gradle-retrolambda' 并且它与此产生了冲突:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

所以我必须通过这个 link 从 Retrolambda 迁移: https://developer.android.com/studio/write/java8-support#migrate_from_retrolambda

请在 build.gradle 文件中添加此行。

android{
 compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8

    }
}

在您的 Android Studio 中转到 build.gradle(Module:App) 然后将此 compileOptions{} 块添加到 android{}

android{
    compileOptions {
        sourceCompatibility 1.8
        targetCompatibility 1.8
    }
}

我遇到了同样的问题。我的是由 ... org.apache.commons:commons-lang ... 引起的。我目前没有完整的错误信息。此错误仅在重建或清理构建期间发生。

我通过将 build.gradle 中的 org.apache.commons:commons-text:1.8 更改为 org.apache.commons:commons-text:1.6 来解决。

注意 - 有人建议 在 Android Studio 中禁用 "Instant Run"。此功能在我的 Android Studio 中不可用。版本 -

Android Studio 3.5.1
Build #AI-191.8026.42.35.5900203, built on September 26, 2019
Windows 10 10.0 

对于 kotlin 你还应该添加 kotlinOptions:

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_1_8.toString()
    }
}

对我来说 Java 8 的解决方案只有在我删除了与 retrolambda 相关的所有 ting 后才开始工作。

对于遇到此问题的其他人,我在添加对此 Jackson 库的依赖项后遇到了这个问题:

implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.11.1")

上面的“compileOptions”解决方案为我解决了这个问题

我按照 BUILD.GRADLE 中的命令删除了错误。

defaultConfig {
      applicationId "com.example.myapptestheader"
      minSdkVersion 21
      targetSdkVersion 29
      versionCode 1
      versionName "1.0"
      testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
   
      multiDexEnabled true
}

我刚刚添加

multiDexEnabled true

这在 2021 年仍然有效,请尝试一下。

android {
    compileSdkVersion 30

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    defaultConfig {
        applicationId "com.example.android.bakingapp"
        minSdkVersion 15
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

对于我的情况,它在添加以下行后也显示 -

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

所以我解决了我的问题只是简单地将我的 minSdkVersion19 更新为 21 然后终于成功了!! !

我在将 build.gradle 中的核心库升级到最新版本后遇到了这个错误,幸运的是接受了对我有用的答案。

已接受答案的工作原因是针对 api 级别 30 的核心库的最新版本发生了变化,它是在 java8 中编写的,因此添加对 java8 的依赖性解决了它.

截至 2021 年 12 月,这是有效的。 我有 运行 的原因是因为缺少 skd 版本 26 和缺少 java 版本 8。在您的 build.gradle(app) 文件夹下,添加以下代码以确保您具有正确的依赖关系、默认配置和 android 兼容性。您不需要知道它们的含义,只需要知道它们存在即可。基本上你试图确保 Android 可以读取以前版本的东西。这是我 运行 确保我的应用 运行.

的代码
apply plugin: 'com.android.application'

android {

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
compileSdkVersion 26
compileSdkVersion 27
compileSdkVersion 30
buildToolsVersion "30.0.2"


defaultConfig {
    applicationId "com.example.firstfirstapp"
    minSdkVersion 16
    targetSdkVersion 26
    targetSdkVersion 30
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 
'proguard-rules.pro'
    }
}

}

dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "androidx.cardview:cardview:1.0.0"
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support:design:26.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

implementation 'com.android.support:appcompat-v7:26.0.0'

}

添加这一行:- multiDexEnabled true in build. gradle

在`defaultConfig { ... multiDexEnabled 真 }

同时使minsdk = 26