Gradle using 2 different JDK - java

I would like use some features from Java 9 and 10 in my PC java application which has common code with android app.
When I use jdk 10 or jdk 9 android application is building, but it doesn't launch(it is throwing a lots of errors). When I use jdk 8 for whole project everything is working correctly without any error. When I manually build project using 2 different jdk everything is working fine.
I tried set targetCompability and sourceCompability for android application for JavaVersion.Version_1_7 but it doesn't help. I tried use different jdks for java 9 and java 10 but it doesn't help with this problem.
I would like build android application and common component with jdk8 and other components with jdk10. Is it possible to force gradle to use different jdk for specific project without using external tools like bash?
My project structure looks like:
build.gradle
common-component(jdk8)/build.gradle
PC(jdk 10)/build.gradle
device-Android(jdk 8)/build.gradle

I found workaround. It works fine with gradle 4.7(Java 10 support was added in this release). This hack/workaround requires to launch project using lower JDK like Oracle JDK 8 or OpenJDK. We can build some components using higher version of JDK, but we can't build JDK project with JDK10 and then use it with JDK8 if we specify targetcompablility higher than 1.8. It will work only for java project and probably it won't work for android plugin and other JVM languages.
Part of build.gradle for PC application :
project (':pc-client') {
dependencies {
compile project(':net-default')
testcompile JUNIT
}
compileJava {
options.fork = true
options.forkOptions.javaHome = file('/usr/lib/jvm/java-10-oracle')
targetCompatibility = 1.10
sourceCompatibility = 1.10
}
compileTestJava {
options.fork = true
options.forkOptions.javaHome = file('/usr/lib/jvm/java-10-oracle')
targetCompatibility = 1.10
sourceCompatibility = 1.10
}
}

Related

What bytecode will Kotlin compiller produce if jvmToolchain is set to 11 and jvmTarget to 1.8?

I have a Kotlin project that I want to compile to Java 1.8 bytecode so that it could also be used on Android as well. I use the com.vanniktech.maven.publish Gradle plugin to publish the library to Maven Central. This plugin works only when I set jvmToolchain to 11. However I can still set jvmTarget to 1.8. This way, my configuration would look like this:
compileKotlin {
kotlinOptions.jvmTarget = '1.8'
}
kotlin {
jvmToolchain(11)
}
This works, but I am not sure what bytecode it would produce. It also gives compilation warning asking me to set both values to the same version.
Question: What version of bytecode will Gradle project produce if jvmToolchain is set to 11 and jvmTarget to 1.8?
Alliteratively, how can I make it produce Java 1.8 bytecode in a different way? (If I am doing something wrong).

how to fix Android Annotations 4.6.0 compile with android Studio 3.5 and 3.5.0 gradle

I updated Android studio to 3.5 but there is a problem when I use android annotations
Gradle may disable incremental compilation as the following annotation processors are not incremental: jetified-androidannotations-4.6.0.jar (org.androidannotations:androidannotations:4.6.0).
Consider setting the experimental feature flag android.enableSeparateAnnotationProcessing=true in the gradle.properties file to run annotation processing in a separate task and make compilation incremental
I've put android.enableSeparateAnnotationProcessing=true in the gradle.properties file
but it's say that
INFO: The option setting 'android.enableSeparateAnnotationProcessing=true' is experimental and unsupported.
The current default is 'false'.
Could not find the AndroidManifest.xml file
I had almost the same problem and couldn't build my app after updating android studio and gradle to 3.5 but according to this answer I added this to my defaultConfig{} in app gradle and problem solved!
javaCompileOptions {
annotationProcessorOptions {
arguments = [
"androidManifestFile": "$projectDir/src/main/AndroidManifest.xml".toString()
]
}
}
There is 2 way of fixing this issue
Method One - Use Snapshot version
Current Annotation lib version is 4.6, to fix these error temporary you can use a Snapshot version
add the following URL in Project-level Gradle
buildscript {
repositories {
maven { url = 'https://oss.sonatype.org/content/repositories/snapshots' }
}
Method Two - Use javaCompileOptions
Add following code in gradle DSL
defaultConfig {
javaCompileOptions {
annotationProcessorOptions {
arguments = ["resourcePackageName": android.defaultConfig.applicationId]
arguments = ["androidManifestFile": "$projectDir/src/main/AndroidManifest.xml".toString()]
}
}
}
Hope it will work :)
AndroidAnnotations does not support Gradle incremental annotation processing, yet. You can still build your app, it only means that incremental compilation time is not as fast as it could be. There is an issue for tracking the implementation of incremental processing.
I suggest not to set android.enableSeparateAnnotationProcessing flag, as it is experimental, can cause other issues, and will slow down your clean builds.

FXGL: NoClassDefFoundError when calling FXGL.play("drop.wav")

