How to add Java 9 to Android Studio?
Asked Answered
C

6

16

I am using Robolectric library and the latest version of it v4.3.1 requires Java 9 to run. I am trying to point JRE on edit configurations but I am not finding Java 9 in the drop-down, even though I have already installed it.

enter image description here

Please check Java 9 installed.

enter image description here

Cicelycicenia answered 23/10, 2019 at 6:27 Comment(3)
android doesn't support android 9 yet you can use older version of RobolectricKenning
Take a look at this https://mcmap.net/q/402135/-does-android-studio-3-support-java-9-for-android-development-if-so-what-features-of-java-9-are-supportedRaveaux
yes android itself doesn't support JDK9 yet but studio can as it is built over IntelliJ IDEKenning
D
7

The only workaround to run Robolectric test in the Android Studio is to change JRE for the test task.

Select IDE menu Run -> Edit Configuration and then change the option from the picture to the location of JDK9:

screenshot

Decuple answered 24/10, 2019 at 5:15 Comment(0)
C
5

Tests can run on prior versions as a temporary workaround

You will not be able to run Robolectric against SDK 29 for now (27 April 2020), but this will likely be updated in the future. Currently, as all of the answers before me pointed out, there is no support of Java 9 or later in Android Studio.

But if you are here to make your Robolectic tests run you could simply avoid SDK 29 by setting the Robolectric @Config annotation for your class. Robolectric will simply run your tests against all of the supported SDK versions of your app except 29.

Implement

Set the maxSdk and minSdk versions to run on. This can be based on the app's min and max SDK versions, or set to one version like below in order to make tests faster.

import androidx.test.ext.junit.runners.AndroidJUnit4
import org.robolectric.annotation.Config
import org.junit.runner.RunWith
import android.os.Build

@RunWith(AndroidJUnit4::class)
@Config(maxSdk = Build.VERSION_CODES.P, minSdk = Build.VERSION_CODES.P) // Value of Build.VERSION_CODES.P is 28
class TestClass {
...
}

Of course it is just another "ugly" patch, but it will get you going until you'll be able to run tests against all of the desired SDKs, or at least SDK 29 and all below.

Chummy answered 27/4, 2020 at 19:46 Comment(0)
C
3

So far Android doesn't support Java 9. As per documentation, Android supports all Java 7 features and a part of Java 8 features.

When developing apps for Android, using Java 8 language features is optional. You can keep your project's source and target compatibility values set to Java 7, but you still need to compile using JDK 8.

Claresta answered 23/10, 2019 at 6:47 Comment(0)
K
2

You can't use Java 9, Android only supports till Java 8.

You should use the JDK version that comes with Android Studio, no need for side alone JDK. The current JDK version is based on OpenJDK 8.

Kibe answered 23/10, 2019 at 6:32 Comment(2)
Won't I be able to get microsecond precision in timestamp using java 9(Instant.now()) in android? Please check this thread - https://mcmap.net/q/409859/-android-not-able-to-get-microseconds-using-java-time-instant/1584121Tambourine
This answer is incorrect. You might not be able to use Java 9 code in an Android app without desugaring, but Robolectric would not be requesting Java 9 unless it was possible to use a Java 9 SDK for compilation. Eugen's solution is correct. As of the latest Robolectric 4.3.1, you may still encounter errors because of this issue: github.com/robolectric/robolectric/issues/5303Listless
K
2

I don't guarantee that it will work but you can set JDK to any version from the project Structure Option File>Project Structure>SDK Location>'change JDK location here'

enter image description here

sorry but it's not working either i have tried to select JDK9 but i got a prompt like thisenter image description here

Kenning answered 23/10, 2019 at 6:41 Comment(0)
T
0

In Android Studio, under "Build, Execution, Deployment => Gradle => Gradle JDK", you can choose the JDK version. In newer versions of Android Studio like Artic Fox, it comes with JDK11.

enter image description here

Turaco answered 3/12, 2022 at 20:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.