Android Tutorial Library compilation - Compile your project to a library

Discussion in 'Tutorials & Examples' started by Erel, Jan 1, 2013.

  1. Erel

    Erel Administrator Staff Member Licensed User

    Starting from Basic4android v2.50 you can compile your project, or part of it to a regular library.

    Why should I compile a library?
    • Break large projects into several smaller (more maintainable) projects.
    • Build reusable components and use them from any number of projects.
    • Share components with other developers without sharing the source code.
    • Create different versions of your application (free, pro...) by referencing the same "core" library.

    The output of library compilation are two files: a jar file with the compiled code and a xml file that includes the metadata that is required by the IDE.

    These two files should be saved in one of the libraries folders.

    Compiling to a library is quite simple. Under Project menu there is a new compile option - "Compile To Library (Alt + 5)". When you choose this option all the modules except of the main activity are compiled into a library.


    You can exclude other modules as well with the ExcludeFromLibrary attribute (see this tutorial for more information about attributes).

    The main activity and the other excluded modules can be used to test the library.

    You can reference the library from other projects and access the same functionality as in the original project.

    Library specific attributes

    The following attributes are specific for library compilation:

    Project attributes (placed in the main activity):
    LibraryVersion - A number that represents the library version. This number will appear next to the library name in the libraries list.
    LibraryAuthor - The library author. This value is added to the library xml file.
    LibraryName (B4A v2.70) - The compiled library name. Sets the library name instead of showing the save dialog.

    All modules:
    ExcludeFromLibrary - Whether to exclude this module during library compilation. Values: True or False. Note that the Main activity is always excluded.

    Event - Adds an event to the list of events. This attribute can be used multiple times. Note that the events list only affects the IDE events autocompletion feature.


    - You should right click on the libraries list and choose Refresh after an update when updating a referenced library..
    - CallSub / CallSubDelayed - The first parameter for these keywords is a reference to the target module. When working with modules that reside in a library you should pass the module reference and not the module name as string (this is the better way to reference all modules in all cases).
    - Code obfuscation - Libraries can be obfuscated during library compilation. Strings will not be obfuscated in this mode.
    - Services that host home screen widgets cannot be compiled into a library.
  2. Omar

    Omar Member Licensed User

    This is fantastic!

    Can't wait to try out v2.50 :sign0098:
  3. barx

    barx Well-Known Member Licensed User

    Sounds very interesting. Erel, I love that you listen to users and respond with these awesome features. Well done buddy.
  4. Harris

    Harris Well-Known Member Licensed User

    I couldn't have imagined even asking for this one.
    This is a major leap forward!

    Whats next? Auto revenue generating ballistic mode.....
  5. arenaluigi

    arenaluigi Well-Known Member Licensed User

    Very Good :sign0142:
  6. rbsoft

    rbsoft Active Member Licensed User

    This is an awesome feature!
    I guess the library section of B4A will literally explode soon.

  7. icefairy333

    icefairy333 Active Member Licensed User

    sounds good
  8. susu

    susu Well-Known Member Licensed User

    Maybe someday we can use Java library (.jar file) in Basic4Android directly :icon_clap:
  9. skrjabin

    skrjabin Active Member Licensed User

    I think on this way more librarys will be published and the b4a-project will become more and more interesting. I really like it...

    Coming from VB.Net I am a Newbie here. After trying a "hallo world"- exemble in Java I more and more love b4a and its beautifull librarys. A web-Service to share and discuss librarys directly from the IDE would be a great think...
    Last edited: Jan 2, 2013
  10. thedesolatesoul

    thedesolatesoul Expert Licensed User

    Is it possible to add the B4A compiled lib and add it to a java project?
    That way we can enhance some java libs with code written in B4A. (I know that sounds stupid!)
  11. Erel

    Erel Administrator Staff Member Licensed User

    It is more or less not possible as the compiled code depends on some APIs of B4A internal framework.
  12. thedesolatesoul

    thedesolatesoul Expert Licensed User

    I see. Thanks. :)

    Sent from my GT-I9000 using Tapatalk 2
  13. bluedude

    bluedude Well-Known Member Licensed User

    few questions


    I have a few questions.

    Project attributes
    I now have this in my main activity

    #ApplicationLabel: ttTest
    #VersionCode: 1
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
    #LibraryVersion: 1.2
    #LibraryAuthor: ThingsTank

    Do I still need VersionCode and VersionName when adding the LibraryVersion and LibraryAuthor?

    Library ExcludeFromLibrary
    How should I put this in the module? Which exact syntax? Is that a project attribute?

    Library with code module
    Created a very simple code module (codeTest) with one sub called myTest. Compiled to library and put them in the library folder.

    I can now pick the library in a new project but my myTest method is not available. I cannot actually reference my library with Dim test as

    I'm probably doing something utterly stupid :)
  14. Erel

    Erel Administrator Staff Member Licensed User

    This attributes do not have any effect during library compilation. However you can also run the project (useful for testing).
    #ExcludeFromLibrary: True

    On each module that you want to exclude (this attribute is not available in the main activity which is always excluded).

    Have you refreshed the libraries after compilation?
  15. monki

    monki Active Member Licensed User

    Hi Erel,
    could you please give an example for the new # event attribute.
    My experiment with # Event: _Callback apears not in the list of events.
    The event list is updated only after compiling ??

    Thank you
  16. Erel

    Erel Administrator Staff Member Licensed User

    The event will only appear in the project that uses this library. Not in the library project.
  17. bluedude

    bluedude Well-Known Member Licensed User

    Great addition to B4A


    Done some testing and now all works fine. I actually like the fact that all stuff in Main can be used to do testing, that makes it very convenient. By using logging and calling all methods it could actually serve as a kind of unit testing.

    Adding events isn't 100% clear to me but I don't need that right now.

    Creating my own libraries will make my projects so much more modular and easier to oversee, thanks!
  18. Erel

    Erel Administrator Staff Member Licensed User

    I'm glad you find it useful. I also think that it will be very useful for building reusable components.

    There is no special mechanism to create events. The #Event attribute only affects the IDE events auto complete list.

    An example of a class with event (ItemClick): [Class] CustomListView - A flexible list based on ScrollView

    If you want to build a library from this class then you can add this attribute to the CustomListView module:
    #Event: ItemClick (Index As Int, Value As Object)
    This will make it easier for other developers to add this event.
  19. monki

    monki Active Member Licensed User

    @ Erel,
    thanks for the quick response.

    It also works perfectly with event parameters Example (_Callback (filename as string)


  20. bluedude

    bluedude Well-Known Member Licensed User

    Tested the event too and indeed that is handy for use in the IDE. This stuff makes me really happy!!! :)
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice