Can't run flutter project on android device with IntelliJ - java

My operating system is Pop!_OS 20.10. I am simply trying to run a flutter project, which I created on IntelliJ, on my android device using IntelliJ IDEA. The build is being completed without any errors but when running the app, it says that my JAVA_HOME variable is set to an invalid directory:
Launching lib/main.dart on Mi A2 in debug mode...
Running Gradle task 'assembleDebug'...
ERROR: JAVA_HOME is set to an invalid directory: /app/extra/idea-IU/jre64
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation.
Exception: Gradle task assembleDebug failed with exit code 1
SDKs installed on my platform settings are openjdk-15.0.2, corretto-1.8 and Android API 30 Platform. I didn't manually altered any environment variables before getting this error. After this, I tried to add JAVA_HOME variable to ~/.bashrc and /etc/environment files to point my JDK installation folder like this:
JAVA_HOME="/usr/lib/jvm/default-java"
and also tried with my current java installation:
JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64"
but no luck. I did a lot of research and really don't know what to do at this point.

There are two possibilities
1 - Your flutter sdk is may be some old version. Run flutter upgrade to update your flutter sdk.
2 - You might not have installed JDK or the JDK Path is not set in Environment Variables.

Related

I am getting this error as shown in body, while building a apk file in ubuntu, please help me to solve this error

chinmay#chinmay-Latitude-E6420:~/MyFiles/FlutterExercise/again$ flutter build apk
Building without sound null safety
For more information see https://dart.dev/null-safety/unsound-null-safety
ERROR: JAVA_HOME is set to an invalid directory: "/usr/lib/jvm/jdk-15-0.2"
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation.
Running Gradle task 'assembleRelease'...
Running Gradle task 'assembleRelease'... Done 138ms
Gradle task assembleRelease failed with exit code 1
chinmay#chinmay-Latitude-E6420:~/MyFiles/FlutterExercise/again$
There's something wrong with your environment configuration. Open Command Palette and choose Java: Configure Java Runtime.
Java Tooling Runtime --> Java Language Server, this option is equal to the setting "java.home". Java extension automatically detects the compatible jdk on local machine and lists the path here for user to choose. Make sure you've set JAVA_HOME properly on your local machine.
Don't forget to reload window to make the change effective.

Using different JDK locations on different processes might cause Gradle to spawn multiple daemons. [Android Studio 4.1] [duplicate]

