升级Android Studio至3.1.1后应用无法运行求助
Hey there, let's tackle this annoying Dex-related build error you're facing post-upgrade. I’ve seen this pop up often when moving to a new AS version, especially after tweaking code. Here are some targeted fixes to try:
1. Verify Gradle Plugin & Gradle Version Compatibility
Android Studio 3.1.1 pairs specifically with Gradle Plugin 3.1.1 and Gradle 4.4 or higher. Mismatched versions are a common culprit here:
- Open your project-level
build.gradleand confirm this line exists in thedependenciesblock:classpath 'com.android.tools.build:gradle:3.1.1' - Next, check
gradle/wrapper/gradle-wrapper.propertiesto ensure the distribution URL points to the right Gradle version:distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip - Sync your project and try building again.
2. Enable MultiDex (If You Hit the 64k Method Limit)
Upgrading dependencies or adding new code might push your app over the 64k method count threshold. Here’s how to enable MultiDex:
- In your app-level
build.gradle, addmultiDexEnabled trueinside thedefaultConfigblock:defaultConfig { // ... other configs multiDexEnabled true } - Add the MultiDex dependency to your
dependenciesblock (use the AndroidX version if you’ve migrated):// For Support Library implementation 'com.android.support:multidex:1.0.3' // For AndroidX implementation 'androidx.multidex:multidex:2.0.1' - If you have a custom
Applicationclass, make it extendMultiDexApplication, or overrideattachBaseContextto callMultiDex.install(this).
3. Do a Deep Clean & Rebuild
The standard Clean/Rebuild might not clear all cached cruft. Try these steps:
- Delete the
app/buildfolder from your project directory. - Delete the Gradle cache folder:
- On macOS/Linux:
~/.gradle/caches - On Windows:
C:\Users\<YourUsername>\.gradle\caches
- On macOS/Linux:
- Restart Android Studio, then sync and rebuild your project from scratch.
4. Hunt for Dependency Conflicts
Version mismatches in dependencies (like Support Library or third-party libraries) can break Dex processing. Use Gradle’s dependency report to spot issues:
- Open the terminal in your project root and run:
# macOS/Linux ./gradlew app:dependencies # Windows gradlew app:dependencies - Look for duplicate dependencies or inconsistent versions (e.g., all Support Library dependencies should use the same version—27.1.1 is a safe match for AS 3.1.1).
5. Disable Instant Run
Instant Run can glitch after AS upgrades. Try turning it off:
- Go to
File > Settings > Build, Execution, Deployment > Instant Run - Uncheck the box labeled "Enable Instant Run to hot swap code/resource changes on deploy"
- Restart Android Studio and try running your app again.
If none of these work, it’d help to share the full error stack trace (your current error is truncated at transformDexWithInstan...). The complete message will pinpoint exactly where the Dex process is failing.
内容的提问来源于stack exchange,提问作者user2101081

