By: scott July 15th, 2011
I was developing an application that needs to be ported to both the web and mobile. This is usually rather easy to do since the release of the Flex 4.5.1 SDK (As of this writing I am using FDT 126.96.36.1998). So I was creating the application as AS3 only (no Flex framework). I have no problem compiling to both web and mobile from the same project. As I am developing the application I realize that using Flex would make development substantially easier because I am having to roll my own components which already exist in Flex. So I figure I could easily convert the project type to a Flex mobile one and viola, I have access to the swc files for the Flex framework so I can use existing components without having to reinvent the wheel. This was not quite the case.
The application works fine when publishing for mobile. But it turns out that when I publish for the web I get the following runtime error:
ReferenceError: Error #1065: Variable flash.events::StageOrientationEvent is not defined.
This makes sense because this class appears to be located in the airglobal.swc file. This is only going to be available when compiling to AIR which is not what I want when trying to compile for the web. So I tried to change the project type to a regular Flex 4 project rather than Flex Mobile one. But I still want to use the Flex SDK 4.5.1. When I try this I see this warning:
Some classpath libraries are missing or SDK version is not compatible.
And of course the flex.swc and a couple of other swc files appeared to be missing. After doing a little searching around I found this jira ticket: FDT-2214 Warning on Flex 4.5.1 SDK (no flex.swc). In the comments for this ticket there is a link to Backwards Compatibility for Flex 4.5. Also Philipp Arnolds posts a link to this video which describes how the flex.swc file is now called the core.swc. So I removed the missing swc files and added the core.swc like in the video and I am able to compile for the web.
But now I have the problem of any code related to AIR has to be commented out. In my case I am making reference to the flash.desktop.NativeApplication class. So now my workflow is rather ugly. I have to switch the project type and then comment/uncomment the code which is specific to mobile anytime I need to compile for web/mobile. I guess it’s not bad right now since the NativeApplication references are all in once place. It only takes less than 20 seconds to switch between mobile and web. But I bet this could get really ugly as the project grows. Maybe I was better off just doing it in AS3.
After doing some more reading, I ran across this article on Mobile Development. Under Managing Mobile Projects it says that it’s typical to have 3 separate projects when creating applications for multiple screens. One for the desktop/web UI, one for mobile UI, and a Library project for the model and data access code which is shared by both UI projects. This makes much more sense although I don’t know if I would call that code once deploy many.