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.
 

Johan Schoeman

Expert
Licensed User
I am trying to create a library. I have the code in a code module and compiled it to a library. The copied the XML and JAR files into the B4A default library folder. When starting the B4A editor my lib file appears in the list of Referenced Libraries. If I select/tick it to be part of a library to use in a project then it does not appear in the drop down list when I want to declare it (eg Dim ml as mylib). Yet, I can use it by referencing it directly in the code (eg mylib.do_whatever(a,b)) - although there is no code module inside the project at all from which I have compiled the library files from.

Also, how do I hide process_globals from appearing in the dropdown list?
 

Johan Schoeman

Expert
Licensed User
With reference to the first issue that I have raised, should the code reside in a class module and not a code module when compiling a standalone library? If I compile a lib when putting the code in a class module it seems to do the trick.

If so, what then is generated when the code is compiled to a library while residing in a code module? it still creates JAR and XML files but does not appear in the Dim xx As.... dropdown box. It is a bit confusing and will appreciate some explanation with regards to the difference
 

klaus

Expert
Licensed User
You should tell us in detail what you want to do and what you have done.
Without this it's very difficult to give you concrete advices because we need to guess what you could have done.
You can Dim a class, but you cannot Din a code module.
In a code module you can call routines, also when compiled to a library, but these don't need any Dim.
 

Johan Schoeman

Expert
Licensed User
Hi Klaus, what I am trying to understand is the following:

If I have created a project with code in the code module of the project and I compile the code in the code module to a library, should the compiled library behave is all aspects (i.e declaring it in a project with Dim, appear in the dropdown list when typing Dim xx As..., etc) similar to a library such as @margret's StringFunctions library once the XML and JAR files have been copied into the default B4A libraries folder? Or should the program code be in a class module prior to it being converted/compiled into a standalone library? Or does it not matter? Should the compiled library be the same regardless of the code being in a class module or in a code module prior to compilation?
 

klaus

Expert
Licensed User
You can compile a code module to a library !
You don't need to copy the jar and xml files to the B4A libraries folder, they are automaticaly copied to the Additional Libraries folder which you should have defined in the menu Tools / Configure Paths.
If you give your library the name MyLibrary, when you compile it, and check it in the Libs Tab you can call all Subs simply with MyLibrary.MySubName. All Subs will be shown in the inline help. To call the Subs you must use the Module name as the prefix, not the library name. You could have deveral modules in the same library.
The library name can be different from the code module name.
There is no need to Dim it.
If you complie the module and want to test in the same project you should remove the code module.
 
Last edited:

Johan Schoeman

Expert
Licensed User
Thanks Klaus. I have noticed that when I have the code in a code module prior to compiling it into a library then I can address/call the lib as you have described above i.e call it directly without having to Dim a variable that will reference it. But it is different when I place the same code in a class module - I have to Dim and Initialize it before I can use it. Probably doing something wrong but that is what I have found to be the case with what I have ben doing. In any case, I have accomplished what I wanted to accomplish by compiling from a class module - it is working 100%. I will recreate the two different scenarios and post it so that you can have a look at it and see where I go wrong.
 

klaus

Expert
Licensed User
Code modules and Class modules are different.
You can have several instances of a class in the same project so you need to Dim them.
In a code module you only have subroutines so there is nothing to initiate, just call them.
As I don't know what you have, want or need in the library impossible to give a concrete advice.
If it's only code use code module.
 

klaus

Expert
Licensed User
Copy the two files into the Files directory, I think they will be included in the zip file, haven't tested it.
Then after unziping it the user must transfer the two files to the Additional Libraries folder.
 

stevel05

Expert
Licensed User
To get them automatically into the zip, I think you will also have to add them to the files Tab within the B4a IDE, or you could just add them to the zip file after creating it using 7zip or similar.
 

klaus

Expert
Licensed User
Yes, I did it !
You have different module names (Module1, Module2 etc) and you save the library with another name (MyLibrary).
Then you check MyLibrary in the Libs Tab and call the routines Module1.AnySub, Module2.AnySub etc.
 

Johan Schoeman

Expert
Licensed User
Yes, I did it !
You have different module names (Module1, Module2 etc) and you save the library with another name (MyLibrary).
Then you check MyLibrary in the Libs Tab and call the routines Module1.AnySub, Module2.AnySub etc.

Hi Klaus

Please help. If I do as you say above then how will one know what subs reside in the different code modules (that is if I for ex downloaded a library and are unfamiliar with the subs within the library)? I cannot seem to get it displayed. Have tried for eg MyLibrary.Module1.Anysub (obvioiusly with my own library name, own module name, own sub name) but it I cannot get the subs that are available to be displayed. I do however get an error message if I try MyLibrary.Module1.Anysub but Module1.Anysub works. But then how does one get to know what subs are in Module1?
 

LucaMs

Expert
Licensed User
You will need to edit the XML for that. Remove ClassB from the XML.

I do not know if I'm wrong now, or when I did that test.

I have two classes, the first of which uses the second. I would like to expose only the first class, but if I take off from the xml file the declaration of the second class I get an error, the second class is not available to the first (I get the classic: "Are you missing a library reference?")
 
Top