解决 Android N requires the IDE to be running with Java 1.8 or later

最近升级了一下IDE,升级到了AS 2.1.1,打开Layout Editor的时候遇到这个问题:

Android N requires the IDE to be running with Java 1.8 or later

AS里面我们需要两个JDK:Project JDK & IDE JDK

  • 前者用于编译Java代码
  • 后者用于启动运行AS自己

Project JDK

  • 可以通过file-> other settings-> default project structure 里面查看修改

  • 如果项目 compileSdkVersion >= 21, 应该设置为Java7 or 8

  • 这个配置也可以在 local.properties 里面设置

解决 Android N requires the IDE to be running with Java 1.8 or later_第1张图片

IDE JDK

  • MAC OSX 里面运行IDE的JDK一般系统会帮你自动选择,而且这是基于IDE里的Info.plist文件里面的一条实体信息,Android Studio.app/Contents/Info.plist。

  • 这个问题的主要原因就是因为IDE JDK太低了,但是请注意请不要修改info.plist来选择一个不同的jdk版本。因为这不仅仅会破坏AS的签名,还会影响以后的补丁更新
    参考 form [1]: Please note: Do not edit Info.plist to pick a different version. That will break not only the application signature, but also future patch updates to your installation.

  • 正确的方式应该是给IDE设置 $STUDIO_JDK 环境变量:

$ export STUDIO_JDK=/Library/Java/JavaVirtualMachines/jdk1.8.0_92.jdk
$ open /Applications/Android\ Studio.app

export STUDIO_JDK=/Library/Java/JavaVirtualMachines/jdk1.8.0_92,这个命令也可以编辑到 .bash_profile 里面,以后就只要用Terminal 执行第二个命令就可以了

  • Java 6 是AS 默认的运行使用JDK版本,但这是为什么呢?原因是因为Java6有一个完整的亚像素LCD反锯齿功能,但是Java7/8没有 – 简单的说,就是使用Java6,代码文字反锯齿显示效果好
    Java 8:
    解决 Android N requires the IDE to be running with Java 1.8 or later_第2张图片
    Java 6:
    解决 Android N requires the IDE to be running with Java 1.8 or later_第3张图片

以上可以解决这个问题,但是解决了这个问题之后又出现了一个新的渲染问题:Exception raised during rendering: com/android/util/PropertiesMap
Detail:

java.lang.NoClassDefFoundError: com/android/util/PropertiesMap
    at com.android.layoutlib.bridge.android.BridgeContext.createStyleBasedTypedArray(BridgeContext.java:940)
    at com.android.layoutlib.bridge.android.BridgeContext.obtainStyledAttributes(BridgeContext.java:638)
    at android.content.res.Resources_Theme_Delegate.obtainStyledAttributes(Resources_Theme_Delegate.java:71)
    at android.content.res.Resources$Theme.obtainStyledAttributes(Resources.java:1436)
    at android.widget.TextView.(TextView.java:761)
    at android.widget.TextView.(TextView.java:704)
    at android.widget.TextView.(TextView.java:700)
    at com.android.layoutlib.bridge.MockView.(MockView.java:50)
    at com.android.layoutlib.bridge.MockView.(MockView.java:45)
    at com.android.layoutlib.bridge.MockView.(MockView.java:41)
    at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:163)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:858)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:70)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:834)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
    at com.android.layoutlib.bridge.bars.CustomBar.(CustomBar.java:95)
    at com.android.layoutlib.bridge.bars.StatusBar.(StatusBar.java:67)
    at com.android.layoutlib.bridge.impl.Layout.createStatusBar(Layout.java:222)
    at com.android.layoutlib.bridge.impl.Layout.(Layout.java:144)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:297)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:429)
    at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:350)
    at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:520)
    at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:508)
    at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:967)
    at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:508)
    at com.android.tools.idea.rendering.RenderTask.access$600(RenderTask.java:75)
    at com.android.tools.idea.rendering.RenderTask$3.call(RenderTask.java:620)
    at com.android.tools.idea.rendering.RenderTask$3.call(RenderTask.java:617)
    at com.android.tools.idea.rendering.RenderService.runRenderAction(RenderService.java:371)
    at com.android.tools.idea.rendering.RenderTask.render(RenderTask.java:617)
    at com.android.tools.idea.rendering.RenderTask.render(RenderTask.java:639)
    at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.doRender(AndroidLayoutPreviewToolWindowManager.java:654)
    at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.access$1700(AndroidLayoutPreviewToolWindowManager.java:82)
    at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$7$1.run(AndroidLayoutPreviewToolWindowManager.java:596)
    at com.intellij.openapi.progress.impl.CoreProgressManager$2.run(CoreProgressManager.java:142)
    at com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.java:446)
    at com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:392)
    at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:54)
    at com.intellij.openapi.progress.impl.CoreProgressManager.runProcess(CoreProgressManager.java:127)
    at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$7.run(AndroidLayoutPreviewToolWindowManager.java:591)
    at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:337)
    at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:327)
    at com.intellij.util.ui.update.MergingUpdateQueue$3.run(MergingUpdateQueue.java:271)
    at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:286)
    at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:244)
    at com.intellij.util.ui.update.MergingUpdateQueue.run(MergingUpdateQueue.java:234)
    at com.intellij.util.concurrency.QueueProcessor.runSafely(QueueProcessor.java:238)
    at com.intellij.util.Alarm$Request$1.run(Alarm.java:352)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

这个问题现在暂时还没有解决,网上找到的方法都是说将要渲染的Preview版本调到Api23:Android API 23,N Preview 以下。

PS:这个方法甚至可以解决Android N requires the IDE to be running with Java 1.8 or later

但是肯定不是一个正确或完美的解决方案,只是逃避性的解决方案。现在我已经在StackOverflow上面再次提问了这个问题:
http://stackoverflow.com/questions/37598554/exception-raised-during-rendering-com-android-util-propertiesmap

相关参考:
[1] http://tools.android.com/tech-docs/configuration/osx-jdk

你可能感兴趣的:(Android)