I was building my test app in Android Studio, then in the Event Log it said:
Android Studio is using this JDK location: C:\Program
Files\Android\Android Studio\jre which is different to what Gradle
uses by default: C:\Program Files (x86)\Java\jdk1.8.0_181 Using
different locations may spawn multiple Gradle daemons if Gradle
tasks are run from command line while using Android Studio.
Then gave these options:
More info...
Set Android Studio to use the same JDK as Gradle and sync
Do not show this warning again
I clicked on:
Set Android Studio to use the same JDK as Gradle and sync
then my app didn't want to build any more.
This is the error:
Gradle sync failed: Unable to start the daemon process.
This
problem might be caused by incorrect configuration of the daemon.
For example, an unrecognized jvm option is used.
Please refer to
the User Manual chapter on the daemon at
https://docs.gradle.org/5.4.1/userguide/gradle_daemon.html
Process command line: C:\Program Files (x86)\Java\jdk1.8.0_181\bin\java.exe
-Xmx1536m -Dfile.encoding=windows-1252 -Duser.country=ZA -Duser.language=en -Duser.variant -cp C:\Users\{MyUserName}\.gradle\wrapper\dists\gradle-5.4.1-all\3221gyojl5jsh0helicew7rwx\gradle-5.4.1\lib\gradle-launcher-5.4.1.jar
org.gradle.launcher.daemon.bootstrap.GradleDaemon 5.4.1
Please read
the following process output to find out more:
Error occurred during initialization of
VM Could not reserve enough space for 1572864KB object heap
Consult IDE log for more details (Help | Show Log) (16 s 166 ms)
I checked the event logs:
I think previously it did this:
Instructing gradle to use java from C:/Program Files/Android/Android
Studio/jre
Now it says this:
Instructing gradle to use java from C:/Program Files
(x86)/Java/jdk1.8.0_181
How do I set it back to the way it was so my app can build again?
Any help / advice will be appreciated
Go to File, Project Structure, SDK location and change the JDK location dropdown to JAVA_HOME.
You need to change/add an environment variable.
Follow this link to know how to change/add environment variables.
Now add/modify the variable 'JAVA_HOME' and set the path to C:\Program Files\Android\Android Studio\jre
Note: When you are going to work on a different Java project later on in a different IDE, you might have to change this variable again to the default Java location (in this case perhaps C:/Program Files (x86)/Java/jdk1.8.0_181)
Ubuntu 16.04 LTS, Android Studio 3.6
That's how I got rid of the warning:
1) locate the AS launcher script studio.sh on your machine, mine is under: /opt/android-studio-3.x/bin
2) inside the script, you'll find the following lines:
# ---------------------------------------------------------------------
# Locate a JDK installation directory which will be used to run the IDE.
# Try (in order): STUDIO_JDK, studio.jdk, ./jre64, JDK_HOME, JAVA_HOME, "java" in PATH.
# ---------------------------------------------------------------------
if [ -n "$STUDIO_JDK" -a -x "$STUDIO_JDK/bin/java" ]; then
JDK="$STUDIO_JDK"
fi
if [ -z "$JDK" -a -s "$HOME/.AndroidStudio3.6/config/studio.jdk" ]; then
USER_JRE=`"$CAT" $HOME/.AndroidStudio3.6/config/studio.jdk`
...
3) if, for some reason, you're unable to export your environment variables to the launcher (like it happens to me from the xfce4 session), you can write the desired JAVA_HOME value inside the studio.jdk file whose location on my system, as per the script, is in $HOME/.AndroidStudio3.6/config/studio.jdk.
$ echo $JAVA_HOME > $HOME/.AndroidStudio3.6/config/studio.jdk
$ cat $HOME/.AndroidStudio3.6/config/studio.jdk
/usr/lib/jvm/java-8-oracle
4) lastly, by restarting AS from the DE session, you should see the JAVA_HOME under the File -> Project Structure -> SDK Location window set to the above value and both AS and gradle should use the same JRE:
On Android Studio I did:
File > Project Structure... > SDK Location (On the left pane)
Then at the bottom there is a drop down below: JDK location:
I then selected / browsed to this directory:
C:\Program Files\Java\jdk1.8.0_181
Gradle Sync worked again for me after that but I still get this warning:
Android Studio is using this JDK location: C:\Program
Files\Java\jdk1.8.0_181 which is different to what Gradle uses by
default: C:\Program Files (x86)\Java\jdk1.8.0_181. Using
different locations may spawn multiple Gradle daemons if Gradle tasks
are run from command line while using Android Studio.
More info...
Set Android Studio to use the same JDK as Gradle and sync project
Do not show this warning again
Gradle is clearly using my native java installation (from Oracle) whereas Android Studio is using its own version.
I have been ignoring this warning since the upgrade of Android Studio to 3.6.1.
No issue so far. Indeed, I have released new versions of my app without issues.
If this becomes annoying, and IF you have no other issues, you have an option to ignore future warnings: Just press the warning and select option to ignore future messages related to this warning.
Update for Android Studio "Artic Fox | 2020.3.1 Patch 4" running on Linux (5.3.0-62-generic #56~18.04.1-Ubuntu) using Xfce4 Desktop environment.
This way allows for using independent, i.e. not synchronized, JRE/JDK versions on system and Android Studio.
Changed: The Gradle settings have been moved to
File -> Settings -> Build, Execution, Deployment -> Build Tools -> Gradle
To use unique JRE/JDK for Gradle execution:
Open Gradle Settings and note the "Gradle JDK" Path.
Hint: To select
"Embedded JDK" first, if available, should be an appropriate choice.
Edit the properties of the Android Studio starter (on Start Menu, Desktop, system menu bar, ...) , which should look like
/home/USERNAME/android-studio/bin/studio.sh
and change to
/bin/bash -c "export JAVA_HOME=<GRRADLE JDK PATH FROM
1.>;/home/m/android-studio/bin/studio.sh"
Hint (untested): May be possible to insert the export JAVA_HOME=... statement into the studio.sh script, which may be overwritten on updates.
Check: After applying changes start Android Studio and execute Build -> Rebuild Project: The error message should have disappeared. Then check the same after File -> Invalidate Caches / Restart. Last, open the Android Studio embedded Terminal (bottom line click) and execute echo $JAVA_HOME. That should show the <GRADLE JDK PATH FROM 1.>, while a terminal opend from outside Android Studio should still show the (different) system's JAVA_HOME.

Getting error trying to launch react-native android application, JAVA_HOME is not set, no java command could be found in your path

I've been trying to launch a basic app using react native. So far, I've followed the guide here, https://reactnative.dev/docs/environment-setup, for react-native CLI quickstart. I'm using Windows, and using bash with Windows linux subsystem (ubuntu)
I've launched metro, but when I try the command,
npx react-native run-android --no-jetifier
I get the following error,
info JS server already running.
info Installing the app...
"ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation.
error Failed to install the app. Make sure you have the Android development environment set up: https://reactnative.dev/docs/environment-setup. Run CLI with --verbose flag for more details.
Error: Command failed: ./gradlew app:installDebug -PreactNativeDevServerPort=8081"
I've tried setting a path to the JRE location used by Android Studio, and also setting the path to C:\Program Files\Java\jre1.8.0_241, restarted etc.
Any insights?
I should add, when I try to launch the app through android studio (on an emulator) it gives this error,
Unable to load script. Make sure you're either running a metro server (run 'react-native start') or that your bundle 'index.android.bundle' is packaged correctly for release
Set JAVA_HOME but also, consider adding the bin to the PATH system variable
C:\Program Files\Java\jre1.8.0_241\bin
Find more information here https://confluence.atlassian.com/doc/setting-the-java_home-variable-in-windows-8895.html

Android Studio SDKmanager Error: Could not find or load main class

Currently I am trying to set up my Android Studio environment for Flutter and I am unable to update sdkmanager or else I would obtain the following errors :
This error affects the rest of my tools folder (sdkmanager,avdmanager, basically any .bat I run within that folder) as well.
What I have tried:
Clean installed and reinstalled Android Studio numerous times
Uninstalled and reinstalled Java numerous times (Version 13)
Change parts of my sdkmanager.bat and android.bat based on the other stack overflow questions--since this did not work, I reverted them back to the original code
Run as admin with command prompt and powershell
Installed JDK 8 because Flutter doesn’t like other versions of Java
Changed my environmental variables -these are the variables in relation to Android Studio and Java as of now
CLASSPATH - %JAVA_HOME%\lib;
JAVA_HOME - C:\Program Files\Java\jdk-13.0.1
PATH - C:\Users[username]\AppData\Local\Android;C:\Program Files\Java\jdk-13.0.1\bin;C:\Windows\system32;C:\Users[username]\Documents\flutter\bin;C:\Program Files\Git\git-cmd.exe;
Updates
For majority of the .bat files, %JAVA_OPTS% is causing the issue, I’ve tried to set a JAVA_OPTS variable but that does not help
Solution
I deleted %JAVA_OPTS% from the .bat file and it seems that was the issue, but I don’t think I’m supposed to delete that part.
Setting JAVA_OPTS causes issues on using JDK versions lower than 11 as mentioned on this issue thread. Removing this should resolve the issue. Another workaround to prevent similar issues is to use the JDK bundled with Android Studio.

Android Studio doesn't find JDK [duplicate]

I downloaded Android Studio and attempted to launch the program.
This is running on Windows 7 64-bit with Java 1.7. During the installation, my Java 1.7 is detected, and the rest of the installation goes through just fine. However, when attempting to launch the application from the desktop icon, nothing happens. Looking at the task manager, a new process from the CMD is loaded. This is because it's attempting to run the batch file studio.bat.
When I execute via CMD, I get the following error:
ERROR: cannot start Android Studio. No JDK found. Please validate
either ANDROID_STUDIO_JDK or JDK_HOME or JAVA_HOME points to valid
JDK installation. ECHO is off. Press any key to continue . . .
I've attempted to open the idea properties file to see if there was something I could configure for this ANDROID_STUDIO_JDK or something like that. However, I found nothing. I hope some of you can let me know if you were able to install this or if you are having problems as well.
Adding a system variable JDK_HOME with value c:\Program Files\Java\jdk1.7.0_21\ worked for me. The latest Java release can be downloaded here.
Additionally, make sure the variable JAVA_HOME is also set with the above location.
OK, I figured out how fix this nasty bug.
Before you start
Go to your Android Studio installation folder and locate the bin folder. Inside the bin folder, you will find studio.bat. Execute the file, and it'll show the error. If it is about the Java path then follow the tip 1.
Tip 1
When you set the path JAVA_HOME, etc., make sure not to include bin at the end of the path. This solved the issue for me.
JAVA_HOME => C:\Program Files\Java\jdk1.7.0_21
path => C:\Program Files\Java\jdk1.7.0_21\bin
It works fine with JDK 1.7 (I tried with 32 bit).
If you do so, you can see the initial screen as below.
Tip 2
Are you getting the below error message?
Your Android SDK is out of date or is missing templates. Please ensure
you are using SDK version 22 or later.
This error probably occurs, because you have reference to your older SDK (that you downloaded with Eclipse) in your path variable. Go to Environment variables and remove any reference to the old SDK folder and point it to new SDK folder that is found inside the Android Studio installation folder.
Save and restart Studio, things should work as expected now.
Tip 3
If you hate messing around with the path variable as given above by removing old references, simply follow the following steps to refer to new SDK path that comes with Studio.
*Project Defaults* -> *Project Structure* -> Click "New" -> Select "Android SDK" -> Select the SDK folder inside the studio installation.
With the last update of Android Studio, I have two versions of the IDE's launcher
One is called studio.exe and the other studio64.exe they are both on:
C:\Users\myUserName\AppData\Local\Android\android-studio\bin
You have to launch the one that matches your Java version 64 or 32 bit
I had the same issue. I got resolved setting up correctly the environment variables in windows, for instance:
JAVA_HOME -> C:\Program Files\Java\jdk1.6.0_45
path -> C:\Program Files\Java\jdk1.6.0_45\bin
I've tried so many of the answers here but none of them works, so I decided to mix some of the answers here and I am successful!
Step 1: Go to the system properties by right-clicking on My Computer or by pressing windows button on typing This PC and right clicking on it and selecting Properties.
Step 2: Click the advanced system settings or Environment Variables
Step 3: Take note that there are 2 different variable. What you need to create is system variables not user variables, when you clicked new type the following
Variable name: JAVA_HOME
Variable value: C:\Program Files\Java\jdk1.8.0_25\ (Note: Please check if the path is right, sometimes it is C:\Program Files (x86)\Java\jdk1.8.0_25)
Step 4: Run the android studio, no need to restart.
Note:
*C:\Program Files\Java\jdk1.8.0_25\ depends entirely on the installation path of your JDK not JRE so don't be confused if you see something like the picture below. Just enter the location of your jdk, in my case it is C:\Program Files\Java\jdk1.8.0_25\.
*Do not include the bin folder when you enter the Variable value.
Edit: For Windows 8 and 10 Users: Try to run C:\Program Files\Android\Android Studio\bin\studio.exe instead of C:\Program Files\Android\Android Studio\binstudio64.exe
I had the same issue. I am having 64-bit windows 8. I downloaded the android studio which worked on a 32-bit machine but not on my 64-bit.
The solution for me was pretty simple. I navigated to
C:\Program Files (x86)\Android\android-studio\bin
there I saw 2 exe files studio.exe and studio64.exe. Normally my start menu was pointing to studio64.exe which always kept on giving me "The environmental variable JDK_HOME does not point to valid JVM". So then I clicked studio.exe and it worked :)
I hope this may help someone facing the same problem as me
The path for the latest JDK. It worked very well.
If you are getting message "Your Android SDK is out of date…" — click "Configure" —> "Project Defaults" —> "Project Structure", pick "SDKs" —> "Android SDK" and in a "Build Target" choose "Android 4.2.2". Click "OK". Now it should work fine.
I got the problem that the installation stopped by the "$(^name) has stopped working" error. I have installed the Java SE Development kit already, and also set both SDK_HOME and JAVA_HOME that point to "C:\Program Files\Java\jdk1.7.0_21"
My laptop installed with Windows 7 64 bits
So I tried to install the 32-bit version of the Java SE Development kit, and set my JAVA_HOME to "C:\Program Files (x86)\Java\jdk1.7.0_21", restart and the installation worked OK.
This problem has been fixed in Android Studio v0.1.1, so just update Android Studio and it should work.
Sometimes you can resolve this type of issue by setting environment variables so the process looks for the JDK in the right spot.
Another approach is to figure out where the process is looking, then put your JDK there.
I've had lots of success using Process Monitor from Sysinternals:
Start -> Run -> procmon
Go to the Filter menu, then select Filter...
Set Event Class is File System then Include
Add, then OK
This will filter down to all the file system operations taking place on your machine.
You could try filtering based on the result of the File System operation:
Right click on the Result column, then Exclude 'SUCCESS'
You can also quite easily filter based on the process name, either from the Filter... menu option or by right clicking on the Process Name column and selecting Include 'process.exe'.
Once you find the file system operation that's failing, the Path column will tell you where to put your JDK.
I've found this to be very empowering. Especially compared to the frustrating process of changing environment variables by trial and error.
My issue was caused because I have an & character in my Windows user name, so when installed in the default path I was getting the following error after running bin/studio.bat
|
v notice broken path
The system cannot find the file C:\Users\Daniel \studio64.exe.vmoptions.
Exception in thread "main" java.lang.NoClassDefFoundError: com/intellij/idea/Main
Caused by: java.lang.ClassNotFoundException: com.intellij.idea.Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: com.intellij.idea.Main. Program will exit.
So I uninstalled and reinstalled it to program files and it launches fine now.
I couldn't get this to work no matter which environment variables I set. So I simply put a copy of the JDK into my Android Studio installation folder.
Copy the contents of the JDK installation (for example, C:\Program Files (x86)\Java\jdk1.7.0_21)
Paste them into the installation directory of the Android Studio (for example, C:\Program Files (x86)\Android\android-studio)
I somewhat assumed that the issue was caused by having the x64 version of the JDK installed. But what was especially confusing was the fact that I could start Android Studio just fine when I started the studio.bat as an Administrator (even though the environment variables were set for my personal user account).
studio.bat will look for several valid options when determining which JDK to use.
:: Locate a JDK installation directory which will be used to run the IDE.
:: Try (in order): ANDROID_STUDIO_JDK, ..\jre, JDK_HOME, JAVA_HOME.
As explained above, I picked the ..\jre option.
Today I found another situation when this problem occures - when you have several JDK, defined in JAVA_PATH. I have:
JAVA_HOME = C:\JAVA\JDK\jdk1.6.0_38;C:\JAVA\JDK\jdk1.7.0_10
So I received this problem with Android Studio setup
But when I've removed one of JDK - problem has been solved:
JAVA_HOME = C:\JAVA\JDK\jdk1.7.0_10
Installation wisard found my jdk and i had a nice night to study studio.
But unfortunatelly even installed studio doesn't work with several jdk.
Does anybody know how to fix it?
I hope I've helped someone
Add JAVA_HOME and JDK_HOME system environment variables.
Right-click
android studio (the 64 version if that's your OS and JDK) and go to
compatibility tab and set "Run as administrator" to true.
Run android studio and high-five me virtually when it works.
If that fails, try "where java" in cmd.exe. If it lists c:\system32\java.exe first, then rename the file and try again.
Windows 64 bit, JDK 64 bit (Solution that worked for me)
Tried all the above solutions, and None of them worked, I have been trying to solve it for the past few days and now I did it successfully. For me the problem was when I first installed Android Studio my JDK version was 1.7, then after installing I updated the JDK to 1.8, then I removed the old JDK folder and everything was messed up, even uninstalling and reinstalling android studio randomly didn't solve the issue.
Below is the solution that worked for me
Uninstall Android Studio.
clean temp files and android studio C:\Users\Username.AndroidStudio1.5
Uninstall JDK.
Now without JDK try to install Android Studio and now it will show the message that it can't find any JDK. Stop installation
Install JDK 1.7 or 1.8 (Set JAVA_HOME, JDK_HOME, path Environment variables as explained by everybody above)
Install Android Studio.
Done. Enjoy and happy coding.
MAKE SURE YOU RESTART ANDROID STUDIO
Even though I should know better and swear I did, make sure you restart studio after making these changes as it clearly does not check them on every build (which to me makes sense that system/user variables should only be read once on startup)
Anyway, yea... Make sure you restart after you make these changes.
Path = to the bin folder in the jdk folder (path already exists)
JAVA_HOME = to the jdk folder
Install the latest JDK in your system from JDK 7 and JRE 7 Installation Guide.
JAVA_HOME -> C:\Program Files\Java\jdk1.7.0_4
path -> C:\Program Files\Java\jdk1.7.0_4\bin
If setting the JAVA_HOME variable doesn't work for you, set:
STUDIO_JDK=C:\Progra~1\Java\jdk1.8.0_172
Where the path points to your JDK installation
This is the only thing which works for me in Windows 32bit with Android 10 and up.
In my experience, I was unable (even after adding JDK_HOME) to launch Studio via either the shortcut or studio.exe itself. I had to first run bin/studio.bat (mentioned in the original question).
After the first successful launch, I'm able to start it with the shortcut.
If you have a 64 bit windows OS, pointing the JAVA_HOME system variable to
C:\Program Files (x86)\Java\jdk1.7.0_21
Will work when
C:\Program Files\Java\jdk1.7.0_21
fails to work.
You need 1.7 JDK installed on your system. Add a system variable with:
name: ANDROID_STUDIO_JDK
path: your JDK path (for example, C:\Program Files\Java\jdk1.7.0_21)
See more at FIX: Android Studio - Installation Issues on Windows 7 | ERROR: cannot start Android Studio. No JDK found.
TRY TO INSTALL 32BIT JDK
if you have jdk installed and had set up the System Varibles such as JAVA_HOME or JDK_HOME
and tried click back and then next ,you might have installed the 64bit JDK,just download the 32bit jdk and install it.
I downloaded the latest jdk version
JAVA_HOME to C:\Program Files\Java\jdk1.8.0_11\bin
Set the PATH to C:\Program Files\Java\jdk1.8.0_11\bin
I restarted the STUDIO and it worked.
On Windows 10, restarting the installer and running as admin worked for me.
To complete this stack of possible solutions: For me the problem was, that I did not execute the Android-Studio-Setup as administrator. Running it as administrator then made me able to install Android-Studio.
For me, the problem was that I had changed the GC vm arg to -XX:+UseParallelGC in the C:\Users\<username>\.AndroidStudio2.1\studio64.exe.vmoptions file. That's what I use in Eclipse and I was trying various things to get AndroidStudio half way as efficent as Eclipse. I restored the GC to -XX:+UseConcMarkSweepGC.
The answer to the original question is that, might be you are opening android studio from 32 bit shortcut icon of android studio, try to open from icon "studio64" located under .../bin/ where android studio setup is install.
In case you had it running but Now it doesn't Launch.
I deleted the C:\Users\<NAME>\.AndroidStudio<version>\ folder and it worked.

Categories