編譯環(huán)境
- 本文是基于AndroidStudio2.3.3,其實(shí)在3.0+上的編譯大同小異,錯(cuò)誤也是基本一致
配置
應(yīng)為AndroidStudio打包是基于Gradle命令的,所以這樣的話,如果在命令行中使用打包命令就要先配置path環(huán)境
- 首先配置環(huán)境在path里面添加:D:\AndroidStudio\gradle\gradle-3.2\bin,根據(jù)自己的Android Studio路徑來進(jìn)行配置
- 配置完成后在命令行中輸入:gradle
image.png
如果出現(xiàn)上面的信息就代表成功了,那么我們就可以進(jìn)入下面的步驟
打包命令
-
很簡(jiǎn)單,進(jìn)入項(xiàng)目的路徑下
image.png 然后執(zhí)行: gradle build
錯(cuò)誤出現(xiàn)1
Unable to start the daemon process.
This problem might be caused by incorrect configuration of the daemon.
For example, an unrecognized jvm option is used.
Please refer to the user guide chapter on the daemon at https://docs.gradle.org/
3.2/userguide/gradle_daemon.html
Please read the following process output to find out more:
-----------------------
Error occurred during initialization of VM
Could not reserve enough space for 1572864KB object heap

image.png
- 執(zhí)行后出現(xiàn)上面的錯(cuò)誤,當(dāng)然了并不是所有的都會(huì)出現(xiàn)上面的問題,可能就是少量的會(huì)出現(xiàn)
錯(cuò)誤的原因應(yīng)該是:Android Studio沒有足夠空間啟動(dòng)JVM
解決方法
第一種
定位到目錄 C:\Users\Administrator.gradle
創(chuàng)建文件gradle.properties
org.gradle.jvmargs=-Xmx512m
重啟你的Android Studio項(xiàng)目
第二種
在當(dāng)前工程下,修改gradle.properties里面:org.gradle.jvmargs=-Xmx512
錯(cuò)誤出現(xiàn)2
A problem occurred evaluating project ':clone'.
> Failed to apply plugin [id 'com.android.application']
> Minimum supported Gradle version is 3.3. Current version is 3.2. If using t
he gradle wrapper, try editing the distributionUrl in D:\ASDemo\learn\clone\grad
le\wrapper\gradle-wrapper.properties to gradle-3.3-all.zip
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug
option to get more log output.

image.png
- 如果上面的步驟沒有問題了,還可能出現(xiàn)這樣的錯(cuò)誤
- 為什么會(huì)出現(xiàn)這樣的問題呢,如果是直接點(diǎn)擊綠色三角運(yùn)行的話可能不會(huì)出現(xiàn)這樣的問題,因?yàn)楝F(xiàn)在在命令行下面,所對(duì)應(yīng)的gradle為當(dāng)前工程下gradle/wrapper/gradle-wrapper.properties 中對(duì)應(yīng)的gradle版本
distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
- 這樣的話,會(huì)和當(dāng)前項(xiàng)目build.gradle下面不一致
classpath 'com.android.tools.build:gradle:2.3.3'
- 下面我們來看看版本對(duì)應(yīng):
| Plugin version | Required Gradle version |
|---|---|
| 1.0.0 - 1.1.3 | 2.2.1 - 2.3 |
| 1.2.0 - 1.3.1 | 2.2.1 - 2.9 |
| 1.5.0 | 2.2.1 - 2.13 |
| 2.0.0 - 2.1.2 | 2.10 - 2.13 |
| 2.1.3 - 2.2.3 | 2.14.1+ |
| 2.3.0+ | 3.3+ |
| 3.0.0+ | 4.1+ |
表格來源:https://developer.android.com/studio/releases/gradle-plugin.html#updating-gradle
- 修改成對(duì)應(yīng)的版本就ok ,就可以執(zhí)行命令行了,目前我發(fā)現(xiàn)的是這個(gè)兩個(gè)錯(cuò)誤。出現(xiàn)別的錯(cuò)誤后續(xù)添加進(jìn)去

