Docker flutter镜像自动部署实践,踩坑记录

前言

本项目主要参考cirruslabs的两个Docker脚本

最简单实践案例

指定SDK版本

Dockerfile

  FROM cirrusci/android-sdk:tools

  ENV ANDROID_PLATFORM_VERSION 29
  ENV ANDROID_BUILD_TOOLS_VERSION 29.0.2

  RUN yes | sdkmanager \
      "platforms;android-$ANDROID_PLATFORM_VERSION" \
      "build-tools;$ANDROID_BUILD_TOOLS_VERSION"

编译镜像,和上面Dockerfile同级目录执行

  docker build -t "image_name" . 
  • -t 指定镜像名称;
  • . 当前目录找Dockerfile 编译文件

如果需要多个SDK的话,可以在上次编译镜像的基础上叠一层,修改Dockfile

建议不要叠太多SDK,否则最终的镜像会很大,而且正式环境是不需要多个SDK的

Dockerfile

  FROM <上次编译的镜像名称或者id>

  ENV ANDROID_PLATFORM_VERSION 29
  ENV ANDROID_BUILD_TOOLS_VERSION 29.0.2

  RUN yes | sdkmanager \
      "platforms;android-$ANDROID_PLATFORM_VERSION" \
      "build-tools;$ANDROID_BUILD_TOOLS_VERSION"

备注: 这里提供自己编译好的SDK版本,本人比较懒提供常用编译好的SDK

dockerlinz/android-sdk:29

dockerlinz/android-sdk:28

dockerlinz/android-sdk:27

指定flutter版本

Dockerfile

  FROM dockerlinz/android-sdk:27

  ARG flutter_version

  ENV FLUTTER_HOME=${HOME}/sdks/flutter \
      FLUTTER_ROOT=$FLUTTER_HOME \
      FLUTTER_VERSION=$flutter_version \
      PUB_HOSTED_URL=https://pub.flutter-io.cn \
      FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn

  RUN mkdir -p ${FLUTTER_HOME}

  ENV PATH ${PATH}:${FLUTTER_HOME}/bin:${FLUTTER_HOME}/bin/cache/dart-sdk/bin

  RUN git clone --branch ${FLUTTER_VERSION} https://github.com/flutter/flutter.git ${FLUTTER_HOME} && \
      flutter doctor

这里我们使用v1.9.1-hotfixes分支

  docker build --tag --build-arg flutter_version=v1.9.1-hotfixes

测试

进入flutter工程所在目录

  docker run --rm -it -v ${PWD}:/build --workdir /build dockerlinz/flutter:stable flutter build apk 

可以加入 -v ~/.gradle:/home/cirrus/.gradle –env GRADLE_USER_HOME=/home/cirrus/.gradle,挂在本地缓存到镜像中

注意:过来人经验,此处没有把握的话还是进入容器里面测试会更省事

  docker run --rm -it -v ${PWD}:/build --workdir /build dockerlinz/flutter:stable

进入后需要重新生成.packages

  fluter clean 
  flutter packages get 

最好进入android或mac目录执行以下原生编译脚本,查看编译环境

  ./gradlew build -d 

最后才是flutter编译

  flutter build apk 

出现的问题

问题一:本地正常跑,但是镜像中很多依赖下载不下来

  • android studio Could not download guava.jar

  • Could not download kotlin-gradle-plugin.jar

  • flutter Build file ‘/build/android/app/build.gradle’ line: 25

  • Could not download kotlin-reflect.jar org.jetbrains.kotlin:kotlin-reflect:1.2.0

    解决:方案

  1. 在gradle中增加阿里云国内镜像

    buildscript {
        repositories {
            maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
            google()
            jcenter()
        }
    
        dependencies {
            classpath 'com.android.tools.build:gradle:3.3.1'
        }
    }
    
    allprojects {
        repositories {
            maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
            google()
            jcenter()
        }
    }
  2. 挂载本地gradle 缓存到镜像中

    docker run --rm -it -v ${PWD}:/build --workdir /build  -v ~/.gradle:/home/cirrus/.gradle --env GRADLE_USER_HOME=/home/cirrus/.gradle  dockerlinz/flutter:stable

问题二: flutter build apk 缓慢

每次都下载gradle版本,导致每次重新下载gradle版本

解决方案一

挂在本地缓存到镜像中

   -v ~/.gradle:/home/cirrus/.gradle --env GRADLE_USER_HOME=/home/cirrus/.gradle

解决方案二

  1. 挂在.gradle 缓存目录,不用每次编译的时候重新下载依赖

    -v ~/.gradle2:/home/cirrus/.gradle
  2. 在叠一层gradle环境

    #https://github.com/frekele/docker-gradle/blob/master/Dockerfile
    FROM dockerlinz/flutter:1.9.1-hotfixes
    
    # Change to tmp folder
    ENV GRADLE_VERSION=4.10.2
    ENV GRADLE_HOME=/opt/gradle
    ENV GRADLE_FOLDER=/root/.gradle
    
    # Change to tmp folder
    WORKDIR /tmp
    
    # Download and extract gradle to opt folder
    RUN wget --no-check-certificate --no-cookies https://downloads.gradle.org/distributions/gradle-${GRADLE_VERSION}-bin.zip \
        && sudo unzip gradle-${GRADLE_VERSION}-bin.zip -d /opt \
        && sudo ln -s /opt/gradle-${GRADLE_VERSION} /opt/gradle \
        && sudo rm -f gradle-${GRADLE_VERSION}-bin.zip
    
    # Add executables to path
    RUN sudo update-alternatives --install "/usr/bin/gradle" "gradle" "/opt/gradle/bin/gradle" 1 && \
        sudo update-alternatives --set "gradle" "/opt/gradle/bin/gradle"
    
    # Create .gradle folder
    RUN sudo mkdir -p $GRADLE_FOLDER
    
    # Mark as volume
    VOLUME  $GRADLE_FOLDER
    
    # Add the files
    #ADD rootfs /
    
    # Change to root folder
    WORKDIR /home/cirrus/
  3. Flutter 中Android工程指定本地gradle编译,不是取下载,文件目录<android_project>/.idea/gradle.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project version="4">
      <component name="GradleSettings">
        <option name="linkedExternalProjectsSettings">
          <GradleProjectSettings>
            <compositeConfiguration>
              <compositeBuild compositeDefinitionSource="SCRIPT" />
            </compositeConfiguration>
            <option name="distributionType" value="LOCAL" />
            <option name="externalProjectPath" value="$PROJECT_DIR$" />
            <option name="gradleHome" value="$USER_HOME$/.gradle/wrapper/dists/gradle-4.4-all/9br9xq1tocpiv8o6njlyu5op1/gradle-4.4/bin" />
            <option name="resolveModulePerSourceSet" value="false" />
            <option name="testRunner" value="PLATFORM" />
          </GradleProjectSettings>
        </option>
      </component>
    </project>
    • distributionType 改为LOCAL
    • gradleHome 指向gradle_home/bin