Android WebView 对于除 `google.com` 之外的所有 URL 都会崩溃

问题描述 投票:0回答:1

我想在 Android 应用程序中有一个网站。所以我制作了一个非常简单的应用程序(来自this youtube教程),如果我将

grafanaWebsite.loadUrl("https://google.com/")
放入类文件中,我就会得到谷歌页面。 如果放置 任何其他 url,甚至
https://www.google.com/"
http://google.com/
,我的应用程序在尝试启动它时会立即崩溃,请参阅下面的 Logcat。 (仍然没有找到任何其他工作网址。) 最后,我想加载
http://192.168.xxx.xxx:3000/
。我究竟做错了什么 ? 谢谢!

这是

AndroidManifest.xml
MainActivity.java
:

<?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>
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'
}
java android android-studio android-webview
1个回答
0
投票

此错误很可能是由旧版 Android Studio 与 Android 14+ 设备结合使用导致的:

Android 14 设备不再允许从应用程序具有写入权限的位置加载“可执行代码”,例如 dex 文件。

Android Studio 包含一个名为“即时运行”的功能,该功能允许将代码更新修补到正在运行的应用程序中,因此如果您在 Android Studio 中按“运行”,则只能上传代码更改而不是整个 APK 文件。特别是对于大型 APK 文件,这可以节省大量时间。

旧的 Android Studio 版本似乎将代码更改写入像

/data/data/<packagename>/code_cache/.overlay/base.apk/classes?.dex
这样的文件中,自 Android 14 以来不再允许这样做。

要解决此问题,您必须将使用的 Android Studio 版本至少更新到 Android Studio Hedgehog。

或者,如果无法或想要更新 Android Studio,您可以尝试通过 Android Studio 设置禁用“即时运行”功能。

或者您不使用运行按钮,而是通过命令行安装应用程序:

./gradlew installDebug

© www.soinside.com 2019 - 2024. All rights reserved.