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.
 

gjoisa

Active Member
Licensed User
Longtime User
Trying to test Library compilation and using the lib in other projects . Totally confused . For testing purpose wrote a small piece of code and compiled . Attching the project here . Please help to solve the problem .
 

Attachments

  • MathLib.zip
    11.4 KB · Views: 618

gjoisa

Active Member
Licensed User
Longtime User
After adding .jar and .xml file to Library folder , I checked library in my project . But how declare a variable which uses Lib's function ?
 

JesseW

Active Member
Licensed User
Longtime User
First off, Erel, THANK YOU SO VERY MUCH for this addition!! We've been needing this for a while now.

...- Code obfuscation - Libraries can be obfuscated during library compilation. Strings will not be obfuscated in this mode.

Will the strings in an obfuscated library be obfuscated in an obfuscated project that includes the library?

Jesse
 
Last edited:

EduardoElias

Well-Known Member
Licensed User
Longtime User
Erel,

My question is about debug.

I have divided my app in many libraries.

I see the Log function appears to not be working from inside the libraries.

Is it possible to generate libraries with debug info? is it possible to trace? or at least make the Log function work that can be viewer on IDE logcat?

Thanks

Eduardo
 

Jaames

Active Member
Licensed User
Longtime User
Hi !

Is it possible to include resources in the compiled library ?

:sign0098: and thanks for this feature
 

abner69

Member
Licensed User
Longtime User
Thanks Erel for a Great Job!

I am wondering, other than editing the libs xml file, is there an easier way to include comments on how to use each function (help)?

...Pablo
 

qsrtech

Active Member
Licensed User
Longtime User
Compile Library - include Layout File

Hi, Can't seem to find this anywhere (no pun intended lol). But how can we go about including a layout file in our library?
 

jcredk

Member
Licensed User
Longtime User
Sorry to ask as I am a bit new to B4A:

Coming from MS C# SharpDX, one thing that was a bit fustrating for me with B4A was the capacity to share reuse (proven bug-free) code in libraries.
I discovered the compile to library in the menu, that should solve my issue but it's usage is still not fully clear to me.

Could it be possible in this tutorial to add a sample with following code to download:
- a library project with a class "MathObj" and a function "Add2Numbers(a as integer, b as interger) as integer"
- a main project with an activity 2 fields (for a & b) and a "+" button) that would create an instance of "MathObj" and would do the sum in a 3rd field

This may help to understand the concepts and upper mentionned attributes !?

Thanks in advance
Jo
 

jcredk

Member
Licensed User
Longtime User
You could make a Class for this.
Have a look at the User's Guide chapter 10 Class modules
Best regards.
o_O I am not sure to understand: my point is to get a full working sample with a library and a main project: having proposed a class within the library is just for demonstration purpose ;) ... !?
 

klaus

Expert
Licensed User
Longtime User
You should try to do it on your own and if you have trouble you can ask for help.
You could write a Class for this purpose and compile it to a library.
Did you have a look at the User's guide there you find an example with the code, not the solution you ask for but the whole principle is explained.

Best regards.
 
Top