Android Tutorial Shared Code Modules

Edit: shared modules are still supported however more powerful options are available in newer versions of B4X: https://www.b4x.com/android/forum/threads/86587

B4A v3.80 adds support for shared code modules.
Shared code modules are modules (Activity, Service, Class or Static Code) that are stored in a specific folder and can be shared by multiple projects, similar to the way that libraries are shared.

How it works

When a project is loaded the IDE looks for the modules files (bas files) in the project folder. If the module file doesn't exist then the IDE will look for the module in the configured shared modules folder.

The shared modules folder is set in the Paths Configuration dialog:

SS-2014-05-08_10.52.44.png


You can see that a module was loaded from the shared folder in the list of modules (the icon with the small arrow):

SS-2016-01-10_10.09.28.png


Adding a shared module to a project is done in the same way as adding a non-shared module.
You choose Project -> Add Existing Module. If the module folder is the shared folder then the module will be loaded as a shared module and will not be copied to the project folder.

If you want to convert a non-shared module to a shared module then you need to manually move the module file to the shared modules folder and reload the project
 
Last edited:

Martin Larsen

Active Member
Licensed User
Longtime User
I can't make this work. I have copied StateManager.bas to Documents\B4A\SharedModules\ and included this path for shared modules in Configure Paths. Then restarted IDE just to make sure.

When I add the module, it is copied to the project folder.

What do I do wrong?
 

Martin Larsen

Active Member
Licensed User
Longtime User
I read it from the SharedModules folder.

This is the path from Configure Paths: c:\Users\marlar\Documents\B4A\SharedModules\

I have copied the StateManager.bas into that folder. Then I include the module using Add existing module and select the module from said path.

But the module is immediately copied to the project folder.
 

Martin Larsen

Active Member
Licensed User
Longtime User
The folder names are the same. I copied it to the clipboard.

I use 3.8.2

Tried again, and it just copies the file to the project folder.
 

sorex

Expert
Licensed User
Longtime User
Are there any plans to have support for a shared modules folder inside the project folder?

I'd prefer

projectfolder\B4A
projectfolder\B4i
projectfolder\SharedModules

to keep things together, also because it's placed in my DropBox folder so that it syncs to both machines.

I guess each app can have its shared classess between B4x that are not relevant to any other project so it better belongs to the project folder unless it's something general like the statemanager that Martin mentioned.
 

sorex

Expert
Licensed User
Longtime User
the problem that we have now is that selecting a code module in a folder outside the project leads to a copy of the file to the project folder
instead of just storing the path to the file.

If it stored the path relative to the project my suggestion above would've worked already.
 

sorex

Expert
Licensed User
Longtime User
right but with the focus of shared modules for all apps.

if I write a tetris clone for B4A & B4i and write the game logic in a class to share between the 2 versions it's only usefull for this app.

so it doesn't fit inbetween classes/modules like statemanager and the likes that can be used in almost every app.

I just did the test.

copy a test.bas file to shared modules folder
add it to the project
nothing gets copied and it shows test (shared)


copy a test.bas file to shared module folder\tetris
add it to the project
files gets copied again and no (shared) mentioned.

maybe it should check if %sharedmodules% is in the selected path so that sub folder work aswell so that we can organize things a little.
 

demasi

Active Member
Licensed User
Longtime User
Close the IDE, delete the module file from the project folder and try to open the project.
It worked for me too, but only when I close the IDE and delete manually the shared module from my project folder.
How can I add a shared module to my project without copy it to my project folder?
thank you!
 

iCAB

Well-Known Member
Licensed User
Longtime User
Is there a way to have shared modules configured on project by project basis?
For example:
Say I have a set of projects (call it "A") that share some common code and another set ("Call it "B" ) that share common code that is not related to projects "A".
Can I create 2 separate shared folders one for "A" and one for "B" and configure the path by project?
 

LucaMs

Expert
Licensed User
Longtime User
Maybe a thread like "Code Snippets", containing shared modules, could be useful. I know that these modules should be "very customized", but maybe...!

Wanting to share Activity modules, an idea may be to create a folder on your PC containing .bal files, like: "... /SharedLayouts".
 

demasi

Active Member
Licensed User
Longtime User
I use to have some files like functions.bas that I use in all my projects.
I think it would be very useful if I could have a folder where I can put this kind of files, and they will be shared by default in all my projects. It´s something like a core extension, everything I put there is imported by all new projects, as default, but really shared, not copied. Example: I put there a file called Functions.bas, and in this file, a code module, I have a function called CreateBox. so, when I created a new project if i just call CreateBox it will be there, ready to use, by default. Or, maybe, at least, put a window above the libs panel with the name of all modules in the Shared Code Folder, just to check what we need.
 

littlerunningdeer

Member
Licensed User
The idea of creating a local copy of an existing module upon inclusion is nuts. You think it's clever - fine - better tell me. Now I have lots of crap to merge. My intuition tells me if I'm including a module outside of the current proj folder - it's shared. If you want to make it a visible option at inclusion - fine - that's the way microsoft does it. Also having a single shared folder is way too limiting.
 
Top