【问题标题】:invokedynamic requires --min-sdk-version >= 26invokedynamic 需要 --min-sdk-version >= 26
【发布时间】:2018-02-02 08:48:42
【问题描述】:

今天下载了studio 3.0 beta 2.0版本,之后尝试在里面打开一个已有的项目,遇到了一些困难,大部分问题我可以借助Google和Stack Overflow解决,但是这个我不行。

Error:Execution failed for task ':app:transformClassesWithDexBuilderForDebug'.
> com.android.build.api.transform.TransformException: org.gradle.tooling.BuildException: com.android.dx.cf.code.SimException: invalid opcode ba (invokedynamic requires --min-sdk-version >= 26)

同时发布我的应用程序 gradle

apply plugin: 'com.android.application'
   android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"

defaultConfig {
    applicationId "com.intersoft.snappy"
    minSdkVersion 19
    targetSdkVersion 22
    multiDexEnabled true
    versionCode 1
    versionName "1.0"
}
buildTypeMatching 'dev', 'debug'
buildTypeMatching 'qa', 'debug'
buildTypeMatching 'rc', 'release'
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
sourceSets { main { assets.srcDirs = ['src/main/assets', 'src/main/assets/'] 
} }
packagingOptions {
    exclude 'META-INF/DEPENDENCIES.txt'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/notice.txt'
    exclude 'META-INF/license.txt'
    exclude 'META-INF/dependencies.txt'
    exclude 'META-INF/LGPL2.1'
}
}
repositories {
mavenCentral()
mavenLocal()
jcenter()
maven { url "https://plugins.gradle.org/m2/" }
maven { url "https://s3.amazonaws.com/repo.commonsware.com" }
maven { url "https://jitpack.io" }
maven { url 'https://dl.bintray.com/ashokslsk/CheckableView' }
maven { url "https://maven.google.com" }

}
android {
useLibrary 'org.apache.http.legacy'
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.0.1'
implementation 'com.github.mrengineer13:snackbar:1.2.0'
implementation 'com.android.support:recyclerview-v7:26.0.1'
implementation 'com.android.support:cardview-v7:26.0.1'
implementation 'com.android.support:design:26.0.1'
implementation 'com.android.support:percent:26.0.1'
implementation 'dev.dworks.libs:volleyplus:+'
implementation 'com.google.guava:guava:21.0'
implementation 'com.facebook.fresco:fresco:1.0.1'
implementation 'com.github.bumptech.glide:glide:3.7.0'
implementation 'com.wdullaer:materialdatetimepicker:3.1.1'
implementation 'com.squareup.picasso:picasso:2.5.2'
implementation 'com.github.stfalcon:frescoimageviewer:0.4.0'
implementation 'com.github.piotrek1543:CustomSpinner:0.1'
implementation 'com.android.support:multidex:1.0.2'
implementation 'com.github.satyan:sugar:1.4'
implementation 'com.hedgehog.ratingbar:app:1.1.2'
implementation project(':sandriosCamera')
implementation('org.apache.httpcomponents:httpmime:4.2.6') {
    exclude module: 'httpclient'
}
implementation 'com.googlecode.json-simple:json-simple:1.1'
}

afterEvaluate {
tasks.matching {
    it.name.startsWith('dex')
}.each { dx ->
    if (dx.additionalParameters == null) {
        dx.additionalParameters = ['--multi-dex']
    } else {
        dx.additionalParameters += '--multi-dex'
    }
}
}
subprojects {
project.plugins.whenPluginAdded { plugin ->
    if ("com.android.build.gradle.AppPlugin".equals(plugin.class.name)) {
        project.android.dexOptions.preDexLibraries = false
    } else if 
("com.android.build.gradle.LibraryPlugin".equals(plugin.class.name)) {
        project.android.dexOptions.preDexLibraries = false
    }
}
}

buildscript {
repositories {
    mavenCentral()
}

dependencies {
    classpath 'com.jakewharton.hugo:hugo-plugin:1.2.1'
}
}

