How to add Java 9 to Android Studio?

六眼飞鱼酱① 提交于 2020-05-14 17:51:11

问题


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!

Please check Java 9 installed.


回答1:


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.




回答2:


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:




回答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.




回答4:


Note: you will not be able to run Robolectric against SDK 29 for now (27 April 2020), but you can still run your tests as it was before. It will change in future for sure.

Hello there!

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.

The solutions is quite simple:

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) // 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.




回答5:


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'

sorry but it's not working either i have tried to select JDK9 but i got a prompt like this



来源:https://stackoverflow.com/questions/58516687/how-to-add-java-9-to-android-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!