Flutter in IntelliJ
Part 3 in my “Flutter from a complete beginner” series.
Tiger asks… how do I keep using my preferred IDE?
VS Code seems to be a popular choice for Flutter projects. And there’s nothing wrong with that per se.
However, I’ve been using Jetbrain’s IntelliJ for years, both at work and personally. I like it, and I’m loathe to part with it, if I don’t have to.
Thankfully, I don’t have to.
Of course that begs the eternal question: IntelliJ or Android Studio?
As far as I’m aware, Android Studio is IntelliJ with some pre-configurations that are useful for Android developers.
Since I already have IntelliJ installed, I don’t think there’s a point in also installing Android Studio.
There’s a chance I’ll have to revise that statement at some point, because I’ve never developed for Android with IntelliJ before, but for now, I’ll be using Intellij IDEA Ultimate (the newest, 2023.3.3), because that’s what I already have. But it should work just as well with the free Community Edition.
If you’re not interested in the more detailed instructions and just want a quick “do this” checklist, skip ahead to the Summary
at the bottom of the article.
Installing the Flutter Plugin
That’s literally the only thing Flutter’s “set up an editor” page is mentioning, so we’re going to do that.
Ctrl+Alt+S
-> findPlugins
- open marketplace tab, search for
flutter
There’s a lot of flutter plugins, apparently, including for e.g. Bloc
, but for now, we’ll just be installing the Flutter
plugin. And the Dart
plugin it tells us it also needs.
then restart the IDE to activate the plugins.
Installing the Flutter SDK
IntelliJ now knows how to use Flutter, but we still need Flutter itself.
In my case, I’ll be installing on Linux and I’ll be using their snap
to do so:
sudo snap install flutter --classic
(if you’re confused by --classic
, there’s an excellent answer over on stackexchange that explains it. TLDR: it’s got nothing to do with flutter and only concerns the access permissions granted to flutter.)
Flutter bundles the Dart SDK. You don’t need to install Dart.
ok, let’s verify our installation with
flutter doctor
This tells us a lot of things, first of all, we want to run
flutter --disable-analytics
This is optional, but I don’t like telemetry.
next, flutter doctor
tells us exactly what is missing:
We need an Android SDK to use flutter for an android application. Not a big surprise. How else are we going to get our android emulator ?
We’ll cross that bridge when we come to actually running our application.
Creating a Flutter Project
IntelliJ should recognise the flutter SDK path. If it does not,
flutter sdk-path
should reveal it. From there, just go through the usual hoops of setting up a new project. I of course will set up the project outlined in the overview post of this series, which means we won’t be targeting iOS.
One git init
and a commit later, we’re good to go.
Personally, I like to generate a more comprehensive .gitignore
file on gitignore.io and replace the one IntelliJ
auto-generates. One word of caution when you do: at the time of this writing, gitignore.io only ignores *.iws
files, whereas the one generated by IntelliJ also ignores
# File-based project format
*.iml
*.ipr
*.iws
.idea/
So you may want to add these.
Running your app in an emulator
A new flutter project automatically comes with a “Hello world” boilerplate application. Apparently, that also includes some helpful comments that give you a mini-tutorial into Flutter. Handy.
For now, though, I’d like to blindly run the application in a virtual flutter device to make sure things work. As we can tell from the warning IntelliJ displays to us, they don’t.
On a side-note, I do like seeing the no device selected
drop-down in the top-right of the IDE. Makes me believe I did the right thing by not bothering with Android Studio.
Installing the Android SDK
I could just press the Configure...
in the warning, however I like to know where to find stuff in the absence of a warning, so I’ll be heading over to
File
-> Project Structure
-> +
instead. Except … that’s not where I want to be because it’s currently bugged.
If I select Download Android SDK
, IntelliJ asks me which version of the JDK I want to download. Which … is not what I want.
I’m sure it’s going to get patched at some point, but for now, I’ll double-press Shift
and search for android, which lets me know there’s something called an Android SDK Manager
, which sounds useful.
If we open that up, that looks more like what we’re looking for.
A press on Edit
, here, will finally start up the SDK Setup
Which will also install, as it informs us:
- the android emulator
- the SDK build tools
- the SDK platform and its tools
- the android sources
Useful, let’s do that. One licence-agreement screen later, we’ve finally installed the SDK.
Installing the correct Android Platform
But wait, the warning from Fig.5 is still here. That’s because the SDK Setup we just ran only let’s me install the latest platform … which is not the API level for which the generated project is configured for.
I could change that to the newest API, but let’s instead install the API the app expects:
One press on Apply
and … the warning from Fig.5 is still there.
That’s because although we’ve installed API 29, the project does not yet know to use it.
So back in the Project Structure
dialog, let’s change the Android API’s build target
to 29
.
Aaaaand … the warning is still there. -.-
Fine, we’ll use the darn Configure...
button:
In Fig.11, Android API
is what I renamed the SDK in the Project Structure
to. IMO, IntelliJ should have been happy with that, but apparently, it’s satisfied with just telling us that we could use it, if we wanted … I’ll instead select the Android API 29 Platform Android SDK
option, which is what IntelliJ detects we can add, now that we’ve installed it.
And the warning is gone. A brief look at the Project
structure informs us, that IntelliJ added the new API as its separate item, so I’ll just go ahead and remove the first.
Creating a android virtual device (AVD)
On the right edge of your IntelliJ, above the three flutter icons (that are the Flutter Inspector
, Flutter Performance
and Flutter Outline
tools), there’s a Device Manager
:
If for some reason it’s not there, double Shift
is your friend again.
There’s already a device pre-configured that I could download the system image for and use. I’m going to ignore that and configure my own device, instead, by pressing the +
.
The process is fairly straightforward. First select the size of the phone:
then select which system image the device should be running. If this is the first time using that image, we’ll need to download it.
Finally give it a name and we’re done. (I’ll skip the advanced options, for now.)
Running the application in an AVD
After refreshing our device list in the device drop-down, we can now start the AVD we created in the emulator.
It may take a while for IntelliJ to boot up the device, but eventually, the AVD will be ready and IntelliJ will display the device where before it said <no device selected>
.
If like me, you get confused that “nothing happens” when you choose the Open Android Emulator
, that’s because by default, the android emulator in IntelliJ is run in the Running Devices
tool window — and that window is not automatically opened when you start a new device.
This behaviour can be changed in the Android Emulator
settings (double Shift
to find it) with the Launch in a tool window
option.
I know it says here that Open the Running Devices tool window when launching an app
is an option, but at least for me, that did not happen.
We can now run the application with the green play or bug button next to the main.dart
dropdown.
Running your app on an actual device
Sometimes, we want/need to run our application on an actual device.
Fairly straightforward process:
- on the device, enable developer mode
(google how to do that for your particular device) - turn on USB debugging
- connect your android device to your development machine and on your device, allow USB debugging
- select the devicefrom the same drop-down you selected the AVD before
- run the application from IntelliJ
Satisfying flutter doctor
The application runs, however, if we run flutter doctor
to verify our flutter installation, flutter still complains:
First, the commandline tools. This one we can install from IntelliJ again. In the Android SDK Manager
, there’s a SDK Tools
tab, which contains an option to install the CLI tools.
Now in a new terminal, let’s run
flutter doctor --android-licenses
And if we run flutter doctor again
,
flutter warns that it cannot find Android Studio
, but that’s ok, we don’t care about that anyway.
Summary
To recap, these are the steps you need to do to successfully start your flutter app in the android emulator:
- install Flutter on your machine
sudo snap install flutter --classic
- (install IntelliJ, if you haven’t yet)
- install the
Flutter
andDart
plugins for IntelliJ - create a new Flutter Project in IntelliJ (this will create a demo app)
- open the
Android SDK Manager
in IntelliJ - install an Android SDK by pressing on the
Edit
option in the SDK manager - in the
SDK Platforms
tab, additionally install the Android SDK with the API level the demo app expects - configure the project to use the Android SDK you just installed
- in the
Project Structure
, make sure you only have one Android SDK added, remove the obsolete one(s), if necessary - use the
Device Manager
to create a new android virtual device - run your application in IntelliJ with the created AVD selected
- find the android emulator in the
Running Devices
tab - optionally configure IntelliJ to open the emulator in a window of its own instead of displaying it in that tab
to then fully satisfy flutter doctor:
- in the
SDK Tools
tab of IntelliJ’s Android SDK manager, install the android SDK command-line tools - in a new terminal accept all licenses flutter wants you to accept
flutter doctor --android-licenses