Android Tutorial Library compilation - Compile your project to a library

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.

SS-2013-01-01_17.18.50.png


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.

Classes:
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.

Notes

- 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.
 

barx

Well-Known Member
Licensed User
Longtime User
Sounds very interesting. Erel, I love that you listen to users and respond with these awesome features. Well done buddy.
 

Harris

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

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

rbsoft

Active Member
Licensed User
Longtime User
This is an awesome feature!
I guess the library section of B4A will literally explode soon.

Rolf
 

susu

Well-Known Member
Licensed User
Longtime User
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.

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

Cadenzo

Active Member
Licensed User
Longtime User
Great!
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:

bluedude

Well-Known Member
Licensed User
Longtime User
few questions

Hi,

I have a few questions.

Project attributes
I now have this in my main activity

#ApplicationLabel: ttTest
#VersionCode: 1
#VersionName:
#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 :)
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Do I still need VersionCode and VersionName when adding the LibraryVersion and LibraryAuthor?
This attributes do not have any effect during library compilation. However you can also run the project (useful for testing).
How should I put this in the module? Which exact syntax? Is that a project attribute?
#ExcludeFromLibrary: True

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

I cannot actually reference my library with Dim test as
Have you refreshed the libraries after compilation?
 

monki

Active Member
Licensed User
Longtime 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
Monki
 

bluedude

Well-Known Member
Licensed User
Longtime User
Great addition to B4A

Erel,

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!
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I'm glad you find it useful. I also think that it will be very useful for building reusable components.

Adding events isn't 100% clear to me but I don't need that right now.
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:
B4X:
#Event: ItemClick (Index As Int, Value As Object)

This will make it easier for other developers to add this event.
 

monki

Active Member
Licensed User
Longtime User
@ Erel,
thanks for the quick response.

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

monki

:sign0098:
 

bluedude

Well-Known Member
Licensed User
Longtime User
Tested the event too and indeed that is handy for use in the IDE. This stuff makes me really happy!!! :)
 
Top