apply plugin: 'com.jakewharton.hugo'

也是我的另一个模块 gradle

apply plugin: 'com.android.library'
apply plugin: 'com.jfrog.bintray'
apply plugin: 'com.github.dcendents.android-maven'

buildscript {
repositories {
    jcenter()
    jcenter()
    maven { url "https://maven.google.com" }
}
dependencies {
    classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7'
    classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
}
}

group = 'com.sandrios.android'
version = '1.0.8'

ext {
PUBLISH_GROUP_ID = 'com.sandrios.android'
PUBLISH_ARTIFACT_ID = 'sandriosCamera'
PUBLISH_VERSION = '1.0.8'
PUBLISH_CODE = 9
}

android {
compileSdkVersion 26
buildToolsVersion "26.0.1"

defaultConfig {
    minSdkVersion 19
    targetSdkVersion 25
    versionCode PUBLISH_CODE
    versionName PUBLISH_VERSION
}

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

task generateSourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier 'sources'
}

task generateJavadocs(type: Javadoc) {
failOnError false
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath()
        .join(File.pathSeparator))
}

task generateJavadocsJar(type: Jar) {
from generateJavadocs.destinationDir
classifier 'javadoc'
}

generateJavadocsJar.dependsOn generateJavadocs

artifacts {
archives generateSourcesJar
archives generateJavadocsJar
}

install {
repositories.mavenInstaller {
    pom.project {
        name PUBLISH_GROUP_ID
        description 'Simple integration of universal camera in android for easy image and video capture.'
        url 'https://github.com/sandrios/sandriosCamera'
        inceptionYear '2016'

        packaging 'aar'
        version PUBLISH_VERSION

        scm {
            connection 'https://github.com/sandrios/sandriosCamera.git'
            url 'https://github.com/sandrios/sandriosCamera'

        }
        developers {
            developer {
                name 'arpitgandhi9'
            }
        }
    }
}
}

bintray {
Properties properties = new Properties()

properties.load(project.rootProject.file('local.properties').newDataInputStream())
user = properties.getProperty('bintray.user')
key = properties.getProperty('bintray.apikey')
configurations = ['archives']
pkg {
    repo = 'android'
    name = 'sandriosCamera'
    userOrg = 'sandriosstudios'
    desc = 'Android solution to simplify work with different camera apis.'
    licenses = ['MIT']
    labels = ['android', 'camera', 'photo', 'video']
    websiteUrl = 'https://github.com/sandrios/sandriosCamera'
    issueTrackerUrl = 'https://github.com/sandrios/sandriosCamera/issues'
    vcsUrl = 'https://github.com/sandrios/sandriosCamera.git'
    version {
        name = PUBLISH_VERSION
        vcsTag = PUBLISH_VERSION
        desc = 'Minor fixes.'
        released = new Date()
    }
}
}

repositories {
jcenter()
}

dependencies {
implementation 'com.android.support:support-v4:26.0.0'
implementation 'com.android.support:appcompat-v7:26.0.0'
implementation 'com.android.support:recyclerview-v7:26.0.0'
implementation 'com.github.bumptech.glide:glide:3.6.1'
implementation 'com.yalantis:ucrop:2.2.0'
implementation 'gun0912.ted:tedpermission:1.0.2'
}

还有项目级别的gradle

// Top-level build file where you can add configuration options common to 
all sub-projects/modules.

buildscript {
repositories {
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:3.0.0-beta2'

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}
}

allprojects {
repositories {
    jcenter()
    maven {
        url "https://maven.google.com"
    }
}
}

请帮我摆脱这个错误

【问题讨论】:

  • 请发布您的 gradle-wrapper.properties 文件
  • @IntelliJAmiya 3.0 据我所知只有 alpha 和 beta
  • @ShaifaliPundir 我没有发布 gradle-wrapper.properties,因为我确信它与此无关

