Android studio 2.0 gradle 使用按钮时 transformClassesWithDexForDebug 失败 "run"

Android studio 2.0 gradle transformClassesWithDexForDebug fails when using button "run"

我从 android studio 1.5 迁移到 2.0

后遇到问题

在我的一个项目(只有一个)中,我无法使用 android studio 运行 按钮,

因为那时 gradle 任务 "transformClassesWithDexForDebug"

构建失败

有错误:

:app:transformClassesWithDexForDebug
AGPBI: {"kind":"error","text":"Uncaught translation error: com.android.dx.cf.code.SimException: local variable type mismatch: attempt to set or access a value of type java.lang.Object using a local variable of type int. This is symptomatic of .class transformation tools that ignore local variable information.","sources":[{}]}
AGPBI: {"kind":"error","text":"1 error; aborting","sources":[{}]}

 FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: java.lang.RuntimeException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/usr/lib/jvm/java-8-oracle/bin/java'' finished with non-zero exit value 1

* Try:
Run with --info or --debug option to get more log output.

* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:transformClassesWithDexForDebug'.
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:69)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:46)
    at org.gradle.api.internal.tasks.execution.PostExecutionAnalysisTaskExecuter.execute(PostExecutionAnalysisTaskExecuter.java:35)
    at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:64)
    at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:58)
    at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:52)
    at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52)
    at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:53)
    at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:203)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:185)
    at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.processTask(AbstractTaskPlanExecutor.java:66)
    at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.run(AbstractTaskPlanExecutor.java:50)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor.process(DefaultTaskPlanExecutor.java:25)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter.execute(DefaultTaskGraphExecuter.java:110)
    at org.gradle.execution.SelectedTaskExecutionAction.execute(SelectedTaskExecutionAction.java:37)
    at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:37)
    at org.gradle.execution.DefaultBuildExecuter.access[=13=]0(DefaultBuildExecuter.java:23)
    at org.gradle.execution.DefaultBuildExecuter.proceed(DefaultBuildExecuter.java:43)
    at org.gradle.execution.DryRunBuildExecutionAction.execute(DryRunBuildExecutionAction.java:32)
    at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:37)
    at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:30)
    at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLauncher.java:154)
    at org.gradle.internal.Factories.create(Factories.java:22)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:90)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:52)
    at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:151)
    at org.gradle.initialization.DefaultGradleLauncher.access0(DefaultGradleLauncher.java:32)
    at org.gradle.initialization.DefaultGradleLauncher.create(DefaultGradleLauncher.java:99)
    at org.gradle.initialization.DefaultGradleLauncher.create(DefaultGradleLauncher.java:93)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:90)
    at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:62)
    at org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradleLauncher.java:93)
    at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLauncher.java:82)
    at org.gradle.launcher.exec.InProcessBuildActionExecuter$DefaultBuildController.run(InProcessBuildActionExecuter.java:94)
    at org.gradle.tooling.internal.provider.runner.BuildModelActionRunner.run(BuildModelActionRunner.java:46)
    at org.gradle.launcher.exec.ChainingBuildActionRunner.run(ChainingBuildActionRunner.java:35)
    at org.gradle.tooling.internal.provider.runner.SubscribableBuildActionRunner.run(SubscribableBuildActionRunner.java:58)
    at org.gradle.launcher.exec.ChainingBuildActionRunner.run(ChainingBuildActionRunner.java:35)
    at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:43)
    at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:28)
    at org.gradle.launcher.exec.ContinuousBuildActionExecuter.execute(ContinuousBuildActionExecuter.java:78)
    at org.gradle.launcher.exec.ContinuousBuildActionExecuter.execute(ContinuousBuildActionExecuter.java:48)
    at org.gradle.launcher.daemon.server.exec.ExecuteBuild.doBuild(ExecuteBuild.java:52)
    at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.WatchForDisconnection.execute(WatchForDisconnection.java:37)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.ResetDeprecationLogger.execute(ResetDeprecationLogger.java:26)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.RequestStopIfSingleUsedDaemon.execute(RequestStopIfSingleUsedDaemon.java:34)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.ForwardClientInput.call(ForwardClientInput.java:74)
    at org.gradle.launcher.daemon.server.exec.ForwardClientInput.call(ForwardClientInput.java:72)
    at org.gradle.util.Swapper.swap(Swapper.java:38)
    at org.gradle.launcher.daemon.server.exec.ForwardClientInput.execute(ForwardClientInput.java:72)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.health.DaemonHealthTracker.execute(DaemonHealthTracker.java:47)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.LogToClient.doBuild(LogToClient.java:66)
    at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.EstablishBuildEnvironment.doBuild(EstablishBuildEnvironment.java:72)
    at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.health.HintGCAfterBuild.execute(HintGCAfterBuild.java:41)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.StartBuildOrRespondWithBusy.run(StartBuildOrRespondWithBusy.java:50)
    at org.gradle.launcher.daemon.server.DaemonStateCoordinator.run(DaemonStateCoordinator.java:246)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:54)
    at org.gradle.internal.concurrent.StoppableExecutorImpl.run(StoppableExecutorImpl.java:40)
