How to specify the JDK version in Android Studio? - java

Android Studio is giving me a Gradle build error that looks like this:
Error:(3, 22) compileSdkVersion android-22 requires compiling with JDK 7
Now it gives me these clickable prompts:
Download JDK 7
Select a JDK from the File System
Open build.gradle File
And I have already downloaded and installed JDK 7. The problem is when I go to select it in the "File System" i can only find a directory named 1.6.0 JDK. Furthermore, the installation of JDK 7 skipped the bullet point where I would've selected the install directory, so I'm really not sure where it is. My java control panel says I have "Java 7 Update 79" so I'm pretty sure I'm close, I just need to tell android studio where it is. I also ran the java -version command in the terminal and it says my version is "1.7.0_79".
Any help would be appreciated!

You can use cmd + ; for Mac or Ctrl + Alt + Shift + S for Windows/Linux to pull up the Project Structure dialog. In there, you can set the JDK location as well as the Android SDK location.
To get your JDK location, run /usr/libexec/java_home -v 11 in terminal. Send 1.7 for Java 7, 1.8 for Java 8, or 11 for Java 11.

Android Studio Arctic Fox (2020.3.1)
In Android Studio Arctic Fox (2020.3.1 Patch 4), the JDK Location setting is moved to:
File > Settings > Build, Execution, Deployment > Build Tools > Gradle > Gradle JDK
Up to Android Studio 4.2
You can follow the answer from Ben Kane, the JDK Location setting is located in:
File > Project Structure > SDK Location

For new Android Studio versions, go to C:\Program Files\Android\Android Studio\jre\bin(or to location of Android Studio installed files) and open command window at this location and type in following command in command prompt:-
java -version

The gradle sync uses the following JDK location:
In Android Studio, File -> Project Structure
Navigate to the SDK Location.
cd /Applications/Android\ Studio\ 4.1.0.app/Contents/jre/jdk/Contents/Home
➜ Home ./bin/java --version
openjdk 11.0.8 2020-07-14
OpenJDK Runtime Environment (build 11.0.8+10-b944.6916264)
OpenJDK 64-Bit Server VM (build 11.0.8+10-b944.6916264, mixed mode)

On a Mac, you can use terminal to go to /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home (or wherever your Android SDK is installed) and enter the following in the command prompt:
./java -version

In Android Studio 4.0.1, Help -> About shows the details of the Java version used by the studio, in my case:
Android Studio 4.0.1
Build #AI-193.6911.18.40.6626763, built on June 25, 2020
Runtime version: 1.8.0_242-release-1644-b01 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Windows 10 10.0
GC: ParNew, ConcurrentMarkSweep
Memory: 1237M
Cores: 8
Registry: ide.new.welcome.screen.force=true
Non-Bundled Plugins: com.google.services.firebase

This is old question but still my answer may help someone
For checking Java version in android studio version ,
simply open Terminal of Android Studio and type
java -version
This will display java version installed in android studio

gradle.properties:org.gradle.java.home=/Applications/Android\ Studio.app/Contents/jre/Contents/Home
./gradlew assembleDebug -Dorg.gradle.java.home=/Applications/Android\ Studio.app/Contents/jre/Contents/Home

Related

Java SDK 11.0 or above is required when using .NET 6 or higher

