Log4j2在Android项目中导致致命异常

时间:2018-01-24 23:28:07

标签: android gradle log4j2

将带有Maven的Java项目移植到IntelliJ中使用Gradle的Android项目,除Log4j2外,一切都顺利过渡。

只要程序尝试创建Logger,就会触发此错误消息:

self.queuedValues = ko.observableArray([]);
self.generate = function() {
    window.open("{{ url_for('genreport') }}/" + qvm.letter() + '/' + checkedValue);
  }

这是build.gradle:

java.lang.ExceptionInInitializerError
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2538)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.NullPointerException: Attempt to get length of null array
at org.apache.logging.log4j.util.ReflectionUtil$PrivateSecurityManager.getCallerClass(ReflectionUtil.java:301)
at org.apache.logging.log4j.util.ReflectionUtil.getCallerClass(ReflectionUtil.java:214)
at org.apache.logging.log4j.util.ReflectionUtil.getCallerClass(ReflectionUtil.java:193)
at org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:72)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:195)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:41)
at org.apache.logging.log4j.LogManager.getContext(LogManager.java:160)
at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:492)
at com.company.sample.MainActivity.<clinit>(MainActivity.java:18)
at java.lang.Class.newInstance(Native Method) 
at android.app.Instrumentation.newActivity(Instrumentation.java:1078) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2538) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6077) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 

我最初使用的是带有Log4j2的SLF4J,但在进行故障排除时重构为直接Log4j2。当我使用较新版本的Log4j2时,我开始得到:

apply plugin: 'com.android.application'

sourceCompatibility = 1.8
targetCompatibility = 1.8

android {
    compileSdkVersion 26
    buildToolsVersion "27.0.3"
    defaultConfig {
        applicationId "com.company.sample"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    packagingOptions {
        pickFirst 'META-INF/DEPENDENCIES'
        pickFirst 'META-INF/LICENSE'
        pickFirst 'META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat'
    }
    dexOptions {
        javaMaxHeapSize "4g"
    }
}

repositories {
    flatDir {
        dirs 'libs'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    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:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'

    compile group: 'org.apache.logging.log4j', name: 'log4j-api', version:'2.3'
    compile group: 'org.apache.logging.log4j', name: 'log4j-core', version:'2.3'

    compile 'com.android.support:multidex:1.0.1'

    compile group: 'org.testng', name: 'testng', version: '6.13'
    compile group: 'org.mockito', name: 'mockito-core', version:'2.11.0'
    testCompile 'junit:junit:4.12'
}

我更新了更新版本的Gradle和Gradle插件,但是这给了类似的任务失败,但是对于'Desugar'进程。

完全删除对Log4j2的任何引用,并且不使用slf4j-simple进行日志记录或记录,程序运行正常。

我不知道这是否相关,但似乎程序没有找到log4j2.xml文件。它最初位于'/ src / main / resources /'文件夹中,但我已将其重命名并将其放在类路径的不同文件夹中,并且错误始终相同。我还在Gradle VM Options上使用了“-Dlog4j.configurationFile = file:/ mypath /.../ log4j2.xml”来尝试强制程序读取具有相同结果的配置。

感谢您提供的任何帮助。

0 个答案:

没有答案