Bug 20084 - Error executing task BuildApk: Out of memory
Summary: Error executing task BuildApk: Out of memory
Alias: None
Product: Android
Classification: Xamarin
Component: MSBuild ()
Version: 4.12.3
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: 4.16.0
Assignee: dean.ellis
Depends on:
Reported: 2014-05-28 04:45 UTC by renan jegouzo
Modified: 2014-08-29 11:39 UTC (History)
6 users (show)

Is this bug a regression?: ---
Last known good build:

build log (486.47 KB, text/plain)
2014-05-28 09:44 UTC, renan jegouzo
project file (902.42 KB, application/octet-stream)
2014-05-28 12:24 UTC, renan jegouzo

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description renan jegouzo 2014-05-28 04:45:15 UTC
I'm building a project with 1.5Mb of assets, and I got this error at build time: "Error executing task BuildApk: Out of memory", Is there a trick to fix it ?
Comment 1 renan jegouzo 2014-05-28 09:44:36 UTC
Created attachment 6889 [details]
build log

I've attached the build log.
Comment 2 Danish Akhtar 2014-05-28 11:04:11 UTC
I have tried to reproduce this issue with:
XS 4.2.5(build 0)
X.Android 4.12.3
ML 10.8.5

I have created Android application in XS and add a text file of size 2mb in Assets folder and able to build application without any error.

Could you please attached your project and XS info? via Xamarin studio->About Xamarin studio->show details->copy the info and paste it.
Comment 3 renan jegouzo 2014-05-28 11:12:13 UTC
=== Xamarin Studio ===

Version 4.2.5 (build 0)
Installation UUID: 2a46676f-5eb8-4569-9aa7-630cd15bdfc2
	Mono 3.2.6 ((no/9b58377)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 302060000

=== Apple Developer Tools ===

Xcode 5.1.1 (5085)
Build 5B1008

=== Xamarin.iOS ===

Version: (Business Edition)
Hash: db4427f
Build date: 2014-04-22 12:49:14-0400

=== Xamarin.Android ===

Version: 4.12.3 (Business Edition)
Android SDK: /Users/renanyoy/android sdk
	Supported Android versions:
		2.3   (API level 10)
		4.0   (API level 14)
		4.0.3 (API level 15)
		4.1   (API level 16)
		4.2   (API level 17)
		4.3   (API level 18)
		4.4   (API level 19)
Java SDK: /usr
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)

=== Xamarin.Mac ===

Xamarin.Mac: Not Installed

=== Build Information ===

Release ID: 402050000
Git revision: 95ac543f224a70960a9ab71e24ce341eb87de06f
Build date: 2014-05-02 13:05:15-04
Xamarin addins: b81285ac0156281956135adb96685f98922893a1

=== Operating System ===

Mac OS X 10.9.3
Darwin macbook.home 13.2.0 Darwin Kernel Version 13.2.0
    Thu Apr 17 23:03:13 PDT 2014
    root:xnu-2422.100.13~1/RELEASE_X86_64 x86_64
Comment 4 renan jegouzo 2014-05-28 11:41:59 UTC
I'm updating with the new rollout stable channel version, I will post again all infos, and the project file.
Comment 5 renan jegouzo 2014-05-28 12:24:50 UTC
Created attachment 6892 [details]
project file

same problem with the last today stable version (osx).
also, I get the same problem on windows with visual studio 2012.

Xamarin Studio
Version 5.0 (build 878)
Installation UUID: 2a46676f-5eb8-4569-9aa7-630cd15bdfc2
	Mono 3.4.0 ((no/c3fc3ba)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 304000204

Apple Developer Tools
Xcode 5.1.1 (5085)
Build 5B1008

Xamarin.Mac: Not Installed

Version: (Business Edition)
Hash: fc6f56b
Build date: 2014-05-19 19:10:29-0400

Version: 4.12.4 (Business Edition)
Android SDK: /Users/renanyoy/android sdk
	Supported Android versions:
		2.3   (API level 10)
		4.0   (API level 14)
		4.0.3 (API level 15)
		4.1   (API level 16)
		4.2   (API level 17)
		4.3   (API level 18)
		4.4   (API level 19)
Java SDK: /usr
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)

Build Information
Release ID: 500000878
Git revision: bcd66920d963483e7d638a2339c4022fe035b529
Build date: 2014-05-27 17:36:26-04
Xamarin addins: da9064ce55b0fa90930a7c437a4cc1ae0e5c778c

