You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@logging.apache.org by "Mark Hoffner (JIRA)" <ji...@apache.org> on 2018/01/25 15:51:00 UTC

[jira] [Created] (LOG4J2-2220) Log4j2 Causing Fatal Exception in Android Project

Mark Hoffner created LOG4J2-2220:
------------------------------------

             Summary: Log4j2 Causing Fatal Exception in Android Project
                 Key: LOG4J2-2220
                 URL: https://issues.apache.org/jira/browse/LOG4J2-2220
             Project: Log4j 2
          Issue Type: Bug
    Affects Versions: 2.8, 2.7, 2.6, 2.5, 2.4, 2.3
         Environment: OS X El Capitan

IntelliJ IDEA 2017.3.3

Android SDK 26

JDK 8

Gradle 2.3
            Reporter: Mark Hoffner


Moving a Java project with Maven over to an Android project with Gradle in IntelliJ and everything made the transition smoothly except for Log4j2.

This error message fires whenever the program tries to create a Logger:

{{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) }}

Here's the build.gradle:

{{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' }}}

I was initially using SLF4J with Log4j2, but refactored to straight Log4j2 while troubleshooting. When I use newer versions of Log4j2 I start getting:

{{Execution failed for task ':app:transformClassesWithDexForDebug'.}}

I updated to a newer version of Gradle and the Gradle plugin, but that gave similar task fails but for the 'Desugar' process.

Totally removing any reference to Log4j2 and using no logging or logging with slf4j-simple the program runs fine.

I don't know if this is related, but it seems like the program is not finding the log4j2.xml file. It was initially in the '/src/main/resources/' folder, but I've renamed it and placed it in different folders along the class path and the errors are always the same. I've also used "-Dlog4j.configurationFile=file:/mypath/.../log4j2.xml" on the Gradle VM Options to try to force the program to read the configuration with the same results.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)