Android Studio更新后Socket通信报错:NullPointerException及libcore.util.Objects无法解析
Hey there, let's break down this frustrating issue you're hitting after updating Android Studio. That java.lang.NullPointerException when trying to evaluate StructAddrinfo.toString()—plus the unresolved libcore.util.Objects—points to a few common culprits tied to SDK compatibility, cached data, or outdated code patterns. Here's how to tackle it step by step:
1. Rule Out Android Studio Cache Corruption
First off, updates often leave behind wonky cached data that messes up class resolutions. Let's clear that out:
- Go to
File > Invalidate Caches...and select Invalidate and Restart. - If that doesn't work, manually delete the
.build,.gradlefolders in your project root, and thebuildfolder inside your app module. Then re-sync your project with Gradle.
2. Check SDK Version Compatibility
The android.system.StructAddrinfo class is part of Android's internal API, and its behavior can shift between SDK versions. After updating Studio, your project might have auto-upgraded compileSdk or targetSdk to a version where this class has breaking changes:
- Open your app's
build.gradle(Module level) and check the values forcompileSdk,targetSdk, andminSdk. - Try rolling back
compileSdkto the version that worked before the update. For example, if you jumped to SDK 34, revert to SDK 33 temporarily to see if the error disappears. - Note: If you're targeting SDK 30+, some internal APIs like
android.systemhave restricted access. You might need to adjust your code to avoid direct reliance on these classes.
3. Fix the Socket Initialization Code
The null pointer is triggering when resolving the address for your Socket. Instead of relying on the internal StructAddrinfo logic, switch to a more stable initialization pattern using InetAddress:
Replace your existing Socket setup (like direct new Socket(host, port)) with:
String raspberryPiHost = "your-rpi-ip-or-hostname"; int port = 1234; // your server port try { InetAddress serverAddress = InetAddress.getByName(raspberryPiHost); Socket sock = new Socket(serverAddress, port); // Rest of your data sending logic } catch (IOException e) { e.printStackTrace(); }
This bypasses the problematic StructAddrinfo path entirely and uses a more standard, SDK-compatible way to resolve the server address.
4. Verify ProGuard/R8 Configuration
If you're using code obfuscation, it's possible that libcore.util.Objects or StructAddrinfo is being stripped accidentally. Add these rules to your proguard-rules.pro file:
-keep class libcore.util.Objects { *; } -keep class android.system.StructAddrinfo { *; }
This ensures the obfuscator doesn't remove or rename these critical classes.
5. Update Android Studio to the Latest Stable Build
Sometimes the issue is a bug in the specific Android Studio version you updated to. Head to Help > Check for Updates and install the latest stable release—this might resolve compatibility gaps between the IDE and SDK tools.
内容的提问来源于stack exchange,提问作者Todd