Operating System
Mac OS X 10.9.3
Darwin macbook.home 13.2.0 Darwin Kernel Version 13.2.0
    Thu Apr 17 23:03:13 PDT 2014
    root:xnu-2422.100.13~1/RELEASE_X86_64 x86_64
Comment 6 renan jegouzo 2014-05-28 12:42:04 UTC
for info, the project build just fine, it's when I try to deploy it (apk build) that I get this error.
Comment 7 renan jegouzo 2014-05-28 23:42:45 UTC
here the error (from the build log)

Task "BuildApk"
		Using task BuildApk from Xamarin.Android.Tasks.BuildApk, Xamarin.Android.Build.Tasks, Version=, Culture=neutral, PublicKeyToken=null
		BuildApk Task
		  ApkInputPath: obj/Debug/android/bin/packaged_resources
		  ApkOutputPath: obj/Debug/android/bin/resources_and_assemblies
		  DalvikClasses: obj/Debug/android/bin/classes.dex
		  SupportedAbis: armeabi;armeabi-v7a;x86
		  UseSharedRuntime: true
		  Debug: True
		  EmbedAssemblies: False
/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Common.targets: error : Error executing task BuildApk: Out of memory
		Error executing task BuildApk: System.OutOfMemoryException: Out of memory
  at (wrapper managed-to-native) object:__icall_wrapper_mono_gc_alloc_vector (intptr,intptr,intptr)
  at (wrapper alloc) object:AllocVector (intptr,intptr)
  at System.IO.MemoryStream.set_Capacity (Int32 value) [0x00000] in <filename unknown>:0 
  at System.IO.MemoryStream.Expand (Int32 newSize) [0x00000] in <filename unknown>:0 
  at System.IO.MemoryStream.Write (System.Byte[] buffer, Int32 offset, Int32 count) [0x00000] in <filename unknown>:0 
  at Ionic.Zip.CountingStream.Write (System.Byte[] buffer, Int32 offset, Int32 count) [0x00000] in <filename unknown>:0 
  at Ionic.Zip.ZipEntry.CopyThroughWithNoChange (System.IO.Stream outstream) [0x00000] in <filename unknown>:0 
  at Ionic.Zip.ZipEntry.CopyThroughOneEntry (System.IO.Stream outstream) [0x00000] in <filename unknown>:0 
  at Ionic.Zip.ZipEntry.Write (System.IO.Stream s) [0x00000] in <filename unknown>:0 
  at Ionic.Zip.ZipFile.Save () [0x00000] in <filename unknown>:0 
  at Ionic.Zip.ZipFile.Save (System.IO.Stream outputStream) [0x00000] in <filename unknown>:0 
  at Xamarin.Android.Tasks.BuildApk.Execute () [0x00000] in <filename unknown>:0 
  at Microsoft.Build.BuildEngine.TaskEngine.Execute () [0x00000] in <filename unknown>:0 
  at Microsoft.Build.BuildEngine.BuildTask.Execute () [0x00000] in <filename unknown>:0 
	Task "BuildApk" execution -- FAILED
	Done building target "_BuildApkFastDev" in project "/Users/renanyoy/git/aestesis-mono/auth/chaoDroid/chaoDroid.csproj".-- FAILED
Done building project "/Users/renanyoy/git/aestesis-mono/auth/chaoDroid/chaoDroid.csproj".-- FAILED
Comment 8 renan jegouzo 2014-05-28 23:49:52 UTC
idk why you zip all in memory...
Comment 9 renan jegouzo 2014-05-28 23:51:09 UTC
the google play market fix the max apk size at 4GB
Comment 10 renan jegouzo 2014-06-01 06:18:36 UTC
any fixes ?
Comment 11 renan jegouzo 2014-06-04 01:16:41 UTC
I need to release this app at the end of this week, I'm stuck for now..
Comment 12 dean.ellis 2014-06-05 05:48:52 UTC
hi you can work around this by not including the assets in the project for now and simply add them manually once you've build the apk using 

aapt add your.apk assets/someasset.foo

you will also need to manually sign and align the .apk after you have added your assets. see http://developer.android.com/tools/publishing/app-signing.html for details on that.

We probably do the zip in memory for speed reasons, but we'll look at reworking this to get round this issue. However if you have a large number of assets (say for a game) I would suggest not adding them to the project as it slows down the build/debug times. I wrote a block post on the subject 