I've installed .NET 6 (version 6.0.100-rc.1.21458.32) and Visual Studio 2020 Preview (although it might be not a prerequisite).
I am trying to debug a hello-world application but I am getting a build error regarding Java.
C:\Program Files\dotnet\packs\Microsoft.Android.Sdk.Windows\31.0.100-rc.1.12\targets\Microsoft.Android.Sdk.Tooling.targets(20,5): error XA0031:
Java SDK 11.0 or above is required when using .NET 6 or higher.
I've installed Java 17 setting Path as well as pointing to the Java Development Kit Location in Visual Studio settings (tools->options->Xamarin->Android Settings). Because that didn't work I've set the JDK location to Microsoft's openjdk, but it still doesn't work.
What's wrong here?
Installing Microsoft's OpenJDK 11 did the job for me.
You may notice that Visual Studio installs a version of Microsoft
OpenJDK, however, you need to install the Microsoft OpenJDK 11
When I installed VS2022 preview 4, my configuration became C:\Program Files\Microsoft\jdk-11.0.10.9-hotspot
This can be fixed for you if you run the maui-check command (if you installed that component during visual studio installation).
If you don't have the maui-check tool, you can get it by running the dotnet tool install -g Redth.Net.Maui.Check command
Maybe you skipped a step during installation and that's why you get the error.
For those who have the same problem in Jetbrains Rider :
Install OpenJDK 11 (Microsoft's OpenJDK 11 seems to be better) as mentioned by #themelis
Press Ctrl + Shift + A and search MSBuild global properties or go to File > Settings > Build, Execution, Deployment > Toolset and Build
Edit the MSBuild global properties and add the following property:
Name: JavaSdkDirectory
Value: {OpenJDK installation directory} (mine is C:\Program Files\Microsoft\jdk-11.0.16.101-hotspot)
Save and Enjoy!
Visual Studio for Mac
Here's how I got it working on macOS with Visual Studio for Mac.
Install Microsoft Open JDK
On macOS, install the Microsoft Open JDK using HomeBrew:
brew install --cask microsoft-openjdk
Configure Visual Studio for Mac
In Visual Studio for Mac, select Visual Studio -> Preferences.
In the Preferences window, navigate to SDK Locations -> Android -> Locations.
In the Java SDK (JDK) Location: text box, enter the following:
/Library/Java/JavaVirtualMachines/jdk-13.0.1.jdk/Contents/Home
Click OK

Xamarin Forms Previewer Not Working for Android in Visual Studio Due to Java Version

I am running...
Visual Studio Community 2017 15.0.0+26228.12
Xamarin 4.3.0.795 (aece090)
Xamarin.Android SDK 7.1.0.43 (3a62f1e)
Running the Forms Previewer for the iOS platform works fine, but once I select the Android platform the following prompt shows on the preview window:
"Something went wrong. Unsupported version of Java detected. The previewer recquires a 64-bit version of Oracle Java JDK 8 (1.8) or higher. Please install it and then set the Java JDK location in the Xamarin Studio or Visual Studio preferences."
I have installed the most recent Java Development Kit (Java SE Development Kit 8u121). It's located in the default location C:\Program Files (x86)\Java\jdk1.8.0_112 and I have pointed it succesfully from with in Visual Studio in Tools->Options->Xamarin->Android Settings->Java Development Kit Location.
I'm not sure what else to do as I have followed the on screen prompt and installed Java JDK 8 multiple times.
Thanks for any suggestions ahead of time!
When you're on Oracle's JDK download page, you need to choose the x64 JDK instead of the x86 JDK. (After installing the 64 bit JDK it should be in C:\Program Files\ not C:\Program Files (x86)\

How to choose Java version for Android Studio

I downloaded Android Studio latest version and installed Java 8. When I launch Android Studio I got below image.
Android studio found my Java version is 1.6 which is deprecate to run it. But I have installed java 1.8.0_101 and set the JAVA_HOME correctly. Please see below terminal output:
zhaoyi0113:Contents zhaoyi0113$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home
zhaoyi0113:Contents zhaoyi0113$ java -version
java version "1.8.0_101"
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)
I wonder how to setup java version for android studio?
EDIT1
I have tried to reboot my mac os but it still got the same error.
EDIT2
This dialog block me using android studio so there is no chance for me to click android studio menu or doing any configuration on it. When I click the update button, my browser will be open to jdk1.6 download page.
you must edit Info.plist :
to right click on the icon for the Android application Studio and choose " Show Package Contents "
Open Contents/Info.plist with a TextEdit, at the line :
<key>JVMVersion</key>
<string>1.6</string>
, changes in
<key>JVMVersion</key>
<string>1.7*,1.8+</string>
Sorry for my very bad english !
Go to File > Project Structure and pick the correct SDK and JDK location.

Error:Could not initialize class com.android.sdklib.repositoryv2.AndroidSdkHandler

I want to build this project with Android Studio at launching, but take a error. Stacktrace is here
This problem occurs when there are multiple JDKs installed in your system, I had the same issue as I had mistakenly installed oracle-jdk-9 and Android studio requires oracle-jdk-8
If you are using Ubuntu you can install jdk-8 from this question.
So, Make following changes as shown below:
Press ctrl+shift+alt+s that will open project structure which can also be opened from
File -> Project Structure
And then change JDK Location where you may have installed JDK 8
this because your classpath build tools in build.gradle root project is deprecated update like this for new android studio 3.2.0
buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:3.2.0-alpha14'
}
and after that update your minimum sdk and build tools to latest and no problem again
I had the same problem. I had installed Java 8 and Java 9.
I set JAVA_HOME to Java 8, but Gradle was using Java 9.
I changed the JDK in File->Project Structure to Java 8 and it worked.
JAVA 9 has forcefully taken over the JAVA_HOME to itself.
after installing java9 you will find
# java -version
java version "9.0.1"
Java(TM) SE Runtime Environment (build 9.0.1+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.1+11, mixed mode)
So instead of uninstalling java 9 let us point the JAVA_HOME back to java 8
First find out the Android studio embeded JRE location
and add an entry in ~./bash_profile at last
export JAVA_HOME="/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home"
# source ~/.bash_profile
# java -version
java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)
source
http://scalebean.blogspot.com/2018/01/fix-gradle-could-not-initialize-class.html
Changing the JDK in Android Studio to point back at 8 didn't work for me. I uninstalled Java 9 and this solved the issue.
One simple solution:
React-Native isn't compatible with Java 9 or Java 10
Resolve this issue by simply downgrading to Java 8
Go to build.gradle,
and make sure your gradle dependencies up to date.
For Android studio 4.2.2
dependencies {
classpath 'com.android.tools.build:gradle:4.2.2'
.........
.........
}
If you have several Javas in Ubuntu, you can
sudo update-alternatives --config java
sudo update-alternatives --config javac
to select the Java 8 that Android and Gradle need.
If anyone is having issues with IntelliJ IDEA, go to File -> Project Structure (or press Ctrl + Alt + Shift + S) and remove all JDK 9 entries. JDKs are marked under Platform Settings -> SDKs with a folder behind a tiny blue cup of coffee.
I tried adding the correct path to JDK 1.8 but IntelliJ IDEA will default back to JDK 9 for whatever reason. Unfortunately, I need JDK 9 for other projects, so uninstalling it would be too much of a hassle. If you need to use JDK 9 for another project (like I do), you can still add a JDK 9 entry via the same menu and then remove it again when required.
If the Problem Persists, just import the gradle to Android Studio and build the file inside the Android Studio.
Android Studio automatically handles gradle and sdk problems.
After successful build you can also try running the app via react-native run-android in the
Command Prompt. Hope this Helps.
This was the only fix that fixed this issue.
I tried all the above answers but problem persisted

