关闭并重启Android Studio, 这种方法是最基本的,但不一定总是有效。
清除Android Studio的缓存。打开命令行,输入以下命令:
$ cd ~/Library/Caches/
$ rm -rf AndroidStudio*
清除Gradle缓存。打开命令行并输入以下命令:
$ rm -rf ~/.gradle/caches/
在项目中关闭即时运行(Instant Run),可通过以下步骤完成:
指定Android Studio使用特定的JDK版本。在项目的build.gradle文件中添加以下代码。请注意,这只是一个示例,您需要根据本地设备上JDK所在的位置来修改路径。
android {
...
...
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
// Change the JDK folder path to the correct location for your machine
// The following path should be modified as per need.
//change /Applications/Android\ Studio.app/Contents/jdk//Contents/Home/ with path of your installed jdk
//on my computer path was /Library/Java/JavaVirtualMachines/jdk1.8.0_251.jdk/Contents/Home/
// windows users should replace jdk with jre
//replace "Program Files" with the folder name where your jdk is installed
//'"C:/Program Files/Java/jdk1.8.0_281"' is the path where wish to set JDK
def jdkPath = "/Library/Java/JavaVirtualMachines/jdk1.8.0_251.jdk/Contents/Home/"
// Use the path which suits you
kotlinOptions {
jvmTarget = "1.8"
javaHome = jdkPath
}
// Add path of JDK to system path
//to add path to path variable on mac run command given below
//export PATH="/Library/Java/JavaVirtualMachines/jdk1.8.0