升级Gradle至4.4后构建APK报错:废弃API与主Dex列表生成失败
Hey there, let's tackle these two linked issues you're facing after upgrading Gradle to 4.4. Both the deprecated API warnings and the multidex build failure can be resolved with targeted steps—let's break it down:
1. First: Get Details on Deprecated APIs
The "Some input files use or override a deprecated API" warning means parts of your code (or dependencies) are relying on outdated Android/Java APIs. To see exactly which classes or methods are problematic, add the -Xlint:deprecation compiler argument to your app-level build.gradle to force detailed warnings:
android { compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 // Add this line to show full deprecation details in build logs compilerArgs << "-Xlint:deprecation" } // Optional: Make deprecation warnings more prominent lintOptions { warning 'deprecation' // Uncomment below if you want to enforce fixing deprecations by treating them as errors // error 'deprecation' } }
Rebuild your project, and the log will now show specific lines of code (or dependency classes) using deprecated APIs. Replace those with modern alternatives—this might even resolve the multidex issue indirectly, as outdated code can sometimes cause dex list generation glitches.
2. Fix the Multidex List Generation Failure
The TransformException: Error while generating the main dex list error usually comes from misconfigured multidex, cached build artifacts, or dependency conflicts. Try these fixes in order:
Verify Multidex Configuration (if your
minSdkVersionis < 21):
Enable multidex in your app-levelbuild.gradle:android { defaultConfig { // ... other existing configs minSdkVersion 19 // Example: any version below 21 multiDexEnabled true } } dependencies { // Use this for non-AndroidX projects implementation 'com.android.support:multidex:1.0.3' // For AndroidX projects, use this instead: // implementation 'androidx.multidex:multidex:2.0.1' }Then update your Application class:
- Either extend
MultiDexApplication:public class MyApp extends MultiDexApplication { // ... your app's existing logic } - Or if you can't extend
MultiDexApplication, add this to your existing Application class:@Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); }
Don't forget to reference your custom Application class in
AndroidManifest.xmlif you haven't already.- Either extend
Clean Build Artifacts
Cached files often cause weird multidex issues. Run these commands in your project root:./gradlew clean ./gradlew assembleDebugAlternatively, in Android Studio, go to File > Invalidate Caches / Restart and select "Invalidate and Restart".
Check for Dependency Conflicts
Duplicate or conflicting dependencies can break dex list generation. Generate a dependency tree to spot issues:./gradlew app:dependenciesLook for duplicate libraries (e.g., multiple versions of support libraries) and exclude duplicates using
exclude group: 'com.example', module: 'conflicting-library'in your dependency declarations.Ensure Gradle Plugin Compatibility
Gradle 4.4 is designed to work with Android Gradle Plugin (AGP) version 3.1.x. Verify your project-levelbuild.gradleuses a compatible AGP version:buildscript { repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.1.4' // Matches Gradle 4.4 } }Customize Multidex Keep Rules (If Needed)
If certain classes must be included in the main dex file (e.g., Application class, startup dependencies), create amultidex-config.txtfile in your app module root with lines like:com/yourpackage/MyApp.class com/yourpackage/important/StartupClass.classThen reference it in your app-level
build.gradle:android { buildTypes { debug { multiDexKeepFile file('multidex-config.txt') } } }
Final Notes
Start by fixing the deprecated API issues first—resolving those often clears up underlying build inconsistencies. If the multidex error persists after that, work through the multidex configuration and cache steps above.
内容的提问来源于stack exchange,提问作者Mukesh Makavana

