最近被这个问题卡了整整两天,期间搜索了各种回答,感觉StackOverflow中许多开发者和我遇到的问题很像,但是依照他们的解决方法去做总是解决不了问题qwq

今天一个偶然的机会,终于解决了这个问题,原来是我之前看了有些不太规范的回答,导致我的gradle.build文件里面有一些很关键的地方写错了,今天恰好看到了一个StackOverflow的帖子对这个细节进行描述,最终解决了问题。

解决过程如下:

之前我的gradle.build文件是这样的

完美解决:app:transformClassesWithDexForDebug_github

请仔细看这个地方

完美解决:app:transformClassesWithDexForDebug_ide_02

这个地方其实是有问题的,问题在哪里呢,就是这个

完美解决:app:transformClassesWithDexForDebug_github_03

运行起来的结果就是...红红火火

完美解决:app:transformClassesWithDexForDebug_Android_04

真的是忧伤

但是去掉之后,it works like a charm...

我现在的build.gradle是这样的

完美解决:app:transformClassesWithDexForDebug_android_05

附上源码,可供大家参考(这个是没有error版的)

apply plugin: 'com.android.application'

android {
compileSdkVersion 27
buildToolsVersion '27.0.0 '
defaultConfig {
applicationId "com.example.administrator.wrongtry"
minSdkVersion 21
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
aaptOptions.cruncherEnabled = false
aaptOptions.useNewCruncher = false

/*dexOptions {
//javaMaxHeapSize "2048M"
}*/
}


repositories {
maven {
url 'https://github.com/uPhyca/stetho-realm/raw/master/maven-repo'
}
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
//compile files('libs/android-support-v4.jar')

// MPAndroidChart
compile 'com.github.PhilJay:MPAndroidChart:v3.0.3'
// RecycleView


//noinspection GradleCompatible
compile'com.android.support:appcompat-v7:25.3.1'
compile'com.android.support.constraint:constraint-layout:1.0.2'
compile'com.android.support:recyclerview-v7:27.0.0'
compile'com.github.PhilJay:MPAndroidChart:v3.0.3'
compile'com.facebook.stetho:stetho:1.4.1'
compile 'com.uphyca:stetho_realm:2.0.0'
compile 'com.github.PhilJay:MPAndroidChart-Realm:v3.0.3@aar'
compile 'com.android.support:cardview-v7:27.0.0'
compile 'com.android.support:design:27.0.0'
compile 'de.hdodenhof:circleimageview:2.1.0'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support:support-v4:27.0.0'
testCompile 'junit:junit:4.12'

compile 'com.jakewharton:butterknife:8.4.0'
compile project(':lib')
compile project(':qmui')
compile 'com.android.support:support-v4:27.0.0'

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

apply plugin: 'realm-android'

下面详细说一下原因



完美解决:app:transformClassesWithDexForDebug_github_06

也就是说,dexOptions{ ... }这个东西是可以放在两个不同的地方的

一个是android{ ... }的这个大括号里面

另一个外面没有东西,直接放

这两种放的方式是不一样的,里面可以放的东西也不一样

我之前就是一个错误示范

切记,如果要在gradle里面加东西(相当于配置工作),当你看到网上有一条语句要你加进去,你最好自己打出来,看看有没有这个候选项的。我之前那个就是,你把那句错的写进去,gradle也是可以sync的,没有问题,那句话也没有标红,但是编译就是有错,你还找不出错在哪里,真是气啊qwq