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.
 

LucaMs

Expert
Licensed User
Longtime User
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?")


I tried again with a very simple example of two classes and it works!

I have better control of my real project.
 

Johan Schoeman

Expert
Licensed User
Longtime User
It is not different than using the original code. In your case you have created a code module, so you should write:
B4X:
MathFunc.xx

Hi Erel

I have 5 code modules inside a project named for eg A, B, C, D, E
In module A I have declared sub A1 as Public - everything else as Private
In module B I have declared sub B1 as Public - everything else as Private
In module C I have declared sub C1 as Public - everything else as Private
In module D I have declared sub D1 as Public - everything else as Private
In module E I have declared sub E1 as Public - everything else as Private
I compiled the project to a library named for eg MyLib and then refreshed the library reference inside a new project and selected it
How do I call A1, B1, C1, D1, E1 from a new project? MyLib.A.A1 does not work. A.A1 does work but only because I know that A and A1 reside inside the MyLib. If someone else compiled the library then MyLib will show in the library reference - but it seems asif the name "MyLib" is now completely unrelated to what is actually inside the library. Type just MyLib or trying to ds MyLib gives no indication of A1 to E1. But if you type dim myA as ..... then A, B, C, D, and E show in the dropdown list (and when typing dim MyA as A the
and then type "A." it will show A1 in the dropdown list). But I can't seem to find a link between MyLib and what actaully resides in A1.

Can you please explain as I am obviously doing something wrong.

Thanks
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The important sentence is: It is not different than using the original code.

You call it exactly like you would have call the subs or variables if the original modules were part of the project.

The library name is never important.
If you want your library to behave like most libraries then you need to use classes instead of static code modules.
 

Johan Schoeman

Expert
Licensed User
Longtime User
The important sentence is: It is not different than using the original code.

You call it exactly like you would have call the subs or variables if the original modules were part of the project.

The library name is never important.
If you want your library to behave like most libraries then you need to use classes instead of static code modules.

Thanks Erel. I have tried the classes route too but it also seems to behave the same way. I thought it would behave as follows (regarless of the library being created via a code or class module):

Dim mlib as MyLib (and after you type "as" the dropdown list shows A1,B1,C1,D1,E1
mlib.A1

and not having to address it as

A.A1

If you for eg created the library and I downloaded it, how will I know what subs reside in the library (other than you perhaps document it somewhere in a post)? The library list will show MyLib but then if I try to do Dim MyLib it will not accept it. Unless I somehow know that A1 to A5 belong with MyLib in the dropdown list it will not be possible to now that it is in actual fact the case.
 

Cableguy

Expert
Licensed User
Longtime User
Hi Guys...

I'm trying this "compile to lib" stuff for the first time and need help...

I need help on events.

Take this code"

B4X:
Dim TT as test 'this will be my lib

TT.initialize(bla,bla,bla)

Now the "I need help" part"
B4X:
Sub TT_MyEvent
log("FIRE")
end Sub

I want the event to be fired to the main activity from within a sub in my lib...
like...
B4X:
........
if x+a+r <> 254658 then
'Call MyEvent in Main
else
......
end if
.............
....
How do I do THAT?
 

Cableguy

Expert
Licensed User
Longtime User
Thanks NJ!!!

I knew I had to pass my lib as an object, I just couldn't see how...
Now I can start my real work!
 

Cableguy

Expert
Licensed User
Longtime User
One last question...

If my Lib has NO GUI interaction, can I surpress the Activity attribute in the initializer?
 

Cableguy

Expert
Licensed User
Longtime User
Another Question...

Is it possible to create multiple libs around the same "external" object and use them in the same app?
Being more concrete...
I aim to build a series of helper libs that target the IOIO lib.
I am going to create one to handle connecting to the Board, and then a few more to do specific tasks...
I guess I need to pass the IOIO object from the first lib to the others, creating a two level hierarchy... but How?


IOIO External LIB
|
|
MyConnection Manager LIB
| | |
Helper#1 Helper#2 Helper#3
Is this even possible?
 

hanyelmehy

Active Member
Licensed User
Longtime User
I compile a library that include activity with GUI and modules ,but when i add this library to my project (i add library asset files to my project asset files)
i get this error when start library activity
java.lang.RuntimeException: Object should first be initialized (Activity).
Did you forget to call Activity.LoadLayout?

i am able to run library as a normal project without problems

Note :When Test Again I Found
-its work if use release mode
-i am not enable to add any thing relate to library activity in my project manifest
for example i need to set screenOrientation using
SetActivityAttribute(library activity name, android:screenOrientation, "landscape")
but i get error when make Compiling :
Compiling code. Error
Module: library activity name not found.
 
Last edited:

hanyelmehy

Active Member
Licensed User
Longtime User
There is an issue with the debugger and compiled activities. You can implement the logic and UI with classes instead of activities (like done in AnotherDataPicker and other custom views).
Thank you for answer ,any other idea for manifest issue
also i think its better to make calls using library name (libname.moduelname) ,in this case it well be more clear ,and well not make conflict with project modules names that use this library
 

opus

Active Member
Licensed User
Longtime User
Another great feature, thank you Erel!

I had one problem (Stupid.me). I searched for the created two library-files in project folder and found only the .xml.
I wouldn't have thought that they were already saved in the folder for additional libraries.
 

Dipak Dahake

New Member
Licensed User
I cannot instance the class which I have compiled to library.
No class is shown with Dim in the testing Project.
Please help.
 
Top