Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Java 9 to Android Studio?

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. It would be really helpful if somebody can please explain!

enter image description here

Please check Java 9 installed.

enter image description here

like image 660
Varun A M Avatar asked Oct 23 '19 06:10

Varun A M


People also ask

How do I install Java JDK 9?

In a browser, go to the Java SE Development Kit 9 Downloads page and click Accept License Agreement. Under the Download menu, click the Download link that corresponds to the .exe for your version of Windows. Download fhe file jdk-9.

What version of Java is needed for Android Studio?

A copy of the latest OpenJDK comes bundled with Android Studio 2.2 and higher, and this is the JDK version we recommend you use for your Android projects.

Do I have to download JDK for Android Studio?

You must install Oracle JDK before installing Android Studio, so please don't start this step until you have completed Step 1 above. Download Android Studio from the following address: Android Studio Download Page.

Can I use Java 9 in Android Studio?

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. Show activity on this post.

How to create a Java class in Android Studio?

In the 'Android' view, which shows all your project files, navigate to the directory you wish to create the class 3. Right click on the directory name, and select New -> Java class

How do I add an embedded JDK to Android Studio?

1 Open your project in Android Studio and select File > Project Structure in the menu bar. 2 In the SDK Location page and under JDK location, check the Use embedded JDK checkbox. 3 Click OK.

How do I create an app in Android Studio?

In the Welcome to Android Studio dialog, click Start a new Android Studio project. Select Basic Activity (not the default). Click Next. Give your application a name such as My First App. Make sure the Language is set to Java. Leave the defaults for the other fields. Click Finish. After these steps, Android Studio:


Video Answer


3 Answers

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

like image 69
Eugen Martynov Avatar answered Oct 21 '22 14:10

Eugen Martynov


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.

like image 44
Jenea Vranceanu Avatar answered Oct 21 '22 14:10

Jenea Vranceanu


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.

like image 3
Zain Avatar answered Oct 21 '22 16:10

Zain