Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

安卓自动化之旅(三)——尝试使用Docker环境来构建APK 上 #68

Open
soapgu opened this issue Aug 4, 2021 · 0 comments
Labels
安卓 安卓

Comments

@soapgu
Copy link
Owner

soapgu commented Aug 4, 2021

  • 起因

    原因是在上一篇里面,在coding自动化构建尝试升级Gradle版本到7,彻底失败。由于腾讯的主机把http的源配置进去了。然后在Gradle初始化环境的时候直接报错。
    后续发工单找客户询问,经过几轮答非所问以后,客服建议用Docker来构建。不得不说起码最后一条还是一条很有启发性的建议。
    图片

  • 过程

  1. 装Docker
    首先我需要在本机安装Docker。过程不表。很久没玩了,windows docker的图形化界面做得好多了
    图片

  2. 找Docker基础镜像
    gradle的环境需要是JAVA的环境。gradle wrapper是会自动安装的
    所以从JRE镜像着手。
    先本机查看 JAVA -version
    openjdk:11.0.10-jdk-buster
    图片
    找了一个和本机版本一致的

  3. 写一个安卓程序
    DockerBuildApk
    一个空的app,使用gradle版本7.0.2

gradle-wrapper.properties

#Wed Aug 04 14:45:37 CST 2021
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
  1. Build Docker
    为了保证代码文件时干净的,我用git clone后的文件夹作为Build根目录。不再用Android Studio打开

Dockerfile

FROM openjdk:11.0.10-jre-buster AS base

RUN mkdir /andriod
WORKDIR /andriod
ADD . /andriod
RUN chmod +x ./gradlew

docker build -t soapgu/androiddockertest:v1.0 .

PS D:\WorkSpace\PlayPen\DockerBuildApk> docker build -t soapgu/androiddockertest:v1.0 .
[+] Building 0.6s (10/10) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                                                                        0.0s
 => => transferring dockerfile: 32B                                                                                                                                                                         0.0s
 => [internal] load .dockerignore                                                                                                                                                                           0.0s
 => => transferring context: 2B                                                                                                                                                                             0.0s
 => [internal] load metadata for docker.io/library/openjdk:11.0.10-jre-buster                                                                                                                               0.0s
 => [1/5] FROM docker.io/library/openjdk:11.0.10-jre-buster                                                                                                                                                 0.0s
 => [internal] load build context                                                                                                                                                                           0.0s
 => => transferring context: 11.35kB                                                                                                                                                                        0.0s
 => CACHED [2/5] RUN mkdir /andriod                                                                                                                                                                         0.0s
 => CACHED [3/5] WORKDIR /andriod                                                                                                                                                                           0.0s
 => [4/5] ADD . /andriod                                                                                                                                                                                    0.0s
 => [5/5] RUN chmod +x ./gradlew                                                                                                                                                                            0.4s
 => exporting to image                                                                                                                                                                                      0.1s
 => => exporting layers                                                                                                                                                                                     0.0s
 => => writing image sha256:37b397ebe834cbbabcdb5db01fbc26c90eb35ede9d16f5201b7c718ad3ece7f9                                                                                                                0.0s
 => => naming to docker.io/soapgu/androiddockertest:v1.0

  1. Run Gradle
    先小试牛刀,跑个gradle version试试
PS D:\WorkSpace\PlayPen\DockerBuildApk> docker run -it  soapgu/androiddockertest:v1.0 ./gradlew -version
/usr/bin/env: ‘sh\r’: No such file or directory

是吧了,这个报错是什么鬼

PS D:\WorkSpace\PlayPen\DockerBuildApk> docker run -it  soapgu/androiddockertest:v1.0 ls /andriod
Dockerfile  build.gradle  gradle.properties  gradlew.bat
app         gradle        gradlew            settings.gradle

问题文件确实存在的
还是StackOverflow比较厉害

Error with gradlew: /usr/bin/env: bash: No such file or directory

The problem's cause was that Git on Windows converted the line endings of gradlew from Unix style (LF) to Windows style (CRLF).

图片
用notepad++转

PS D:\WorkSpace\PlayPen\DockerBuildApk> docker run -it  soapgu/androiddockertest:v1.0 ./gradlew -version
Downloading https://services.gradle.org/distributions/gradle-7.0.2-bin.zip
..........10%...........20%...........30%..........40%...........50%...........60%..........70%...........80%...........90%..........100%

Welcome to Gradle 7.0.2!

Here are the highlights of this release:
 - File system watching enabled by default
 - Support for running with and building Java 16 projects
 - Native support for Apple Silicon processors
 - Dependency catalog feature preview

For more details see https://docs.gradle.org/7.0.2/release-notes.html


------------------------------------------------------------
Gradle 7.0.2
------------------------------------------------------------

Build time:   2021-05-14 12:02:31 UTC
Revision:     1ef1b260d39daacbf9357f9d8594a8a743e2152e

Kotlin:       1.4.31
Groovy:       3.0.7
Ant:          Apache Ant(TM) version 1.10.9 compiled on September 27 2020
JVM:          11.0.10 (Oracle Corporation 11.0.10+9)
OS:           Linux 5.10.16.3-microsoft-standard-WSL2 amd64


成功

  • 构建APK

  • docker run -it -v "D:\dockerbuilds:/andriod/app/build" soapgu/androiddockertest:v1.0 ./gradlew assembleDebug

PS D:\WorkSpace\PlayPen\DockerBuildApk> docker run -it -v "D:\dockerbuilds:/andriod/app/build" soapgu/androiddockertest:v1.0 ./gradlew assembleDebug
Downloading https://services.gradle.org/distributions/gradle-7.0.2-bin.zip
..........10%...........20%...........30%..........40%...........50%...........60%..........70%...........80%...........90%..........100%

Welcome to Gradle 7.0.2!

Here are the highlights of this release:
 - File system watching enabled by default
 - Support for running with and building Java 16 projects
 - Native support for Apple Silicon processors
 - Dependency catalog feature preview

For more details see https://docs.gradle.org/7.0.2/release-notes.html

Starting a Gradle Daemon (subsequent builds will be faster)

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine the dependencies of task ':app:compileDebugJavaWithJavac'.
> SDK location not found. Define location with an ANDROID_SDK_ROOT environment variable or by setting the sdk.dir path in your project's local properties file at '/andriod/local.properties'.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/7.0.2/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 4m 34s

本来想一鼓作气的。结果还是太乐观,Gradle在下完依赖后就构建失败了,缺了Android的SDK环境。
以前都是装Android Studio自动全配好的没有关注这一块(IDE也害人啊,离开就是残废啊)。Anyway,开局还行。
Dockfile还要重做,下一篇 我们来解决Android编译环境的问题

@soapgu soapgu added the 安卓 安卓 label Aug 4, 2021
@soapgu soapgu changed the title 安卓自动化之旅——尝试使用Docker环境来构建APK 上 安卓自动化之旅(三)——尝试使用Docker环境来构建APK 上 Aug 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
安卓 安卓
Projects
None yet
Development

No branches or pull requests

1 participant