I am trying to follow the second tutorial https://github.com/AlmasB/FXGL/wiki/Adding-Images-and-Sounds-%28FXGL-11%29 and it appears that i get an error when i run the application. I use gradle run or run it in eclipse, without the sound everything works fine.
My project structure looks like this:
I use openJDK 11.0.3 and linux mint 19.1 64-bit.
It is just basically the same program as in the tutorial, i get the following exception:
Message: javafx/scene/media/AudioClip Type: NoClassDefFoundError
Method: DesktopAudioService.loadAudioImpl() Line:
DesktopAudioService.kt:28
My build.gradle is pretty straight forward i guess; the gradle init and the dependencies:
plugins {
id 'application'
id 'java-library'
id 'org.openjfx.javafxplugin' version '0.0.7'
}
repositories {
mavenCentral()
jcenter()
}
dependencies {
api 'org.apache.commons:commons-math3:3.6.1'
implementation 'com.google.guava:guava:27.0.1-jre'
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
compile 'com.github.almasb:fxgl:11.1-beta'
}
javafx {
version = "12"
modules = [ 'javafx.controls' ]
}
mainClassName = 'game.idea.BasicGameApp'
I expect the sound to be played when clicking 'f' on my keyboard without crashing the program. I also hope for some background explanation what causes the error.
You use the JavaFX Gradle plugin and set the modules in your build like this:
javafx {
version = "12.0.1"
modules = [ 'javafx.controls' ]
}
This means that the plugin will add to your project the javafx.base, javafx.graphics and javafx.controls modules, with the version and correct classifier based on your platform.
If you check your external libraries, you won't find any other JavaFX module implementation, but you might find the "empty" modules that FXGL is using:
So Base, Graphics and Controls use the Mac classifier (in my case), and the version I've set (12.0.1), while the other modules (FXML, Media and Swing) are empty modules added from FXGL (see for instance the Media dependency).
When you run your project, the Media classes are not there, so when you try to play the sound you get the reported exception:
Fatal exception occurred: java.lang.NoClassDefFoundError : javafx/scene/media/AudioClip
E: com.almasb.fxgl.audio.impl.DesktopAudioService.loadAudioImpl(DesktopAudioService.kt:28)
E: com.almasb.fxgl.audio.impl.DefaultAudioService.loadAudio(DefaultAudioService.kt:29)
E: com.almasb.fxgl.app.AssetLoader.loadSound(AssetLoader.kt:247)
E: com.almasb.fxgl.dsl.FXGL$Companion.play(FXGL.kt:228)
E: com.almasb.fxgl.dsl.FXGL.play(FXGL.kt)
E: game.idea.BasicGameApp$5.onActionBegin(BasicGameApp.java:61)
The solution is quite easy: just add the missing modules to your build:
javafx {
version = "12.0.1"
modules = [ 'javafx.controls', 'javafx.fxml', 'javafx.swing', 'javafx.media' ]
}
Finally, as an aside, you can use 'com.github.almasb:fxgl:11.3'.
If you are using OpenJDK , then JavaFx might not be available. This will be the reason for the exception. Change to Oracle JDK if you are using windows. On linux, there are other ways with OpenJDK itself.
Refer to this post JavaFX and OpenJDK for some details on how-to.

How do I know what to set sourceCompatibility vs targetCompatibility to?

according to MoPub Docs I should add this to my gradle file, but my question is what do I pick for sourceCompatibility and target compatibility, it seems like target Compatibility has to be set to 1_8 or else it will not build. But what is the difference if I leave sourceCompatibility to 1_7 vs 1_8 is there a way to know which one my project uses, sourceCompatibility builds the app fine with either one set I would just like to know what is the difference between the 2 and if I actually do need to set it to 1 of them instead of the other also I've read that gradle uses the java version which is on our machine and mine is 1.8.0 so why do I have to explicitly declare VERSION_1_8 ?
on java docs it states "Generates class files that target a specified release of the virtual machine. Class files will run on the specified target and on later releases, but not on earlier releases of the JVM. Valid targets are 1.1, 1.2, 1.3, 1.4, 1.5 (also 5), 1.6 (also 6), 1.7 (also 7), and 1.8 (also 8)." but java 1.7&1.8 not available on android 4.1 and it still runs on android 4.1 when I set java version to 1.7 or 1.8 how is this possible ?
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_8
} }

Pure Java 8 Library module in Android Studio/Gradle Project

I have an Android Studio project with a pure Java 8 library module. Here is the complete gradle.build file for that library:
apply plugin: 'java-library'
dependencies {
testImplementation 'junit:junit:4.12'
}
sourceCompatibility = "1.8"
targetCompatibility = "1.8"
The code is tested and works fine.
The problem I am having is that I have built the project, and then tried to use the built .jar file in another Android project that doesn't uses Java 7 (not 8). Obviously, the code does not work. However, I am surprised that there are no compile-time errors indicating that the library is not compatible. Instead, I only first notice the incompatibility when a runtime function is called.
Am I doing something wrong in how I am compiling the Java module? Or is it expected behavior that you will only get runtime errors that the library is not compatible? I expected compile-time errors.
Also, please tell me how to compile properly so that I can get compile-time errors in Android Studio indicating that a library was compiled with a newer Java version.
For completeness, I am getting the following runtime error when I use my Java 8 library module
java.lang.NoClassDefFoundError: Failed resolution of: Ljava/util/Base64;
java.util.Base64 added in API level 26 (https://developer.android.com/reference/java/util/Base64.html).
You must use android.util.Base64 (https://developer.android.com/reference/android/util/Base64.)
For universal library I use Google Guava.
<dependency>
<artifactId>guava</artifactId>
<groupId>com.google.guava</groupId>
<type>jar</type>
<version>14.0.1</version>
</dependency>

Categories

Resources