Android Studio Emulator - Java instance does not support 32 bit JVM

I am running android studio on a Mac os x maverick. I have installed the 64bit JDK. When I run the emulator I get the message
Error:Abnormal build process termination:
Error: This Java instance does not support a 32-bit JVM.
Please install the desired version.
in android studio.
java -version
returns
version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)
and
$JAVA_HOME
points to
/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home
So every thing seems to be fine. What could possibly be wrong?
Update:
I still had an old jdk1.6 lying around on my mac. When removing it Android Studio was asking to install Java 6 again. So apparently Android Studio needs Java 6 to compile (as confirmed by Andrew in the comment of this question). Hoping that it would install the x64 version I let it install Java 6. However I still wasn't successful afterwards. I neither could find Java 6 for Mac OS X 64 Bit. Any hints on that?
Android Studio can run on whatever it needs, but the error seems to be pointing to the SDK that the project is using. To check what your project is using, go into Project Structure > Project Settings > Project. You should now see the Project SDK. It should say something like "Android API ## Platform (java version "1.7.[current version number]"). If it doesn't say 1.7.[current version number], then it probably needs to be updated.
Here's what you can do to update it:
In Project Structure > Platform Settings > SDKs, click the "+" button to add a new SDK.
In the pop-up, go into your Android SDK folder and click "Choose"
Another pop-up will appear asking for which SDK and JDK you want to use. Choose any Android SDK and the 1.7 JDK.
Go to Project Structure > Project Settings > Project and change your Project SDK to the one you just created. You should see the name of the SDK with the new Java version that you installed.
I am not sure but I think that sometimes the error is actually stating a falsehood, and is misleading.
I agree with #Maxwell inasmuch as the problem is a mismatch between the JDK configured for the project and the JDK that IntelliJ is running under.
The specific problem I encountered was Running IntelliJ 13.1.1 with 1.6.0_65 ... x86_64 while compiling a project configured with a Java 7 x64 JDK. Reconfiguring the project to an x64 Java 6 JDK put out the fire.
I resolved this issue by doing the following:
Go to File -> Project Structure... -> Platform Settings -> SDKs
Select the SDK listed
Click on the ' - ' to remove it.
Restart Android Studio
Go to File -> Project Structure -> Platform Settings -> SDKs
Select the SDK listed
Click on the ' + ' to add it (follow path to your JDK HOME)
Restart Android Studio, and re-Build your Project.

Categories