Android应用程序抛出Guice异常

时间:2016-11-05 18:46:43

标签: java android gradle guice

我是Android applcation的新手,可能做错了什么。 我尝试使用javampd modue编译应用程序,但它抛出异常:“com.google.inject.internal.util。$ ComputationException:com.google.inject.internal.util。$ ComputationException:java.lang.ExceptionInInitializerError”at at biulder创造:

try {
    MPD.Builder mpdBuilder = new MPD.Builder();
} catch (Exception error) {
    String text = error.getMessage();
    Toast.makeText(this, "Exception: " + text, Toast.LENGTH_LONG).show();
}

我已将该异常追溯到Guice,并发现该异常正在javampd模块中抛出

injector = Guice.createInjector(new MPDModule());

这是完整的堆栈跟踪:

W/nalizableReferenceQueue: Could not load Finalizer in its own class loader. Loading Finalizer in the current class loader instead. As a result, you will not be able to garbage collect this class loader. To support reclaiming this class loader, either resolve the underlying issue, or move Google Collections to your system class path.
                           java.io.FileNotFoundException: com/google/inject/internal/util/$Finalizer.class
                               at com.google.inject.internal.util.$FinalizableReferenceQueue$DecoupledLoader.getBaseUrl(FinalizableReferenceQueue.java:269)
                               at com.google.inject.internal.util.$FinalizableReferenceQueue$DecoupledLoader.loadFinalizer(FinalizableReferenceQueue.java:253)
                               at com.google.inject.internal.util.$FinalizableReferenceQueue.loadFinalizer(FinalizableReferenceQueue.java:175)
                               at com.google.inject.internal.util.$FinalizableReferenceQueue.<clinit>(FinalizableReferenceQueue.java:100)
                               at com.google.inject.internal.util.$MapMaker$QueueHolder.<clinit>(MapMaker.java:787)
                               at com.google.inject.internal.util.$MapMaker$WeakEntry.<init>(MapMaker.java:946)
                               at com.google.inject.internal.util.$MapMaker$Strength$1.newEntry(MapMaker.java:312)
                               at com.google.inject.internal.util.$MapMaker$StrategyImpl.newEntry(MapMaker.java:498)
                               at com.google.inject.internal.util.$MapMaker$StrategyImpl.newEntry(MapMaker.java:419)
                               at com.google.inject.internal.util.$CustomConcurrentHashMap$ComputingImpl.get(CustomConcurrentHashMap.java:2029)
                               at com.google.inject.internal.Annotations$AnnotationChecker.hasAnnotations(Annotations.java:116)
                               at com.google.inject.internal.Annotations.isScopeAnnotation(Annotations.java:124)
                               at com.google.inject.internal.ScopeBindingProcessor.visit(ScopeBindingProcessor.java:40)
                               at com.google.inject.internal.ScopeBindingProcessor.visit(ScopeBindingProcessor.java:30)
                               at com.google.inject.spi.ScopeBinding.acceptVisitor(ScopeBinding.java:59)
                               at com.google.inject.internal.AbstractProcessor.process(AbstractProcessor.java:55)
                               at com.google.inject.internal.InjectorShell$Builder.build(InjectorShell.java:165)
                               at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:103)
                               at com.google.inject.Guice.createInjector(Guice.java:95)
                               at com.google.inject.Guice.createInjector(Guice.java:72)
                               at com.google.inject.Guice.createInjector(Guice.java:62)
                               at org.bff.javampd.MPD$Builder.<init>(MPD.java:186)
                               at com.yarygin.alex.mpd_test.MainActivity.onCreate(MainActivity.java:16)
                               at android.app.Activity.performCreate(Activity.java:5971)
                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2522)
                               at android.app.ActivityThread.access$800(ActivityThread.java:167)
                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1417)
                               at android.os.Handler.dispatchMessage(Handler.java:111)
                               at android.os.Looper.loop(Looper.java:194)
                               at android.app.ActivityThread.main(ActivityThread.java:5534)
                               at java.lang.reflect.Method.invoke(Native Method)
                               at java.lang.reflect.Method.invoke(Method.java:372)
                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955)
                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)

我的gradle文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"
    defaultConfig {
        applicationId "com.yarygin.alex.mpd_test"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:22.2.1'
    testCompile 'junit:junit:4.12'
    compile 'net.thejavashop:javampd:5.0.3'
}

我的活动档案:

package com.yarygin.alex.mpd_test;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import org.bff.javampd.MPD;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try {
            MPD.Builder mpdBuilder = new MPD.Builder();
        } catch (Exception error) {
            String text = error.getMessage();
            Toast.makeText(this, "Exception: " + text, Toast.LENGTH_LONG).show();
        }
    }
}

我在两周内找不到原因。请帮忙!

1 个答案:

答案 0 :(得分:1)

尽管Android应用程序是用Java编写的,但您通常不能简单地使用Java库并在没有配置问题的Android应用程序中使用它们。

如果该库Java MPD内部依赖于Guice,那么在Android项目中使用它将很困难。这是因为Guice在使用Android时遇到了许多配置问题。此外,还有一些其他问题需要考虑。为了演示,我引用了Android的其他DI框架的FAQ

  
      
  1. Guice很慢,至少它不再符合预期   Android应用程序的性能。
  2.   
  3. Guice 4使用包含数十的Guava   成千上万的方法导致多索引问题和沉重的问题   apks
  4.   
  5. Guice使用反射和反射特别慢   Android系统。主要是因为没有反射代码的JIT   反对PC JVM。
  6.   

您可以尝试使用Roboguice,它使用Guice的分支和注释处理器来解决Guice对Android的反射和其他集成问题的使用。如果这提供了足够的Guice类来使您的MDP库工作,我会感到惊讶。否则,您将不得不找到另一个要使用的库。