user6274128
user6274128

Reputation:

How to run a Dart program without running simulator/emulator in Android Studio

Is it possible to run this code in Android studio without firing up any device, I just want to print it to the console.

void main() {
  print("Hello World");
}

Note:

I am not looking for DartPad or any other online IDE to run Dart code.

Upvotes: 1

Views: 2412

Answers (3)

Amit Tumkur
Amit Tumkur

Reputation: 2835

As of 2024, you might need to create a run configuration as a Dart console in the latest Android Studio. Follow the below steps to get it working.

  1. Click on main.dart dropdown button in the toolbar beside the green play button and choose the Edit Configurations... option.

enter image description here

  1. In the current open window, click on the top-left + button to open the Add New Configuration menu

enter image description here

  1. Click on the Dart Command Line App. Enter Name for run config, Choose the path to your Dart file:

enter image description here

  1. Choose the working directory which will be your project folder. Then click the Apply and the OK buttons to close the window.

enter image description here

  1. Now you can run dart-only file by clicking the green play button making sure you have selected the newly created run configuration from the dropdown shown in the toolbar.

enter image description here

Upvotes: 1

Jean G
Jean G

Reputation: 167

in your Dart file right click on the green arrows in front of your main() function:

enter image description here

then click on the dart icon from the popup menuCreate ...

enter image description here

and then OK:

enter image description here

now you can run it without the simulator:

enter image description here

And get the console output in the Run window:

enter image description here

Upvotes: 3

jamesdlin
jamesdlin

Reputation: 90015

On a Unix-like system (e.g. Linux, macOS), if dart is in your executable PATH, you can add a shebang line to your .dart file:

#!/usr/bin/env dart

void main() {
  print("Hello World");
}

and mark your file executable (chmod a+x your_file.dart). Then you can run your_file.dart directly without needing to run dart your_file.dart explicitly.

(If the dart binary is not in PATH, then you would need to use #!/full/path/to/dart as the first line instead.)

On Windows, you could set up a file association for .dart files.

Upvotes: 1

Related Questions