Android WebView加载除google.com外所有URL均崩溃求助
我想在Android应用里嵌入网页,跟着YouTube教程做了个简单应用。用grafanaWebsite.loadUrl("https://google.com/")时能正常加载谷歌页面,但换成其他任意URL(比如https://www.google.com/、http://google.com/),应用启动就崩溃,最终我要加载的是http://192.168.xxx.xxx:3000/,求排查错误。
以下是相关代码和日志:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.example.pvmonitoring"> <uses-permission android:name="android.permission.INTERNET"/> <application android:usesCleartextTraffic="true" android:allowBackup="true" android:dataExtractionRules="@xml/data_extraction_rules" android:fullBackupContent="@xml/backup_rules" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.PVMonitoring" tools:targetApi="31"> <activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
MainActivity.java
package com.example.pvmonitoring; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.webkit.WebView; import android.webkit.WebViewClient; public class MainActivity extends AppCompatActivity { WebView grafanaWebsite; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); grafanaWebsite = findViewById(R.id.grafanaWebsite); grafanaWebsite.getSettings().setJavaScriptEnabled(true); grafanaWebsite.setWebViewClient(new WebViewClient()); grafanaWebsite.loadUrl("https://www.google.com/"); } }
崩溃Logcat输出
2024-04-09 15:13:09.832 5963-5963/com.example.pvmonitoring E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.pvmonitoring, PID: 5963 java.lang.SecurityException: Writable dex file '/data/data/com.example.pvmonitoring/code_cache/.overlay/base.apk/classes3.dex' is not allowed. at dalvik.system.DexFile.openDexFileNative(Native Method) at dalvik.system.DexFile.openDexFile(DexFile.java:406) at dalvik.system.DexFile.<init>(DexFile.java:128) at dalvik.system.DexFile.<init>(DexFile.java:101) at dalvik.system.DexPathList.loadDexFile(DexPathList.java:438) at dalvik.system.DexPathList.makeDexElements(DexPathList.java:387) at dalvik.system.DexPathList.<init>(DexPathList.java:166) at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:160) at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:130) at dalvik.system.PathClassLoader.<init>(PathClassLoader.java:146) at com.android.internal.os.ClassLoaderFactory.createClassLoader(ClassLoaderFactory.java:93) at com.android.internal.os.ClassLoaderFactory.createClassLoader(ClassLoaderFactory.java:134) at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:126) at android.app.ApplicationLoaders.getClassLoaderWithSharedLibraries(ApplicationLoaders.java:61) at android.app.LoadedApk.createOrUpdateClassLoaderLocked(LoadedApk.java:1051) at android.app.LoadedApk.getClassLoader(LoadedApk.java:1143) at android.app.LoadedApk.getResources(LoadedApk.java:1428) at android.app.ContextImpl.createAppContext(ContextImpl.java:3487) at android.app.ContextImpl.createAppContext(ContextImpl.java:3479) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7539) at android.app.ActivityThread.-$$Nest$mhandleBindApplication(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2478) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:230) at android.os.Looper.loop(Looper.java:319) at android.app.ActivityThread.main(ActivityThread.java:8893) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:608) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)
build.gradle (Module)
plugins { id 'com.android.application' } android { compileSdk 34 defaultConfig { applicationId "com.example.pvmonitoring" minSdk 24 targetSdk 34 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } 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 { implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'com.google.android.material:material:1.10.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' }
这个崩溃和你加载的URL无关,是Android系统的Dex文件优化缓存出了问题,按以下步骤修复:
清理应用缓存和数据
打开设备设置 → 应用 → 找到PVMonitoring应用 → 存储 → 点击「清除缓存」和「清除数据」清理Android Studio构建缓存
在Android Studio菜单栏点击File→Invalidate Caches...→ 勾选Invalidate and Restart,重启IDE后重新构建项目禁用Dex优化覆盖(临时方案)
在AndroidManifest.xml的<application>标签里添加android:useEmbeddedDex="false",强制系统不使用优化后的Dex覆盖文件:<application ... android:useEmbeddedDex="false" ...>重启设备
部分定制ROM的Dex优化策略可能异常,重启设备后再运行应用
完成上述步骤后,再尝试加载目标URLhttp://192.168.xxx.xxx:3000/——你的WebView已经正确开启JavaScript和明文流量支持,只要网络连通正常就能加载。
内容的提问来源于stack exchange,提问作者Stijn B

