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.
 

luke2012

Well-Known Member
Licensed User
Longtime User
Outstanding feature!

Only a question.

How to use ExcludeFromLibrary for esclude modules?
 

Julio Ochoa

Member
Licensed User
Longtime User
I question... I can compile a library built in Basic4Android and call it from Javascript (with Html5) ... I would like to to this:
1. Create a library apk using some funcionality of BasicAndroid (i.e call the QR Bar code)
2. Create an apk using html and javascript (i.e phone gap)
3. Invoke the library built in BasicAndroid from the app apk built in phone gap

is this possible?
 

DonManfred

Expert
Licensed User
Longtime User
What you can do is making a simple wrapper-app using webview (incl extras; see tutorials about webview here).

Inside this webview you can show up your html5-website giving it a special parameter that it is called from within this webview (your app)

OR

You also can check if the b4a-object (see infos in warwounds posts and examples) is available from within you html5-page's javascript.
If it is avaiable then your html5-file seems to called from within you app resp. the webview with javascriptinterface in your app

You then can call subs from your app using javascript-calls (see link i posted above)
To make things like getting a barcode happen you have to deal with the javascriptinterface. As i think getting a barcode is handled like "effecting the ui when a sub is called which want a returnanswer. I think the way could be

To get an barcode you have to call a app-sub which dont give an result. In this sub you call whatever it needs to get the barcode... If you got the barcode as a string you then call a pieces of javascript from within your app (see here for more info) to give the barcode back to your website using javascript.

Hope it helps
 
Last edited:

stevel05

Expert
Licensed User
Longtime User
Dim the other view as Private within your class.
 

LucaMs

Expert
Licensed User
Longtime User
Damn, maybe I have not explained well :( :)

I have a class: "ClassA". It uses another class: "ClassB", which should not be exposed from the library.

Private objA as ClassA

ObjA.ClassB <--- should not be visible (available)
 

stevel05

Expert
Licensed User
Longtime User
You must have a Dim for ClassB within ClassA, this should be
B4X:
Private ObjB As ClassB
 

stevel05

Expert
Licensed User
Longtime User
I've just tested it here and it works as expected, can you post your code?
 

stevel05

Expert
Licensed User
Longtime User
If I type "ObjA." I don't see ObjB in the list.
 

stevel05

Expert
Licensed User
Longtime User
No Problem:)
 

stevel05

Expert
Licensed User
Longtime User
Yes I can see both under these circumstances, I'm not sure you can hide it. One for Erel I think.
 
Top