Caused by: java.lang.RuntimeException: com.android.build.api.transform.TransformException: java.lang.RuntimeException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/usr/lib/jvm/java-8-oracle/bin/java'' finished with non-zero exit value 1
    at com.android.builder.profile.Recorder$Block.handleException(Recorder.java:54)
    at com.android.builder.profile.ThreadRecorder.record(ThreadRecorder.java:57)
    at com.android.builder.profile.ThreadRecorder.record(ThreadRecorder.java:47)
    at com.android.build.gradle.internal.pipeline.TransformTask.transform(TransformTask.java:173)
    at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:75)
    at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$IncrementalTaskAction.doExecute(AnnotationProcessingTaskFactory.java:244)
    at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:220)
    at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$IncrementalTaskAction.execute(AnnotationProcessingTaskFactory.java:231)
    at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:209)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:80)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:61)
    ... 70 more
Caused by: com.android.build.api.transform.TransformException: java.lang.RuntimeException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/usr/lib/jvm/java-8-oracle/bin/java'' finished with non-zero exit value 1
    at com.android.build.gradle.internal.transforms.DexTransform.transform(DexTransform.java:425)
    at com.android.build.gradle.internal.pipeline.TransformTask.call(TransformTask.java:178)
    at com.android.build.gradle.internal.pipeline.TransformTask.call(TransformTask.java:174)
    at com.android.builder.profile.ThreadRecorder.record(ThreadRecorder.java:55)
    ... 79 more
Caused by: java.lang.RuntimeException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/usr/lib/jvm/java-8-oracle/bin/java'' finished with non-zero exit value 1
    at com.android.ide.common.internal.WaitableExecutor.waitForTasksWithQuickFail(WaitableExecutor.java:119)
    at com.android.build.gradle.internal.transforms.DexTransform.transform(DexTransform.java:365)
    ... 82 more
Caused by: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/usr/lib/jvm/java-8-oracle/bin/java'' finished with non-zero exit value 1
    at com.android.builder.core.AndroidBuilder.runDexer(AndroidBuilder.java:1473)
    at com.android.builder.core.AndroidBuilder.preDexLibraryNoCache(AndroidBuilder.java:1659)
    at com.android.builder.internal.compiler.PreDexCache.preDexLibrary(PreDexCache.java:119)
    at com.android.builder.core.AndroidBuilder.preDexLibrary(AndroidBuilder.java:1614)
    at com.android.build.gradle.internal.transforms.DexTransform$PreDexTask.call(DexTransform.java:475)
    at com.android.build.gradle.internal.transforms.DexTransform$PreDexTask.call(DexTransform.java:429)
Caused by: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/usr/lib/jvm/java-8-oracle/bin/java'' finished with non-zero exit value 1
    at com.android.builder.core.AndroidBuilder.runDexer(AndroidBuilder.java:1457)
    ... 5 more
Caused by: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/usr/lib/jvm/java-8-oracle/bin/java'' finished with non-zero exit value 1
    at com.android.build.gradle.internal.process.GradleProcessResult.assertNormalExitValue(GradleProcessResult.java:43)
    at com.android.builder.core.AndroidBuilder.call(AndroidBuilder.java:1465)
    at com.android.builder.core.AndroidBuilder.call(AndroidBuilder.java:1457)
