When I target Android API level 21 what I set as sourceCompatibility 11 or 8 and what's the differences in my build gradle I always set it like that
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
but on android studio Electric Eel it says:
Starting with Android Gradle plugin 7.4.0-alpha04, AGP ships wth JVM 11 bytecode.
When you set your compile options to
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
you will be able to compile with Java 11 language features
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.
I have a project that has two source trees (client and main). The client needs to be compiled to Java 1.7 while main contains Java 1.8 constructs. I have the following gradle source set defined:
sourceSets {
main {
java {
srcDir "src/main/java"
srcDir "src/client/java"
}
}
compileMainJava {
sourceCompatibility = "1.8"
targetCompatibility = "1.8"
}
client {
java {
srcDir "src/client/java"
}
}
compileClientJava {
sourceCompatibility = "1.7"
targetCompatibility = "1.7"
}
}
However, the main source set is compiled with 1.7 apparently since I get this
:compileJavawarning: [options] bootstrap class path not set in conjunction with -source 1.7
C:\dev\eclipse-ws\generic\IoT-Sound\src\main\java\com\ibm\watson\iot\sound\feature\mfcc\MFCCFeatureExtractor.java:129: error: method references are not supported in -source 1.7
double[] x = Stream.of(xDataBoxed).mapToDouble(Double::doubleValue).toArray();
(use -source 8 or higher to enable method references)
If I comment out the compileMainJava element, then it builds both successfully and seems to compile the client to 1.7 since i get the same warning for compileClientJava (without errors). I guess this is because I'm using 1.8 jdk, but would like to add these attributes anyway.
So, how do i get main compatibility set to 1.8 and client to 1.7? Thanks.
You won't be able to have two language versions in the same project. That said you could have other modules with different Java versions.
In this case you probably want 3 modules
:client
:main
:common
Now if you want the project(':client') to be Java 7 you can set it in client/build.gradle
compileJava {
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
}
Then define your project(':main') to be Java 8 in the main/build.gradle
compileJava {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
Just be sure you use Java 7 in the project(':common') or the client won't know how to read the java 8 bytecode generated by common.
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
I would like to set the required Java version (e.g. 7 or 8) in my Gradle build file without having to specify the actual path to a local JDK installation.
Is this possible?
This feature was just added to Gradle 6.7 as Java toolchains:
// build.gradle.kts
plugins {
id("java-library") // or id("application")
}
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(11))
}
}
With this in place Gradle will automatically download & use the appropriate JDK (using AdoptOpenJDK by default) for the specified Java version.
TLDR;
Thanks #franklin-yu "targetCompatibility = '1.7' -> your user can compile with 8 and run with 7."
See Gradle, "sourceCompatibility" vs "targetCompatibility"?
targetCompatibility = '1.7' does the trick for e.g. Java 7
Use sourceCompatibility = '1.7' for the language level
You can try this:
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
tasks.withType<JavaCompile> {
options.compilerArgs.addAll(arrayOf("--release", "8"))
}
This will also give JDK compliance to you. You can also see the following related issues:
Gradle: [Java 9] Add convenience method for -release compiler argument
Eclipse Plug-ins for Gradle: JDK API compatibility should match the sourceCompatibility option.
In the build.gradle file, add the following two lines:
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
The targetCompatibility defines the generated JVM bytecode version (this is the version that users of your application need). The sourceCompatibility defines which source code constructs are allowed (e.g. you need Java 1.8 or higher to use lambda expressions in source code).
Source
Based on the answer of CletusW. In windows 10, it seems that the new installed Java will not be chosen automatically, so I check it manually.
apply plugin: 'java'
java.toolchain.languageVersion = JavaLanguageVersion.of(15) // auto install
// check JDK version
if (!System.getProperty("java.home").contains(java.toolchain.languageVersion.get().toString())) {
def msg = ('JDK in this project: ' + System.getProperty('java.home') + '\n' +
'In this project, you should use JDK-' + java.toolchain.languageVersion.get())
throw new GradleException(msg)
}