---
title: "使用ant打包android程序遇到OutOfMemoryError"
author: "chen592969029"
date: "2011-02-16T15:04:11Z"
tags: ["Android", "Ant", "Java", "脚本"]
categories: ["编程语言"]
slug: "ant-android-outofmemoryerror"
draft: false
lastmod: "2011-03-08T18:52:34Z"
id: 907899
url: "https://chen592969029.iteye.com/blog/907899"
---

[**问题描述：**]()

 

一般在编译大量 Java 源文件时出现，显示为：

 

```
java.lang.OutOfMemoryError:Java heap space 
```

 

**解决方法：**

在系统环境变量中添加 :

   

```
Variable name:ANT_OPTS 

Variable value:-Xms256M -Xmx512m 
```

 

如果这样还没有解决的话，则：

找到你在 ant 脚本中使用的 dx 文件（一般在 Android SDK 的 platforms 目录下），添加

javaOpts="-Xmx256M" ，数值可以更大一些。

打开 dx.bat 文件找到 set javaOpts=     ，默认是没有设置的，改成

   

```
set javaOpts="-Xmx256M"   
```

 

在这句的下面看到了几句注释（ android 1.5 版本）：

    REM If you want DX to have more memory when executing, uncomment the 

    REM following line and adjust the value accordingly. Use "java -X" for 

    REM a list of options you can pass here. 

    REM 

 

    REM set javaOpts=-Xmx256M

意思说的就是：如果你想你的 DX 执行的时候拥有更大的内存，就对下面的这句代码（也就是 set javaOpts=-Xmx256M ）取消注释，相应地调整数值，使用“ Java — X ”可以看到一系列的选项，可以在这里使用。