java.lang.ClassNotFoundException 尝试使用 GeckoView 制作浏览器时

java.lang.ClassNotFoundException when trying to make a browser with GeckoView

我是 java 编程的新手。 运行 在尝试使用本地存储的 GECKOVIEW 库创建浏览器时出现错误消息。构建确实完成了,但应用程序崩溃了。非常感谢您的帮助,因为我很困惑,而且事实证明很难找到有关 GeckoView 出现的问题的信息。

错误信息:

FATAL EXCEPTION: main Process: com.example.browser, PID: 12465  
 java.lang.NoClassDefFoundError: Failed resolution of: 
Lorg/yaml/snakeyaml    /constructor/Constructor;**strong text**

Caused by: java.lang.ClassNotFoundException: Didn't find class 
"org.yaml.snakeyaml.constructor.Constructor" on path: DexPathList[[zip file 
"/data/app/com.example.browser-REepjBL5JVM2lFFcNMNHJA==
/base.apk"],nativeLibraryDirectories=[/data/app/com.example.browser-
REepjBL5JVM2lFFcNMNHJA==/lib/arm64, /data/app/com.example.browser-
REepjBL5JVM2lFFcNMNHJA==/base.apk!/lib/arm64-v8a, /system/lib64, /product    /lib64]]

(完整的错误信息在底部)

构建 Gradle:APP:

   apply plugin: 'com.android.application'

android {
compileSdkVersion 29
buildToolsVersion "29.0.3"

defaultConfig {
    applicationId "com.example.browser"
    minSdkVersion 29
    targetSdkVersion 29
    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 fileTree(dir: 'libs', include: ['*.jar'])

implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation(name:'gclib',ext:'aar')
}

GeckoView 库已复制粘贴到项目库文件夹中。

构建Gradle项目:

// Top-level build file where you can add configuration options common to 
all sub-projects/modules.

buildscript {

repositories {
    google()
    jcenter()

}
dependencies {
    classpath 'com.android.tools.build:gradle:3.6.3'


    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}
}

allprojects {
repositories {
    google()
    jcenter()
    flatDir{
        dirs 'libs'
    }

}
}

task clean(type: Delete) {
delete rootProject.buildDir
}

主要ACTIVITY:

public class MainActivity extends AppCompatActivity {

private GeckoView geckoview;
private GeckoSession geckoSession;
private GeckoRuntime geckoRuntime;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    geckoview = findViewById(R.id.geckoView);

    geckoSession = new GeckoSession();
    geckoSession.getSettings().setUseTrackingProtection(true);
    geckoSession.getSettings().getUsePrivateMode();
    geckoSession.getSettings().setUserAgentMode(0);
    GeckoRuntimeSettings.Builder builder = new GeckoRuntimeSettings.Builder()
            .aboutConfigEnabled(true)
            .useMultiprocess(false)
            .forceUserScalableEnabled(true)
            .javaScriptEnabled(true);
    geckoRuntime = GeckoRuntime.create(MainActivity.this,builder.build());
    geckoSession.open(geckoRuntime);
    geckoview.setSession(geckoSession);
    geckoSession.loadUri("Https://duckduckgo.com");

}
}

完整的错误信息:

2020-04-25 01:11:44.403 12465-12465/com.example.browser E/AndroidRuntime:         FATAL EXCEPTION: main
Process: com.example.browser, PID: 12465
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/yaml/snakeyaml/constructor/Constructor;

    at org.mozilla.gecko.util.DebugConfig.fromFile(DebugConfig.java:40)
    at org.mozilla.geckoview.GeckoRuntime.init(GeckoRuntime.java:362)
    at org.mozilla.geckoview.GeckoRuntime.create(GeckoRuntime.java:546)
    at com.example.browser.MainActivity.onCreate(MainActivity.java:32)
    at android.app.Activity.performCreate(Activity.java:7955)
    at android.app.Activity.performCreate(Activity.java:7944)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3423)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3595)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2147)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:237)
    at android.app.ActivityThread.main(ActivityThread.java:7814)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1047)

 Caused by: java.lang.ClassNotFoundException: Didn't find class 
 "org.yaml.snakeyaml.constructor.Constructor" on path: DexPathList[[zip file
"/data/app/com.example.browser-REepjBL5JVM2lFFcNMNHJA==
/base.apk"],nativeLibraryDirectories=[/data/app/com.example.browser-
REepjBL5JVM2lFFcNMNHJA==/lib/arm64, /data/app/com.example.browser-
REepjBL5JVM2lFFcNMNHJA==/base.apk!/lib/arm64-v8a, /system/lib64, /product
/lib64]]

    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:196)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    at org.mozilla.gecko.util.DebugConfig.fromFile(DebugConfig.java:40) 
    at org.mozilla.geckoview.GeckoRuntime.init(GeckoRuntime.java:362) 
    at org.mozilla.geckoview.GeckoRuntime.create(GeckoRuntime.java:546) 
    at com.example.browser.MainActivity.onCreate(MainActivity.java:32) 
    at android.app.Activity.performCreate(Activity.java:7955) 
    at android.app.Activity.performCreate(Activity.java:7944) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3423) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3595) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2147) 
    at android.os.Handler.dispatchMessage(Handler.java:107) 
    at android.os.Looper.loop(Looper.java:237) 
    at android.app.ActivityThread.main(ActivityThread.java:7814) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at     com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1047) 
2020-04-25 01:11:44.420 12465-12465/com.example.browser I/Process: Sending     signal. PID: 12465 SIG: 9

问题已解决。

Bij 将 geckoview pom 文件中提到的依赖项添加到构建 gradle 应用程序的依赖项块中,原来的问题已解决。

sqlite 库加载失败时出现了一个新问题。 显然 android studio 的新版本默认为 extractnativelibs=false。通过将 android:extractNativeLibs="true" 添加到 androidmanifest.xml 问题已解决。