下载Android工具链以在没有Android Studio的情况下与Flutter一起使用

时间:2018-11-10 12:25:45

标签: android flutter

我正在McBook air上进行全新安装,并且想在没有Android Studio的Android上与Flutter一起工作,所以我做了以下工作:

我下载了flutter-sdk,并将其添加为以下路径:

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

添加:

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

然后,保存文件并运行:

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

通过运行确认已添加:

Hasans-Air:~ h_ajsf$ echo $PATH

已安装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$ 

并按照上面的要求添加了Anroid-SDK_ROOT

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

已安装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$ 

我已经安装了OpenJDK

$ brew cask install java

并通过以下方式添加了JAVA_HOME

$ sudo code $HOME/.bash_profile

添加行:

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

这样我的$HOME/.bash_profile就变成了:

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

然后运行:

$ source $HOME/.bash_profile

并通过以下方式确认添加:

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

以及Gradle

$ brew install gradle

并将VS code安装为:

$ brew cask install visual-studio-code

在与flutter doctor进行检查时,我得到了以下信息:

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 

运行命令env时,我得到以下信息:

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$ 

当我检查变量时,我得到以下信息:

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$ 

我的问题是: 我在哪里犯了错误,为什么说Android SDK missing command tool,以及如何解决它”?

1 个答案:

答案 0 :(得分:0)

我发现问题是OpenJDK没有bin来按照Flutter的要求定义JAVA_HOME,因此我按照以下说明安装了java8并设置了JAVA_HOME { {3}}:

// 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

因此,我的$HOME/.bash_profile变成了:

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

然后我通过运行以下命令添加所需的Android licenses

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

然后我运行Flutter doctor,发现链条已正确设置:

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)

然后我创建了我的第一个应用程序:

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

然后按照here所述,按照以下命令创建avd:

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

在另一个终端

Hasans-Air:first_flutter h_ajsf$ flutter run

注意

您可能需要移动以下命令来将Modify flutter.gradle下的⁨flutter⁩ ▸ ⁨packages⁩ ▸ ⁨flutter_tools⁩ ▸ ⁨gradle⁩固定:

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

在顶部:

jcenter()

以便将文件.flutter/packages/flutter_tools/gradle/flutter.gradle更新为以下内容,如here所示:

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