在更新android studio之前,我的项目编译良好。但是,当我将android studio更新到v3.5之后,我无法编译我的项目。我收到以下错误消息:
Cannot fit requested classes in a single dex file (# methods: 72945 > 65536)
我什至不能更改一行代码。
我的礼物:
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.app"
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
def retrofitVersion = "2.5.0"
def supportVersion = '1.0.0-beta01'
def glideVersion = "4.8.0"
def rxjava_version = '2.2.8'
def rxandroid_version = '2.1.1'
def dagger_version = "2.19"
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"
implementation "com.squareup.retrofit2:converter-gson:$retrofitVersion"
implementation "com.squareup.okhttp3:logging-interceptor:3.10.0"
def lifecycle_version = '2.1.0-alpha04'
// ViewModel and LiveData
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
implementation "com.google.android.material:material:$supportVersion"
def reactivestreams_version = '2.0.0-beta01'
implementation "androidx.lifecycle:lifecycle-reactivestreams:$reactivestreams_version"
def rxcalladapter_version = '2.5.0'
implementation "com.squareup.retrofit2:adapter-rxjava2:$rxcalladapter_version"
// Glide
implementation "com.github.bumptech.glide:glide:$glideVersion"
annotationProcessor "com.github.bumptech.glide:compiler:$glideVersion"
implementation("com.github.bumptech.glide:recyclerview-integration:$glideVersion") {
transitive = false
}
implementation 'de.hdodenhof:circleimageview:3.0.0'
def room_version = '2.1.0-alpha07'
// Room
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
implementation('io.socket:socket.io-client:1.0.0') {
// excluding org.json which is provided by Android
exclude group: 'org.json', module: 'json'
}
implementation 'com.google.code.gson:gson:2.8.5'
// RxJava
implementation "io.reactivex.rxjava2:rxjava:$rxjava_version"
// RxAndroid
implementation "io.reactivex.rxjava2:rxandroid:$rxandroid_version"
implementation 'androidx.room:room-rxjava2:2.0.0-beta01'
// Dagger
// def dagger_version = "2.16" // version used by google samples
// Dagger2 core
implementation "com.google.dagger:dagger:$dagger_version"
annotationProcessor "com.google.dagger:dagger-compiler:$dagger_version"
// Dagger Android
implementation "com.google.dagger:dagger-android:$dagger_version"
implementation "com.google.dagger:dagger-android-support:$dagger_version"
annotationProcessor "com.google.dagger:dagger-android-processor:$dagger_version"
implementation 'com.makeramen:roundedimageview:2.3.0'
implementation 'pub.devrel:easypermissions:3.0.0'
implementation 'com.theartofdev.edmodo:android-image-cropper:2.6.+'
//Camera library
implementation 'com.camerakit:camerakit:1.0.0-beta3.11'
implementation 'com.camerakit:jpegkit:0.1.0'
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.0'
//Image Picker
implementation 'com.github.esafirm.android-image-picker:imagepicker:2.0.0'
//Audio recorder
def audio_recorder_version = "1.6.0"
implementation "com.github.piasy:rxandroidaudio:$audio_recorder_version"
implementation "com.github.piasy:AudioProcessor:$audio_recorder_version"
implementation 'com.hhl:gridpagersnaphelper:1.0.0'
implementation 'com.hhl:recyclerviewindicator:1.0.0'
implementation 'com.h6ah4i.android.widget.advrecyclerview:advrecyclerview:1.0.0'
implementation 'com.github.ahmadmuzakki29:subtitle-collapsingtoolbar:v2'
implementation 'com.airbnb.android:lottie:3.0.7'
implementation 'com.android.billingclient:billing:2.0.1'
implementation 'com.jsibbold:zoomage:1.3.0'
implementation 'me.relex:circleindicator:2.1.4'
implementation 'com.facebook.shimmer:shimmer:0.4.0'
implementation 'com.google.firebase:firebase-analytics:17.2.0'
implementation 'com.google.firebase:firebase-messaging:20.0.0'
}
我研究了stackoverflow中的dex文件错误,有人说“启用即时运行为我解决了这个问题”,我怀疑即时运行是因为他们已经更改了即时运行机制并从android studio中删除了即时运行设置。也许这是问题所在?
在不启用multiDex支持的情况下如何解决此问题?
答案 0 :(得分:0)
没有Multidex,我们将不得不剥离未使用的库。
请在您的app / build.gradle文件的底部添加以下内容
def toCamelCase(String string) {
String result = ""
string.findAll("[^\\W]+") { String word ->
result += word.capitalize()
}
return result
}
afterEvaluate { project ->
Configuration runtimeConfiguration = project.configurations.getByName('compile')
ResolutionResult resolution = runtimeConfiguration.incoming.resolutionResult
// Forces resolve of configuration
ModuleVersionIdentifier module = resolution.getAllComponents().find { it.moduleVersion.name.equals("play-services") }.moduleVersion
String prepareTaskName = "prepare${toCamelCase("${module.group} ${module.name} ${module.version}")}Library"
File playServiceRootFolder = project.tasks.find { it.name.equals(prepareTaskName) }.explodedDir
Task stripPlayServices = project.tasks.create(name: 'stripPlayServices', group: "Strip") {
inputs.files new File(playServiceRootFolder, "classes.jar")
outputs.dir playServiceRootFolder
description 'Strip useless packages from Google Play Services library to avoid reaching dex limit'
doLast {
copy {
from(file(new File(playServiceRootFolder, "classes.jar")))
into(file(playServiceRootFolder))
rename { fileName ->
fileName = "classes_orig.jar"
}
}
tasks.create(name: "stripPlayServices" + module.version, type: Jar) {
destinationDir = playServiceRootFolder
archiveName = "classes.jar"
from(zipTree(new File(playServiceRootFolder, "classes_orig.jar"))) {
exclude "com/google/ads/**"
exclude "com/google/android/gms/analytics/**"
exclude "com/google/android/gms/games/**"
exclude "com/google/android/gms/plus/**"
exclude "com/google/android/gms/drive/**"
exclude "com/google/android/gms/ads/**"
exclude "com/google/android/gms/wallet/**"
exclude "com/google/android/gms/wearable/**"
}
}.execute()
delete file(new File(playServiceRootFolder, "classes_orig.jar"))
}
}
project.tasks.findAll { it.name.startsWith('prepare') && it.name.endsWith('Dependencies') }.each { Task task ->
task.dependsOn stripPlayServices
}
}
在此示例中,我们将剥离分析,游戏,驱动器,广告,钱包,可穿戴代码和功能。 您应该手动添加不需要的库/软件包。因此,整个功能计数在64K范围内。
另一种方法是使用此库(旧):https://github.com/casidiablo/multidex
答案 1 :(得分:0)
这很不寻常,您不想启用multidex-在这种情况下,这是最佳选择。但是,还有更多选项可以使它相对容易:
minSdkVersion
从19
更改为21
-这样,运行时必须能够自行处理65k限制。 Documentation minifyEnabled
从false
更改为true
-如果您有幸运的保镖,则r8会将您的项目缩小到足以容纳65k的程度。这样,您可能需要创建带有最小化规则的文件。 Documentation {
transitive = false;
}
添加到所有可能的依赖项中-希望所有需要的依赖项都存在于项目中,而不是您使用此命令忽略的那些内部依赖项。 Documentation 您还可以尝试使用gradle build config进行操作以手动收缩类,但这不是一个好主意,因为对于gradle的不同版本,您可能需要使用不同的方法(因为弃用,新的API等)。同样,这样一来,您可能会删除过多内容,并在以后收到调试的噩梦。
希望它会以某种方式帮助您。
答案 2 :(得分:0)
如果您想远离multiDex
,则有两种选择。实际上是一个选项,另一个只是主选项的残缺形式。顺便说一句,我只是复制粘贴了您所有的gradle
文件并对其进行了测试。
选项1。(推荐)
步骤1 。为您的调试创建一个proguard
文件。假设debug-proguard-rules.pro
并将以下行添加到调试proguard文件。这行代码将强制编译器在调试构建期间将类和类成员的名称保持不变,这非常有帮助:
-dontobfuscate
第2步。将debug
构建类型添加到gradle中。这将同时应用proguard
和proguard-rules.pro
文件中的proguard-rules-debug.pro
规则。
buildTypes {
debug {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'),
'proguard-rules.pro', 'proguard-rules-debug.pro'
}
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'),
'proguard-rules.pro'
}
}
选项2。
如果您不关心自己的班级和班级成员在debug
版本中被混淆,那么只需将以下行添加到您的build types
:
buildTypes {
debug {
minifyEnabled true
}
release {
//........
}
}
答案 3 :(得分:-1)
修改app / build.gradle文件以启用multidex并将multidex库添加为依赖项,如下所示:
android {
defaultConfig {
...
minSdkVersion 15
targetSdkVersion 28
multiDexEnabled true
}
...
}
dependencies {
implementation 'com.android.support:multidex:1.0.3'
}
详细信息可以在这里找到:https://developer.android.com/studio/build/multidex