Caused by: org.gradle.process.internal.ExecException: Process 'command '/usr/lib/jvm/java-8-oracle/bin/java'' finished with non-zero exit value 1
    at org.gradle.process.internal.DefaultExecHandle$ExecResultImpl.assertNormalExitValue(DefaultExecHandle.java:367)
    at com.android.build.gradle.internal.process.GradleProcessResult.assertNormalExitValue(GradleProcessResult.java:41)
    ... 2 more

更令人沮丧的是,当我使用相同 gradle 任务

的终端命令时,这并没有发生
./gradlew clean generateDebugSources generateDebugAndroidTestSources mockableAndroidJar prepareDebugUnitTestDependencies assembleDebug

apk文件生成的很好 问题出现在 MAC 和 Linux,因此它不是指定的平台,但它没有出现在 Android Studio 1.5

我正在发布我的 gradle.build 文件

非常感谢

    apply plugin: 'com.android.application'
    apply plugin: 'com.neenbedankt.android-apt'
    apply plugin: 'kotlin-android'
    apply plugin: 'kotlin-android-extensions'

    buildscript {
        repositories {
            jcenter()
        }

        dependencies {
            classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
        }
    }


    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.2"

        defaultConfig {
            applicationId "com.wojtek.MyProject"
            minSdkVersion 19
            targetSdkVersion 23
            versionCode 4
            versionName "0.1.3"
            testInstrumentationRunner 'com.wojtek.my_project.AndroidTestRunner'
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
        lintOptions {
            abortOnError false
        }

        testOptions {

            unitTests.returnDefaultValues = true
        }
    }

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])

        compile "com.android.support:support-annotations:$support_version"
        compile "com.android.support:appcompat-v7:$support_version"
        compile "com.android.support:recyclerview-v7:$support_version"
        compile "com.android.support:cardview-v7:$support_version"
        compile "com.android.support:design:$support_version"
        compile "com.android.support:palette-v7:$support_version"

        compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
        compile "io.reactivex:rxkotlin:$rx_kotlin"
        compile "org.jetbrains.anko:anko-sqlite:$anko_version"
        compile 'io.reactivex:rxandroid:1.1.0'
        compile "com.squareup.retrofit2:retrofit:$retrofitVersion"
        compile 'com.squareup.okhttp3:logging-interceptor:3.0.1'

        compile "com.squareup.retrofit2:converter-gson:$retrofitVersion"
        compile "com.squareup.retrofit2:adapter-rxjava:$retrofitVersion"

        apt "com.google.dagger:dagger-compiler:${daggerVersion}"
        compile "com.google.dagger:dagger:${daggerVersion}"
        provided 'org.glassfish:javax.annotation:10.0-b28'

        compile 'com.squareup.picasso:picasso:2.5.2'


        //TEST******************************************************************************************
        testCompile 'junit:junit:4.12'
        testCompile 'org.mockito:mockito-core:1.10.19'

        androidTestCompile "com.android.support:support-annotations:$support_version"
    androidTestCompile "com.android.support.test.espresso:espresso-core:$ESSPRESSO_VERSION"

    androidTestCompile ("com.android.support.test.espresso:espresso-contrib:$ESSPRESSO_VERSION"){
        exclude group: 'com.android.support', module: 'appcompat'
        exclude group: 'com.android.support', module: 'support-v4'
        exclude module: 'recyclerview-v7'
    }

    androidTestCompile 'com.android.support.test:runner:0.4.1'
    androidTestCompile 'com.android.support.test:rules:0.4.1'

    androidTestCompile 'org.mockito:mockito-core:1.10.19'
    androidTestCompile 'com.google.dexmaker:dexmaker:1.2'
    androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.2'

    androidTestApt "com.google.dagger:dagger-compiler:$daggerVersion"


}

添加

dexOptions {
    javaMaxHeapSize "4g"
}

在android

我在 linux 上遇到了同样的问题。出于某种原因,/build 中的某些文件和目录无法删除,甚至 'sudo -rf' 也无法删除,这就是我问题的根源。我在 MacBookPro 上构建了这个项目,没问题。

