【问题标题】:Error: Invoke-customs are only supported starting with Android O (--min-api 26)错误:仅从 Android O (--min-api 26) 开始支持调用自定义
【发布时间】:2019-09-12 17:27:45
【问题描述】:

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

我收到以下编译错误:

原因:com.android.builder.dexing.DexArchiveBuilderException: 无法处理 C:\Users\Technical.gradle\caches\transforms-2\files-2.1\4f3f8638c6a9f961dae488a0387efb6b\jars\classes.jar

原因:com.android.builder.dexing.DexArchiveBuilderException: dexing 时出错。

原因:com.android.tools.r8.CompilationFailedException:编译失败

原因:com.android.tools.r8.utils.AbortException: Error: Invoke-customs 仅支持从 Android O (--min-api 26) 开始

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

如果不是新版本中的哪些更改导致创建 dex 文件失败?

我已尝试按照建议的heregradle.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'
}

【问题讨论】:

    标签: android android-gradle-plugin


    【解决方案1】:

    试试这个:

        defaultConfig {
        ...
        multiDexEnabled true
        }
    

    【讨论】:

    • 添加到哪里?
    • 你的项目 build.gradle 文件
    • 当我这样做时出现以下错误,ERROR: Could not find method defaultConfig() for arguments [build_1wu37mi9z13d069hc2dpowvjx$_run_closure3@546708aa] on project ':app' of type org.gradle.api.Project. Open File
    • 是的,但它没有解决原始编译错误
    【解决方案2】:

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

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

    【讨论】:

    • 在我将 apachi commons lang 库添加到项目后,我遇到了这个错误。然后在添加上面的代码后修复了我的错误。谢谢。
    • 不适合我,知道吗?
    • 谢谢,当我遇到这个问题时它解决了这个问题!
    【解决方案3】:

    在这个堆栈跟踪中相关的是这一行:

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

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

    【讨论】:

      【解决方案4】:

      我必须从 buildTypes 配置中删除 useProguard true

      根据the documentationminifyEnabled 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"
              }
          }
      }
      

      【讨论】:

        【解决方案5】:

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

        minifyEnabled 为真

        评论下面一行。

        //            minifyEnabled true
        //            shrinkResources true
        

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

        【讨论】:

          【解决方案6】:

          如果您使用的是 Google Cloud 库,请尝试将版本降低到 1.90

          【讨论】:

            【解决方案7】:

            添加以下内容以使您的应用由java8 编译器编译。

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

            并禁用在

            找到的即时运行

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

            【讨论】:

              【解决方案8】:

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

              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
              }
              

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

              【讨论】:

                【解决方案9】:

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

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

                【讨论】:

                • 如果您查看已经给出的答案,这是去年 4 月给出的公认解决方案
                【解决方案10】:

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

                android{
                    compileOptions {
                        sourceCompatibility 1.8
                        targetCompatibility 1.8
                    }
                }
                

                【讨论】:

                  【解决方案11】:

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

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

                  注意 - 有人建议 here 在 Android Studio 中禁用“即时运行”。此功能在我的 Android Studio 中不可用。版本 -

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

                  【讨论】:

                    【解决方案12】:

                    对于kotlin,您还应该添加 kotlinOptions:

                    android {
                        compileOptions {
                            sourceCompatibility JavaVersion.VERSION_1_8
                            targetCompatibility JavaVersion.VERSION_1_8
                        }
                    
                        kotlinOptions {
                            jvmTarget = JavaVersion.VERSION_1_8.toString()
                        }
                    }
                    

                    【讨论】:

                      【解决方案13】:

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

                      【讨论】:

                        【解决方案14】:

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

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

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

                        【讨论】:

                          【解决方案15】:

                          对我来说,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
                          

                          【讨论】:

                            【解决方案16】:

                            这在 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"
                                }
                            

                            【讨论】:

                              【解决方案17】:

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

                              compileOptions {
                                  sourceCompatibility JavaVersion.VERSION_1_8
                                  targetCompatibility JavaVersion.VERSION_1_8
                              }
                              

                              所以我解决了我的问题,只需将我的 minSdkVersion19 更新为 21 然后终于成功了!!!

                              【讨论】:

                                【解决方案18】:

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

                                接受答案的原因是最新版本的核心库针对 api 级别 30 的变化,它是用 java8 编写的,因此添加对 java8 的依赖解决了它。

                                【讨论】:

                                  【解决方案19】:

                                  截至 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'
                                  
                                  }
                                  

                                  【讨论】:

                                    猜你喜欢
                                    • 2020-03-11
                                    • 2019-12-05
                                    • 2020-09-25
                                    • 1970-01-01
                                    • 2020-06-25
                                    • 2021-04-26
                                    • 1970-01-01
                                    • 1970-01-01
                                    相关资源
                                    最近更新 更多