添加SQLCipher依赖后低版本Android出现SoapSerializationEnvelope类找不到错误
Alright, let's figure out why adding SQLCipher's AAR is causing that NoClassDefFoundError for org.ksoap2.serialization.SoapSerializationEnvelope on Android 4.0/4.4 but not on newer versions. I've run into similar issues before, so here's the breakdown and fixes:
The key difference lies in how older vs newer Android runtimes handle dex files. Android 4.4 and below use the Dalvik runtime, which can only load a single dex file by default. When you add the SQLCipher AAR, it probably pushes your app's total method count over the 65,536 limit (the infamous "64k method limit").
Android 5.0+ uses the ART runtime, which natively supports multiple dex files, so it doesn't hit this problem. On Dalvik, once you exceed the method limit, only a subset of your app's classes get loaded—hence why the ksoap2 class goes missing.
This is the standard fix for the 64k method limit on older Android versions. Here's how to set it up step by step:
Update your module-level
build.gradle
Add themultiDexEnabledflag and include the multidex library:android { defaultConfig { // Turn on multidex support multiDexEnabled true } } dependencies { // For apps using the old support library implementation 'com.android.support:multidex:1.0.3' // If you're using AndroidX, use this instead // implementation 'androidx.multidex:multidex:2.0.1' }Configure your Application class
- If you already have a custom
Applicationclass, overrideattachBaseContextto install multidex:public class YourApp extends Application { @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } } - If you don't have a custom
Application, just extendMultiDexApplicationinstead of the regularApplication:public class YourApp extends MultiDexApplication { // Your existing app logic goes here }
- If you already have a custom
Update your
AndroidManifest.xml
Make sure your manifest points to your custom Application class:<application android:name=".YourApp" <!-- other app attributes --> > <!-- Your activities, services, etc. --> </application>
While unlikely, it's possible SQLCipher's AAR includes a conflicting version of ksoap2 that's causing class loading issues on older devices. To check:
- Run this command in your project root to generate a dependency tree:
Look for any ksoap2 references under./gradlew app:dependenciesnet.zetetic:android-database-sqlcipher:3.5.9. If you find any, exclude them in your build.gradle:implementation('net.zetetic:android-database-sqlcipher:3.5.9@aar') { exclude group: 'org.ksoap2', module: 'ksoap2-android' }
Build your app and test it on an Android 4.0 or 4.4 device. The NoClassDefFoundError should disappear once multidex is properly configured.
内容的提问来源于stack exchange,提问作者Lokesh Deshmukh

