Hasan A Yousef
Hasan A Yousef

Reputation: 24948

Downloading Android toolchain to work with Flutter without Android Studio

I'm doing fresh install at McBook air, and wanted to work with Flutter at Android without Android Studio, so I did the below:

I downloaded flutter-sdk and added it to the permeant path as:

Hasans-Air:~ h_ajsf$ sudo code $HOME/.bash_profile

Adding:

PATH=$PATH:/usr/local/Cellar/flutter/bin

Then, saving the file, and running:

Hasans-Air:~ h_ajsf$ source $HOME/.bash_profile

And it was confirmed to be added by running:

Hasans-Air:~ h_ajsf$ echo $PATH

Installed android-sdk:

Hasans-Air:~ h_ajsf$ brew cask install android-sdk
==> Caveats
You can control android sdk packages via the sdkmanager command.
You may want to add to your profile:

  'export ANDROID_SDK_ROOT="/usr/local/share/android-sdk"'

android-sdk requires Java 8. You can install it with

  brew cask install homebrew/cask-versions/java8

==> Satisfying dependencies
==> Downloading https://dl.google.com/android/repository/sdk-tools-darwin-433379
######################################################################## 100.0%
==> Verifying SHA-256 checksum for Cask 'android-sdk'.
==> Installing Cask android-sdk
==> Linking Binary 'android' to '/usr/local/bin/android'.
==> Linking Binary 'archquery' to '/usr/local/bin/archquery'.
==> Linking Binary 'avdmanager' to '/usr/local/bin/avdmanager'.
==> Linking Binary 'jobb' to '/usr/local/bin/jobb'.
==> Linking Binary 'lint' to '/usr/local/bin/lint'.
==> Linking Binary 'monkeyrunner' to '/usr/local/bin/monkeyrunner'.
==> Linking Binary 'screenshot2' to '/usr/local/bin/screenshot2'.
==> Linking Binary 'sdkmanager' to '/usr/local/bin/sdkmanager'.
==> Linking Binary 'uiautomatorviewer' to '/usr/local/bin/uiautomatorviewer'.
==> Linking Binary 'mksdcard' to '/usr/local/bin/mksdcard'.
==> Linking Binary 'monitor' to '/usr/local/bin/monitor'.
🍺  android-sdk was successfully installed!
Hasans-Air:~ h_ajsf$ 

And added Anroid-SDK_ROOT as requisted above:

export ANDROID_SDK_ROOT="/usr/local/share/android-sdk"

Installed Platform-tools:

Last login: Sat Nov 10 14:13:29 on ttys002
Hasans-Air:~ h_ajsf$ brew cask install android-platform-tools
==> Satisfying dependencies
==> Downloading https://dl.google.com/android/repository/platform-tools_r28.0.1-
######################################################################## 100.0%
==> Verifying SHA-256 checksum for Cask 'android-platform-tools'.
==> Installing Cask android-platform-tools
==> Linking Binary 'adb' to '/usr/local/bin/adb'.
==> Linking Binary 'dmtracedump' to '/usr/local/bin/dmtracedump'.
==> Linking Binary 'etc1tool' to '/usr/local/bin/etc1tool'.
==> Linking Binary 'fastboot' to '/usr/local/bin/fastboot'.
==> Linking Binary 'hprof-conv' to '/usr/local/bin/hprof-conv'.
==> Linking Binary 'mke2fs' to '/usr/local/bin/mke2fs'.
🍺  android-platform-tools was successfully installed!
Hasans-Air:~ h_ajsf$ 

I already installed OpenJDK:

$ brew cask install java

And Added JAVA_HOME by:

$ sudo code $HOME/.bash_profile

Adding line:

export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/A/Commands

So that my $HOME/.bash_profile became as:

PATH=$PATH:/usr/local/Cellar/flutter/bin
export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/A/Commands

Then run:

$ source $HOME/.bash_profile

And confirmed the addition by:

Hasans-Air:lib h_ajsf$ echo $JAVA_HOME
/System/Library/Frameworks/JavaVM.framework/Versions/A/Commands

As well as Gradle:

$ brew install gradle

And installed VS code as:

$ brew cask install visual-studio-code

While checking with flutter doctor, I got the below:

Hasans-Air:~ h_ajsf$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel beta, v0.8.2, on Mac OS X 10.14.1 18B75, locale en-SA)
[!] Android toolchain - develop for Android devices
    ✗ Android SDK is missing command line tools; download from 

When I run the command env, I get the below:

Hasans-Air:~ h_ajsf$ env
TERM_PROGRAM=Apple_Terminal
SHELL=/bin/bash
TERM=xterm-256color
TMPDIR=/var/folders/_k/x16061cd4rj7sj1z0fkcqghw0000gn/T/
Apple_PubSub_Socket_Render=/private/tmp/com.apple.launchd.bX9BTj1wQC/Render
TERM_PROGRAM_VERSION=421.1
TERM_SESSION_ID=C2B37969-41A9-467F-83E4-7E3DAC126B6F
USER=h_ajsf
SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.48dHTxJRcg/Listeners
PATH=/usr/local/Cellar/flutter/bin:/Users/h_ajsf/.cargo/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/tools:/tools:/platform-tools:/platform-tools
PWD=/Users/h_ajsf


JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/A/Commands
XPC_FLAGS=0x0
XPC_SERVICE_NAME=0
SHLVL=1
HOME=/Users/h_ajsf
LOGNAME=h_ajsf
LC_CTYPE=UTF-8
_=/usr/bin/env
Hasans-Air:~ h_ajsf$ 

When I check for the variables, I get the below:

Hasans-Air:~ h_ajsf$ /usr/libexec/java_home
/Library/Java/JavaVirtualMachines/openjdk-11.0.1.jdk/Contents/Home
Hasans-Air:~ h_ajsf$ /usr/libexec/android_home
-bash: /usr/libexec/android_home: No such file or directory
Hasans-Air:~ h_ajsf$ /usr/libexec/android_sdk_root
-bash: /usr/libexec/android_sdk_root: No such file or directory
Hasans-Air:~ h_ajsf$ 

My Question is: Where is the mistake I did, why it is saying Android SDK missing command tool, and how to fix it"?

Upvotes: 0

Views: 4337

Answers (1)

Hasan A Yousef
Hasan A Yousef

Reputation: 24948

I found the issue is having OpenJDK that does not have a bin to define JAVA_HOME as required in flutter, so I installed java8 and setup JAVA_HOME as explained here:

// Remove OpenJDK
Hasans-Air:~ h_ajsf$ brew cask uninstall java --force
// Tap casks versions
Hasans-Air:~ h_ajsf$ brew tap caskroom/versions
// Search or required java version
Hasans-Air:~ h_ajsf$ brew search "java*"
// Confirm required java version info
Hasans-Air:~ h_ajsf$ brew cask info java8
// Install required java version
Hasans-Air:~ h_ajsf$ brew cask install java8
// Opened .bash_profile file
Hasans-Air:~ h_ajsf$ sudo code $HOME/.bash_profile
// Add the below to the .bash_profile file
export JAVA_HOME=$(/usr/libexec/java_home)
// Save the file
// Refresh the environment variables
Hasans-Air:~ h_ajsf$ source $HOME/.bash_profile
//Check for JAVA_HOME
Hasans-Air:~ h_ajsf$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home

So, my $HOME/.bash_profile became:

PATH=$PATH:/usr/local/Cellar/flutter/bin 
export JAVA_HOME=$(/usr/libexec/java_home) 
export ANDROID_HOME=/usr/local/share/android-sdk

Then I added the required Android licenses by running:

Hasans-Air:~ h_ajsf$ flutter doctor --android-licenses

Then I run Flutter doctor and found the chain had been setup correctly:

Hasans-Air:~ h_ajsf$ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel beta, v0.8.2, on Mac OS X 10.14.1 18B75, locale en-SA)
[✓] Android toolchain - develop for Android devices (Android SDK 28.0.3)
[✓] VS Code (version 1.28.2)

Then I created my first app:

Hasans-Air:Documents h_ajsf$ flutter create -i swift -a kotlin first_flutter

Then created avd following the below set of commands as explained here:

Hasans-Air:~ h_ajsf$ touch ~/.android/repositories.cfg // To avoid an error of missing folder
Hasans-Air:~ h_ajsf$ avdmanager list avd
Available Android Virtual Devices
Hasans-Air:~ h_ajsf$ sdkmanager 'platforms;android-28'
[=======================================] 100% Computing updates...             
Hasans-Air:~ h_ajsf$ avdmanager list target
Available Android targets:==============] 100% Fetch remote repository...       
----------
id: 1 or "android-28"
     Name: Android API 28
     Type: Platform
     API level: 28
     Revision: 6
Hasans-Air:~ h_ajsf$ sdkmanager --list
Hasans-Air:~ h_ajsf$ sdkmanager 'system-images;android-28;google_apis;x86_64'
Hasans-Air:~ h_ajsf$ avdmanager create avd -k 'system-images;android-28;google_apis;x86_64' -n flutter_avd -d 'android-28'
// OR
Hasans-Air:~ h_ajsf$ avdmanager create avd -k 'system-images;android-28;google_apis;x86_64' -n flutter_avd
Hasans-Air:~ h_ajsf$ /usr/local/Caskroom/android-sdk/4333796/tools/emulator -list-avds
Hasans-Air:~ h_ajsf$ /usr/local/Caskroom/android-sdk/4333796/emulator/emulator -avd flutter_avd

In another terminal

Hasans-Air:first_flutter h_ajsf$ flutter run

NOTE

You may need to fix Modify flutter.gradle under ⁨flutter⁩ ▸ ⁨packages⁩ ▸ ⁨flutter_tools⁩ ▸ ⁨gradle⁩ by moving :

maven {
    url 'https://dl.google.com/dl/android/maven2'
}

in the top of:

jcenter()

So that the file: .flutter/packages/flutter_tools/gradle/flutter.gradle be updated to the below as shown here:

buildscript {
   repositories {
       maven {
          url 'https://dl.google.com/dl/android/maven2'
       }
       jcenter()
   }
   dependencies {
      classpath 'com.android.tools.build:gradle:3.1.2'
   }
}

Upvotes: 1

Related Questions