./Development

Manifests, Namespaces and Flex Builder 2

I’ve been intrigued by the name spaces within Flex Builder and considering I add two to three at a time in most of my applications I was on the scout for a more efficient way in which to deal with them. I finally found time to have a hunt through the Flex directory on my Mac and over a conversation with a Simon Gregory, a fellow Flex 2 developer. I finally achieved my desire to reduce all of the namespaces and created one for the FlashGen.Com domain.

So how can you create one for yourself? It’s pretty easy once you know how and to be honest once you finish reading this you’ll be questioning why you hadn’t worked it out earlier.

The trick is that the URL is nothing more that a unique identifier – as anyone who has actually gone to http://www.adobe.com/2006/mxml will no doubt already have discovered there is nothing there (if memory serves you get their custom 404 page). So having established that there is “nothing to see here” in regards the the actual URL how does this relate to the namespace (xmlns:mx=”…”) and how can you make your own? Read on and you’ll find out..


All it relates to is a set of files called flex-config.xml and mxml-manifest.xml (in the case of Adobe and the mx name space). I’ve provided a snippet of both so you can see what they contain (if you really want to know more just have a sniff through your Flex SDK folder (search or spotlight will find them easily). DO NOT EDIT THESE. If you do you are likely to stuff up your Flex installation. So make copies of each of them and play around with those instead.

This is the flex-config.xml, cut down to show what we’re interested in (most of it you can ignore as we’re not interested in it beyond knowing it’s there).

Lets start with the flex-config.xml file. The default one that comes with Flex Builder 2 / 3 provides all of the compiler information that is needed to create your SWF’s via Flex Builder, it does a few other things but we are going to concentrate on specific elements enclosed within the <compiler></compiler> tags in this file, primarily <namespaces></namespaces>. If you look at the section I’ve extracted below you’ll see that I’ve started with the namespaces first. Now all this does is tell this config file what manifest file it needs to use (we’ll come on to the manifest file in a moment).

You’ll notice we define our URI base namespace in the <uri></uri> tag and our manifest in, you guessed it, the <manifest></manifest> tag. These are in turn enclosed within an individual <namespace></namespace> tag. This allows us to define a selection of namespaces and include them within the <namespaces></namespaces> tag for use by the compiler that way we can utilize more than one selection of code libraries within a single application. Well leave the flex-config file there for a bit and have a look at the manifest file now but don’t close this file as we’ll need to make a quick alteration at the end. You can pretty much delete everything else from the flex-config.xml file as you don’t need it.

The default manifest file for Flex Builder is called mxml-manifest.xml and it is very similar (read identical) to the manifest files created for packaging up SWC’s. It contains a single <componentPackage></componentPackage> tag that encloses all of the class descriptors for that namespace. i.e. When you create an app that uses a DataGrid in the MXML the compiler looks at this file to resolve what class that actually relates to and imports it. Below is a snapshot of part of the mxml-manifest.xml file.

That’s all we need to know about the manifest (see what I mean by how easy it is). If you haven’t already (and you really should have), make copies of both flex-config.mxml and mxml-manifest.xml and open them up in your favourite text editor. Right, now we have the copies let’s do a bit of editing. First lets make our namespace. locate the <namespaces></namespaces> tags in your flex-config.xml copy and change the URI to your own URL – if you have one, if not you can use mine.

Whatever comes after the actual domain is purely subjective so feel free to use anything (except special characters etc as they won’t work correctly). Don’t feel you have to copy mine verbatim, however I would suggest keeping the year in there as it helps identify when you may have created this namespace, and again years are unique (so I’m told :p). The FGXML part is just illustrative and as I already mentioned, it can be whatever word or additional “folder” structure you wish to use.

Save the file (we’ll come to the actual file name in a bit), Now open up the mxml-manifest.xml copy you made. This is the really labourious bit of the process, you now need to go through your entire package structure and add in the id and the fully qualified path for each class in the same format as above. Now I know I said it was simple, but I didn’t guarantee it would be easy :p. Don’t worry though I have something to take the edge of this whole process.

OK, don’t worry about doing the whole of the manifest now just add in a couple of your components and their class paths and save the file (delete the rest of the content of the mxml-manifest.xml file so you are left with something like this:

I’ve used the class names MyComponent1 and MyComponent2 for illustrative purposes you would obviously insert your actual class names.

So we have our copied flex-config.xml and mxml-manifest.xml files, How do we go about using them in our Flex projects? This is the really easy part, firstly we need to rename the mxml-manifest.xml file. For this example we’ll rename it to flashgen-manifest.xml. Now, you should still have the flex-config.xml open, if not open it again and locate the <manifest></manifest> tags, change the entry to read flashgen-manifest.xml instead of mxml-manifest.xml. Save and close the file. The final bit is to import them into the source root of your specific Flex project in Flex Builder 2 and rename the flex-config.xml to that of your default application-config.xml file (this is usually the same name as your project). So if your application file was called MyApplication.mxml we would name our flex-config.xml to MyApplication-config.xml.

The reason for doing this is so that the compiler picks it up and uses it in relation to that application.

Right thats it all set up to actually use it we just need to wire it into our MXML files like the example below:

Ok so that’s it, now a few things to be aware of – these are compile time settings so don’t expect the code hinting to work (I’m currently looking into how this works and will hopefully have an answer shortly). Also I mentioned that I had a solution to the whole manifest creation issue and having to go through all of the packages and adding them. I wrote a shell script and a selection of Ant tasks to create these files (currently these will only work on Mac and *nix systems and don’t actually need to be used with Flex Builder. I’m looking at creating a Windows version as well so watch this space.

I’m a bit tied for time this morning (plus I need to clean up the scripts for distribution), but I will have them up after lunch – 1pm-ish BST

Discussion

5 comments for “Manifests, Namespaces and Flex Builder 2”

  1. This is a great overview. I tried applying this but I was not able to complete the final step, namely to replace default application-config.xml with my new config files.

    What I’m trying to do is to customize the layout of the RichTextEditor control, but just for one section of my site. I copied the RichTextEditor.mxml file in my SDK directory and named the copy RichTextEditorLite.mxml. I then added the following to my flex-config.xml file:

    http://www.adobe.com/2006/mxml
    mxml-manifest.xml

    http://www.mydomain.com/2007/mxml
    mxml-manifest_mydomain.xml

    inside of mxml-manifest_mydomain.xml i entered:

    and finally, inside of my MXML application I added:

    xmlns:mx=”http://www.mydomain.com/2007/mxml”

    and

    Posted by td | August 15, 2007, 01:12
  2. Hi,

    Thanks for ur post, helped me tons. I’m interested to hear if you have figured out a way to get flex builder to code hint the new namespace?

    Cheers

    Posted by Chris | July 10, 2008, 15:07
  3. Hi Chris, no I haven’t yet. I’d let it slide for a bit, however I will put it back on my “things to find out” list. So hopefully I will have an update in the future :)

    Posted by FlashGen | July 23, 2008, 12:45
  4. Hi –

    Thank you for the article. This is good. However, I am trying to use compc command for the namespace file..following is my code and I keep getting the “Error: unknown namespace ‘http://www.csun.com/2008′” error..please help

    http://www.csun.com/2008

    Posted by csun | August 14, 2008, 22:30
  5. Author,

    I am trying applying the namespace to the application (not the library). So its similar to you. My question is do I need to have a flex-config.xml equivalent in the project or can I use compiler option -namespace to point to my local manifest file.

    The problem is I try that and FB outputs compiler error ‘could not resolve to a component implementation.”

    Basically I am wondering if there is any option without bringing the entire flex-config.xml into my workspace.

    Posted by md | October 10, 2008, 21:01

Post a comment