几个星期以来,我一直在努力解决这个问题。

现在我找到了解决方法。如果有人遇到同样的问题,在 Android Studio 设置中关闭即时 运行 将会有所帮助。

我知道这不是解决方案,但这是目前最好的办法。

干杯 沃伊泰克

当我遇到这个问题时,我会重新启动 Android Studio (2.0)。然后就可以了。无需编辑 gradle 个文件或关闭即时 运行。

attempt to set or access a value of type java.lang.Object using a local variable of type int

ProGuard 似乎在某些地方优化了变量分配,但做错了。

尝试通过在 proguard-rules.pro:

中添加以下行来禁用此优化

-optimizations !code/allocation/variable

对我来说,在 gradle.properties 中添加这一行(或注释掉,因为它写在第 13 行)有效:

org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

我还关闭了 Instant 运行(Android Studio 2.1.2)。

接受的答案可能有效,但我会与我分享修复此问题的方法。这是由于添加额外的库依赖后达到了最大方法数。

请参阅 this guide here 进行修复,跳至 使用 Gradle 为 Multidex 配置您的应用程序部分进入正题。

 android {
    compileSdkVersion 23
    buildToolsVersion "24.0.0"

    defaultConfig {
        ...
        minSdkVersion 14
        targetSdkVersion 23
        ...

        // Enabling multidex support.
        multiDexEnabled true
    }
    ... }

    dependencies {   compile 'com.android.support:multidex:1.0.0' }

    repositories {
    mavenCentral() }


    <application    ...
    android:largeHeap="true"
    android:supportsRtl="true"
    android:name="android.support.multidex.MultiDexApplication"> </application>

我只需要将以下几行添加到移动应用程序的 build.gradle 文件中即可解决此问题:

...

android {

    ...

    // Enabling multidex support.
    multiDexEnabled true
}

dexOptions {
    javaMaxHeapSize "4g"
}

...

}

依赖项{

...

compile 'com.android.support:multidex:1.0.0'

...

}

以上

None 对我有用,但这个 developer.android.come 指南对我有用:

https://developer.android.com/studio/build/multidex.html

内容如下:

如果您的 minSdkVersion 设置为 21 或更高,您需要做的就是在模块级别 build.gradle 文件中将 multiDexEnabled 设置为 true,如 enter code here 所示:

android {
    defaultConfig {
        ...
        minSdkVersion 21 
        targetSdkVersion 25
        multiDexEnabled true
    }
    ...
}

但是,如果您的 minSdkVersion 设置为 20 或更低,那么您必须使用 multidex 支持库,如下所示:

修改模块级 build.gradle 文件以启用 multidex 并将 multidex 库添加为依赖项,如下所示:

android {
    defaultConfig {
        ...
        minSdkVersion 15 
        targetSdkVersion 25
        multiDexEnabled true
    }
    ...
}

dependencies {
  compile 'com.android.support:multidex:1.0.1'
}

Refactoring 库帮助我解决了这个问题。这一定有助于从 app 模块中删除重复的 类。

     defaultConfig {

    minSdkVersion 16
    targetSdkVersion 25  

    multiDexEnabled true
}

只需在您应用的 build.gradle 文件中进行设置

我发现了很多解决方案,但我通过添加解决了这个问题:

太阳 1:build.gradle:

defaultConfig {
    multiDexEnabled true
}

清理您的项目并重建。

Sol 2: 如果您在 local.properties 中仍有问题,请添加,

org.gradle.jvmargs=-XX\:MaxHeapSize\=512m -Xmx512m

Sol 3 尽管如此,您仍然有问题添加下面提到的依赖项:

compile 'com.android.support:multidex:1.0.1'

任何解决方案都一定适合您。 否则将所有 3 个都添加到您的申请中。

这对我有用,

  1. 在build.gradle, 将您当前的 android 版本作为示例 3.0.1

    依赖项{ 类路径 'com.android.tools.build:gradle:3.0.1'}

  2. 在gradle-wrapper.properties, 更改您的 gradle 版本,例如 gradle-4.4 distributionUrl=https://services.gradle.org/distributions/gradle-4.4-all.zip

希望对您有所帮助!