the Flagship of independent news, reviews and resources for Developers and Users of Samsung's mobile platform Bada
Apps & Projects, Bada Tutorials, BadaDev Projects, Fundamentals

Components development for bada (Part 2)

[ This is the second part of this bada tutorial . Also keep in mind this post at our bada forums]

Reusable components make the development of applications faster, more flexible, less buggy and more fun :-) . If you wanted to reuse some of your code you could, of course, simply put all your classes into separate files and copy-paste those on-demand to your new projects. That approach, however, would make the maintenance of the shared code very difficult. Imagine the scenario where you find a bug in one of your components and have to modify the same piece of code across a dozen projects. In extreme cases such a job would be barely doable.

That is why you need a library. Putting pre-compiled code into a library and link this library whenever needed from other projects has several advantages:

  1. If you have to modify the code, you do it just once
  2. The code is already pre-compiled, which means that you do not have to compile it again – saving time
  3. You can share (sell?) your library with other developers without releasing your source code.

The bada IDE offers a project of type Bada Static Library, meaning you can throw into it all the functionality you had in a normal application project… even the resources!

What I have created was a simple library for my re-usable components: Project BadaDevCC (BadaDev Common Components). Then I moved two classes from my recent project, the Simple Finger Drawing app, to the library project. These classes were the InheritanceContainer and the BrushPicker. As I said in the first part of this tutorial, these are the classes that I might want to use in other projects at a later date.

Next, and this is a very good idea once you start development of  libraries, I defined a new namespace called “badadev” for those classes. The reason for this is obvious: if I were to use the library in other projects, it is quiet possible that at some moment I would name another class “Brush”, too. So the namespace allows me a clear differentiation of the class for the years to come. You put the namespace statement around the classes declarations in the header files. E.g.

namespace badadev
{

class InheritanceContainer
{
// Stuff goes in here
};

}

Finally in the definitions of the classes,  the .cpp files, you have to simply put

using namespace badadev;

somewhere at the top. Compile/build the library.

You may have noticed that the generated library has the following naming convention: lib[Your Project Name].lib. (Note: as of IDE 1.0.0b1 RC2, the library is generated as an archive file lib[...].a)This one is quiet important to know once you want to use the library in other projects.  To do so in my SimpleDrawing project I need to go to its properties and:

  1. Define an additional directory where the compiler has to look for the header files of the library
  2. Tell the linker where to find the library (name and directory)

Please note that the library name you have to input is lib[Your Project Name] , without the extension (as of IDE/SDK 1.0.0b RC2 also without the “lib” prefix). Once this is done you simply have to make some minor corrections in the rest of the code due to the new namespace you defined in the library. Basically this means that you have to:

  1. for example, use “badadev::Brush” instead of “Brush” in the header
  2. make a “using namespace” statement in the corresponding .cpp files
using namespace badadev;

Compile the new project and you are good to go! One important thing to know is this: the objects of the static library are directly integrated into the executable file – therefore you will not see any additional files in the output directory.

I will release shortly the new, improved :-) , version of the Simple Finger Drawing project (1.0.2) .

And so, I wish you good luck developing! Share the goods (libs) with us! :-D

Related posts:

  1. Components development for bada (Part 3)
  2. Components development for bada (Part 1)
  3. Bada Development- How to Start?
  1. 4 Responses to “Components development for bada (Part 2)”

  2. By Steve on Jan 8, 2010 | Reply

    Thanks for doing these tutorials wit, I’m finding them very helpful.

  3. By Florian on Jan 8, 2010 | Reply

    True, keep up the good work, wit! This website is more fun than the official one lol

  4. By wit on Jan 9, 2010 | Reply

    Thank you for your nice words, guys! I hope bada will be worth the work involved and the posts were not completely in vain!

    I enjoy discovering new technologies and I will continue what I have been doing, unless Samsung doesn’t release the SDK/IDE soon hehe.

    Perhaps you may want to join the forums as well? ;-)

  1. 1 Trackback(s)

  2. Jan 25, 2010: bada Dev » Blog Archive » Components development for bada (Part 3)

Post a Comment

Editor's picks

Copyright 2009-2010 BadaDev.com (unless otherwise stated). All rights reserved! Powered by Wordpress!