Issue with log4j version 2.9.0 in Android - java

I am using log4j in one of my Android lib projects. Now I updated the log4j version from 2.3.0 to 2.9.0 and building the lib project is successful and also I was able to successfully add this lib project to my main project as a dependency. But when I run my project, I get the following error.
Error:Error converting bytecode to dex:
Cause: Dex cannot parse version 53 byte code.
This is caused by library dependencies that have been compiled using Java 8 or above.
If you are using the 'java' gradle plugin in a library submodule add
targetCompatibility = '1.7'
sourceCompatibility = '1.7'
to that submodule's build.gradle file.
...while parsing META-INF/versions/9/org/apache/logging/log4j/util/ProcessIdUtil.class
To over come the above error I made the following changes in the gradle.
android {
compileSdkVersion 25
buildToolsVersion '25.0.2'
defaultConfig {
...
jackOptions {
enabled true
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
After the above changes I got the following error while running the project.
Error:Jar transformation: class files coming from an unsupported Java
version Error:Execution failed for task
':MyApp:transformClassesWithPreJackPackagedLibrariesForDebug'.
com.android.build.api.transform.TransformException: com.android.builder.core.JackToolchain$ToolchainException: Jack compilation exception
Now I am not getting a proper solution for this problem. Even I raised this in log4j forum (https://issues.apache.org/jira/browse/LOG4J2-2038). But the provided solution doesn't explain much.
Mean while I am trying to work directly with the log4j android code from here .
This has become a blocker for me. Any help will be appreciated. Thanks.

Related

Cordova Android App Build Issue - Duplicate zip entry

My AngularJS based Cordova project facing various Android build issues recently. It was in gradle version of 2.14.1 with build tools of 2.2.3. However, faced issue as "A problem occurred configuring root project ".
I tried removing android platform and adding again with platform version 6.4.0,
Recent error While perform Cordova build android:
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
FAILURE: Build failed with an exception.
What went wrong:
Execution failed for task ':transformClassesWithMultidexlistForDebug'.
java.io.IOException: Can't write [/Users////platforms/android/build/intermediates/multi-dex/debug/componentClasses.jar] (Can't read [/Users/****/.gradle/caches/transforms-1/files-1.1/support-v4-23.4.0.aar/21482513ed63a92f167953e0b46db339/jars/classes.jar(;;;;;;.class)] (Duplicate zip entry [classes.jar:android/support/v4/util/ArrayMap.class]))
I tried using multiDexEnabled as true in defaultconfig, but error remains same,
Project.properties :
target=android-26
android.library.reference.1=CordovaLib
cordova.gradle.include.1=cordova-android-support-gradle-release/stcmobile-cordova-android-support-gradle-release.gradle
cordova.system.library.1=com.android.support:support-v4:24.1.1+
cordova.system.library.2=com.android.support:support-v4:+
cordova.system.library.4=com.android.support:multidex:1.0.1
Build.gradle :
android {
......
defaultConfig {
multiDexEnabled true
}
compileSdkVersion 26
buildToolsVersion '26.0.2'
.......
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_6
targetCompatibility JavaVersion.VERSION_1_6
}
......
dependencies {
implementation fileTree(dir: 'libs', include: '*.jar')
// SUB-PROJECT DEPENDENCIES START
implementation(project(path: "CordovaLib"))
compile "com.android.support:support-v4:24.1.1+"
compile "com.android.support:support-v4:+"
compile "com.android.support:multidex:1.0.1"
// SUB-PROJECT DEPENDENCIES END
}
}
I'm not in a position to migrate the application to latest Angular and hence this build related issues needs to be fixed.
current Versions details,
Cordova Android : 6.4
Cordova : 7.1
Gradle : 4.1
com.android.tools.build:gradle : 3.0.0
compileSdkVersion 26
buildToolsVersion '26.0.2'
After multiple trial and check with various compile sdk and build tools versions, the errors got eliminated and still running the project.

Java 1.7 compatibility for a library in android studio

I want to use this library in my android studio project but when I add it into project using gradle it show the following error
Error:Error converting bytecode to dex: Cause: Dex cannot parse version 52 byte code. This is caused by library dependencies that have been compiled using Java 8 or above. If you are using the 'java' gradle plugin in a library submodule add targetCompatibility = '1.7' sourceCompatibility = '1.7' to that submodule's build.gradle file.
It seems it is because of java 1.7 compatablility but I have no idea how to fix it. Android studio suggests to add targetCompatibility = '1.7' sourceCompatibility = '1.7' to that submodule's build.gradle but how can I add it into gradles dependencies section?
Version 52 is actually Java 8.
The library was compiled without Java 7 compatibility, so it cannot be used if you are targeting Java 7 or lower.
I see that there's an open issue about it on their GitHub. It might be a problem with this versions only, so you might try using a previous version until they fix the issue.
You should use VERSION_1_8 instead of VERSION_1_7
Install JDK 1.8
You should add this
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
FYI
Before use 1_8 please read Use Java 8 Language
To enable Java 8 language features and Jack for your project, enter the following in your module-level build.gradle file:
android {
...
defaultConfig {
...
jackOptions {
enabled true
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
Clean-Rebuild and RUN

Android: Dex cannot parse version 52 byte code

I just switched to Android Studio 2.1 and this error showed up when trying to compile an app the was previously working:
Error:Error converting bytecode to dex:
Cause: Dex cannot parse version 52 byte code.
This is caused by library dependencies that have been compiled using Java 8 or above.
If you are using the 'java' gradle plugin in a library submodule add
targetCompatibility = '1.7'
sourceCompatibility = '1.7'
to that submodule's build.gradle file.
I had already updated the main project's gradle.build file to force Java 1.7 code generation:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
apply plugin: 'java'
sourceCompatibility = 1.7
targetCompatibility = 1.7
}
}
I had also updated the module gradle.build as follows to set the java version:
android {
compileSdkVersion 19
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.abc.def"
minSdkVersion 19
targetSdkVersion 19
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
The submodule being built with Maven. In the pom.xml file I have also tried to force 1.7 code generation.
I understand that I am using an assembly artifact, which incorporates subordinate modules, but i have not changed any of the subordinate modules and the resulting .jar file for the module ran fine last time I compiled.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId> <!-- maven-compiler-plugin -->
<version>2.6</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
My question:
Is this an Android Studio 2.1 problem? Have others seen it?
Assuming this is my error and since the error message gives no help in finding the bad module, are there any recommendations on finding the V52 code? I can't just omit the libraries without breaking large amount of code. Can one inspect a .jar file to find the code revision?
just use java 1.8 with Android Studio 3.0+ and set following works for me:
it seems need the latest build tools
classpath 'com.android.tools.build:gradle:3.0.0'
and
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
...
//jackOptions { // DEPRECATED
//enabled true
//}
}
dexOptions {
incremental true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
If you have a module with a java library that is not Android-specific, this should work: apply plugin:'java'
Put it at the top of the build.gradle file, then rebuild.
apply plugin: 'java'
apply plugin: 'jacoco'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.11'
sourceCompatibility = 1.7
targetCompatibility = 1.7
}
If you use org.jetbrains:annotation:15 and retrolambda plugin then remove line compile org.jetbrains:annotations:15.0 from your build.gradle and the error will disappear. It works for me.
Possibly, some of your dependencies was compiled with Java 8, not for Android especially. Try to switch that dependencies to older version. I don't exactly know which library you should downgrade, because you haven't attached a list of dependencies of your main module.
For example: I had the same problem. After hours of searching, I've found that library org.codehaus.httpcache4j.uribuilder:2.0.0 requires Java 8, as of github. So, when i've switched to 1.1.0, project has been successfully builded and deployed.
Try to add to main build.gradle in section allprojects
tasks.withType(JavaCompile) {
sourceCompatibility = "1.7"
targetCompatibility = "1.7"
}
or add this in dependencies
sourceCompatibility = 1.7
targetCompatibility = 1.7
in all modules manually
I was able to solve this issue by adding the following lines:
jackOptions {
enabled true
}
to defaultConfig in build.gradle file.
You can follow the guidelines for Java 8 on the link -
https://developer.android.com/guide/platform/j8-jack.html
I had the same problem with the greendao-generator dependency. I mistakenly added that dependency into my build.gradle (compile 'org.greenrobot:greendao-generator:3.1.0') and AndroidStudio showed me the same error message.
Probably it's because that module has been compiled with Java 8.
So I removed that dependency from my build.gradle and all compiled happily :)
I solved this problem as belowed :
apply plugin: 'java'
sourceCompatibility = 1.7
targetCompatibility = 1.7
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
Switching off Instant Run in Android Studio 2.2 with Gradle plugin 2.2.2 fixed it for me. Switching back to an older version of the Gradle plugin (such as 2.2.0) also fixed it, but that's less desirable imho.
This happened to me with Android Studio 2.3.3. The solution I found was to delete the build folder and then Rebuild Project. It was as simple as that.
I also faced same error in Android 2.3.3, after adding few JAR depencies. Problem was due to the depenecy io.netty:netty-all:4.1.16.Final. This 4.1.16 version JAR was compiled with Java 1.8 and all others was generated with Java 1.7.
This get resolved, after including older version of netty(which was generated with Java 1.7) in my build.gradle file.
compile 'io.netty:netty-all:4.1.5.Final'
I have come across this issue when trying to upgrade to auto-value v 1.5 in Android Studio v 2.3.3. Auto-value 1.5 will presumably be compatible with AS 3 (It requires an updated java compiler)
For now auto-value 1.4.1 works.
I face the same issue with Reactive Location APIs Library for Android and RxJava 2.Just update build.gradle to 3.0.1 and reduce the Reactive Location APIs Library for Android and RxJava 2 library version from 1.0.4 to 1.0.3
It works fine in my case.
I have come across this issue when trying to import a jar compiled by jdk 1.8 in Android Studio 3.0. I tried all above solution, but none work. so, I asked the developer of that jar to re-compile it with jdk 1.7, and then it work well, not come across this issue again.
If possible for you:
Upgrade android tools to: classpath 'com.android.tools.build:gradle:3.0.0'
buildToolsVersion "26.0.2"
This should take care of the issue.
Reference: https://developer.android.com/studio/write/java8-support

Error when using a jar in my project

I use Java 1.8 to create my jar.
I can use it in a Java project, but in an Android project, I have the following error:
Error:Error converting bytecode to dex:
Cause: Dex cannot parse version 52 byte code.
This is caused by library dependencies that have been compiled using Java 8 or above.
If you are using the 'java' gradle plugin in a library submodule add
targetCompatibility = '1.7'
sourceCompatibility = '1.7'
to that submodule's build.gradle file.
Error:1 error; aborting
Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: java.lang.RuntimeException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'E:\Android-tool\JAVA\jdk1.8.0_77\bin\java.exe'' finished with non-zero exit value 1
Information:BUILD FAILED
Information:Total time: 6.468 secs
Information:3 errors
Information:0 warnings
Information:See complete output in console
Add these things to the module's build.gradle:
android {
buildToolsVersion "24rc4"
defaultConfig {
jackOptions {
enabled true
}
}
dexOptions {
incremental true
}
compileOptions {
sourceCompatibility = org.gradle.api.JavaVersion.VERSION_1_8
targetCompatibility = org.gradle.api.JavaVersion.VERSION_1_8
}
}
Fell into the same situation. Here's what I did.
1. Download Jdk 1.7 from this link Oracle Java 1.7
Now, after downloading and installing it go to system properties (My computer) and then to advanced system properties.
Go to environment variables and change the path home and java home to your newly installed jdk 1.7.
It should work fine now. If not then follow the below steps:
Switch to Android studio and then right click on your module. Go to module settings/project structure.
now go to modules section and specify the source and target compatibility to 1.7
Works like a charm for me.
Ps. - Sorry for such a late answer
In case of a Java library in a multi-app project, use code mentioned bellow in the library's build.gardle file.
apply plugin: 'java'
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
compileJava.options.bootClasspath = "D:/android/sdk/platforms/android-23/android.jar"
...
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
...
}
You will need to change compileJava.options.bootClasspath with your local path to android.jar file. This works for me in AS 2.2 on Win10.
for more references please visit here
If self explain in the error:
This is caused by library dependencies that have been compiled using
Java 8 or above. If you are using the 'java' gradle plugin in a
library submodule add targetCompatibility = '1.7' sourceCompatibility
= '1.7' to that submodule's build.gradle file.

"Bad class file magic" from setting up Android Studio for NDK

I'm setting up a project to work with Androids NDK. By that I have to use the experimental Gradle plugin. Gradle syncs without errors and the make is without errors.
But when I try to 'run app' Gradle gives this error message:
UNEXPECTED TOP-LEVEL EXCEPTION:
Error:com.android.dx.cf.iface.ParseException: bad class file magic
(cafebabe) or version (0034.0000) at
com.android.dx.cf.direct.DirectClassFile.parse0(DirectClassFile.java:472)
at
com.android.dx.cf.direct.DirectClassFile.parse(DirectClassFile.java:406)
at
com.android.dx.cf.direct.DirectClassFile.parseToInterfacesIfNecessary(DirectClassFile.java:388)
at
com.android.dx.cf.direct.DirectClassFile.getMagic(DirectClassFile.java:251)
at com.android.dx.command.dexer.Main.parseClass(Main.java:764) at
com.android.dx.command.dexer.Main.access$1500(Main.java:85) at
com.android.dx.command.dexer.Main$ClassParserTask.call(Main.java:1684)
at com.android.dx.command.dexer.Main.processClass(Main.java:749) ...
19 more
From previous questions I can see that it might depend on me using Java 8 and Android requiring Java 7. But my other projects compile and run without this issue.
I missed this answer and the code snippet that solved the problem.
model {
android {
compileOptions.with {
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
}
... //rest of gradle code
}
}
Sloppy research from my side.

Categories

Resources