Comment 13 renan jegouzo 2014-06-05 07:09:25 UTC
a faster way than zipping in memory should be to keep the last built apk and only update modified files, so xamarin could deals with large apk really fast.
Comment 14 renan jegouzo 2014-06-05 07:10:41 UTC
I will do my project in java for now
Comment 15 renan jegouzo 2014-06-05 07:16:46 UTC
also zipping in memory is a really bad optimization, as hard drive have a cache disk developed by specialized developers in cache and optimizations, and you try to avoid them...
Comment 16 renan jegouzo 2014-07-23 03:10:48 UTC
dean, how I can add the zipped assets in debug mode ?
Comment 17 renan jegouzo 2014-07-23 03:16:59 UTC
I mean, where I can add "aapt add your.apk assets/someasset.foo" in xamarin studio, so I can launch and debug the app using xamarin studio.
Comment 18 dean.ellis 2014-07-23 09:55:40 UTC
We do have a fix for this issue that is awaiting code review but it probably won't be out until 4.16 is released. 

In the meantime if anyone has a app with a large number of assets (i.e over a gig) you can use the following workaround.

For debugging you don't want to be including all of those assets in the apk. It will slow down the build/debug process. Each png will need to be squashed by the android aapt tool which can take an age to do and then added to the apk which is where you are currently hitting a memory error. 

So as per the blog post I linked, its probably best for debugging if you remove the assets from the project file and just zip them up into a zip file and manually copy them over to the device using the following command line

adb push <zipfile> /mnt/sdcard/Download/<zipfile>

This will get all of your assets onto the device you are testing on. Then you can use the AssetMgrExt I posted in the blog to open read the assets. You are probably doing something like

Assets.Open ("foo");

in your app to read the asset and then load the shader/bitmap etc. With the AssetMgrExt class in your project you will just need to change it to 


and then call 

AssetMgrExt.Initialize ("/mnt/sdcard/Download/<zipfile>");

in the OnCreate of your app (or somewhere before you load the data) then the app will load the assets from the zip not the android package. The bonus point for this is fast debug times, the assets will be on the device already so only your app will be deployed. Also if you do change your assets you can just update the zip and copy it to the device. 

Don't forget to call the AssetMgrExt .Close method when you no longer need the zip file open.

Doing a release to the play store if you have allot of assets can be tricky since Google Play does have a limit of 50 meg for the actual .apk file but up to 4 gig if you use an expansion file(s) (.obb) 


The good news on this is that the AssetMgrExt class from the article can open an expansion file easily as its just a zip-aligned zip file. it just needs the right path to the file which can be calculated using something like

ApplicationInfo appInfo = ApplicationInfo;
			if (Android.OS.Environment.ExternalStorageState.Equals (Android.OS.Environment.MediaMounted)) {
				string obbDirectory = Android.OS.Environment.ExternalStorageDirectory + "/Android/obb/" + appInfo.PackageName;
				if (Directory.Exists (obbDirectory)) {
					var obbFiles = Directory.GetFiles (string.Format ("main.*.{0}.obb", appInfo.PackageName));
					if (obbFiles.Length > 0) {
						var obbFile = obbFiles [0];

There is however a C# port of the Android Expansion Library to help manage expansions files available at 


This will help you download the .obb files if they are not downloaded with the app (which they might not be).
Comment 19 dean.ellis 2014-07-23 09:59:08 UTC
oops the Directory.GetFiles call should have been 

var obbFiles = Directory.GetFiles (Path.Combine(obbDirectory, string.Format ("main.*.{0}.obb", appInfo.PackageName)));
As it needs to look in the obbDirectory
Comment 20 renan jegouzo 2014-07-24 06:56:47 UTC
thanks deans, 

I didn't understand all with the first posts,
it's clear now.
Comment 21 dean.ellis 2014-07-31 12:00:45 UTC
Fixed in monodroid/master/6119554 

Should make it into the 4.16 release
Comment 23 dean.ellis 2014-08-22 03:35:16 UTC
The only way I can think of to test this out is to do the following

1) Disable the Page File on your windows machine. 
2) Create a project with Assets who's combined size > the total amount of memory on the machine. 
3) Try to create a .apk

In theory because the old code was using a memory stream to build the .apk it should error out as soon as the total free memory is exceeded. The new code uses a temp file so that should not have the same issue.
Comment 24 renan jegouzo 2014-08-22 04:51:48 UTC
you don't need to go so far, the old code used an old int32 api that clash at 2giga, and usually will all the redundancy in the code at 600mo, it's over..
Comment 25 Mohit Kheterpal 2014-08-29 11:39:04 UTC
Today I have tried to verify this issue on VM memory 4GB. Then I create a folder of images size near about 4GB. After adding this folder in Android template project in asset folder, I have click on Create Android package then I am getting following build error :

Please let me know what should I do to remove this error? So that I can verify this issue.