标签: android android-studio android-gradle-plugin build.gradle


【解决方案1】:

重要的部分:

您需要在该模块的 build.gradle 中添加它,而不是像 app 模块那样添加它。

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

另外你忘了为插件添加repo:

buildscript {
    repositories {
        jcenter()
        google()
    }
}

【讨论】:

  • 很确定他不能使用 Java 1.8,因为他的最小 SDK 是 19。我遇到了同样的问题。另外,我尝试这样做,但错误仍然存​​在。
  • 我将它与最小 SDK 15 一起使用。Gradle Plugin 2.4+ 运行 desugar 获取代码,您可以在 developer.android.com/studio/write/java8-support.html 处查看 lambda support
  • @TheHebrewHammer Android 插件 v3 将采用 Java 8 代码并基于最低 SDK 版本生成兼容的 Dex。它确实支持 Java 8 语言结构,但不支持 java.time 等新 API。
  • 我使用的是 3.0.0-beta2 它只有在我切换到使用 java 1.7 的 guava 的 Android 版本时才有效
  • 谢谢!就我而言,一个库使用 JavaVersion.VERSION_1_8,但使用该库的项目在 build.gradle 中没有它。添加它立即解决了问题。
【解决方案2】:

我添加了一个带有compileOptions Java 1.8 的库,而在我的主项目中没有。 修正添加:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

【讨论】:

  • 对我来说同样的问题,添加它可以解决问题
【解决方案3】:

我遇到了同样的问题。我最近删除了我的 .gradle 缓存文件夹,并重新安装了 Android Studio 和 SDK。最终当试图git bisect 问题时,它消失了。我只能推测为什么会发生这种情况,但我怀疑下载旧版本的构建工具和 SDK,并使用这些旧工具构建(并且可能缓存)我们的代码版本会导致它以一种没有的方式构建不会引起问题。

这表明新的(API 26?)构建工具构建源代码的方式存在某种错误,因此,如果您遇到此问题并且其他解决方案不起作用,我的建议是要将您的目标 SDK 版本降低到 25 或更低版本,请安装必要的构建工具,并尝试使用这些工具编译您的代码,然后再恢复为 26 或更高版本的构建工具。

【讨论】:

    【解决方案4】:

    我遇到了与您遇到的相同的错误 (SimException)。我在 android clean architecture 项目中有 3 个模块:

    • 数据(安卓库)
    • 域(纯 java 模块)
    • 演示文稿(应用程序 - 所有 android 的东西)

    解决方案

    导航到File/Project Structure...

    确保您的模块具有相同的源和目标兼容性(在本例中为 1.8)

    【讨论】:

    • 当 java 库模块使用 1.8 时,即使 kotlin 模块现在也需要设置 sourceCompatibilitytargetCompatibility。感谢您的提示。
    • 我在尝试为 Android 编译 Scala 时遇到了类似的错误,这也解决了这个问题。
    【解决方案5】:

    找到我的答案,就我个人而言,它正在使用

    implementation "com.google.guava:guava:23.0"
    

    代替

    implementation "com.google.guava:guava:23.0-android"
    

    【讨论】:

    • 恕我直言:在 Android 应用中使用 Guava 是不好的解决方案
    • 库太胖了。它将强制使用multidex。如果您想使用 Guava 中的一两个类 - 仅提取这些类并放入您的应用中
    • 它起作用了,但你能解释一下吗? @TheHebrewHammer
    • 当然,留在 guava 22(我认为)他们开始使用 JDK 1.8 功能,这些功能直到 API 24 才可用(在 guava 23 中有一些语言功能直到 26 才可用为什么你会看到错误)。当他们这样做时,他们拆分了库以支持较旧的 Android API。所以,guava-android 被锁定到 JDK 1.7。
    猜你喜欢
    • 2019-04-02
    • 2019-11-23
    • 1970-01-01
    • 2012-05-06
    • 1970-01-01
    • 1970-01-01
    • 2012-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多