Java Question .:Create Basic4Android Libraries:.

XverhelstX

Well-Known Member
Licensed User
Longtime User
Create Basic4Android Libraries - Step by Step Tutorial

I. Introduction.

Welcome to my step-by-step tutorial about creating a Basic4Android Library.
First, I would like to thank Erel a lot for making this wonderful application. It’s really one of the best and easiest programs to use for making as well easy as complex android programs. Normally I never buy something on the internet, but this really did change my mind.

II. Eclipse

Ok, So how do we start on creating a library.
First you should start by downloading Eclipse. Eclipse is a multi-language software development environment comprising an integrated development environment (IDE). It is written mostly in Java and can be used to develop applications in Java, Android and more. You can download Eclipse here:
Eclipse - The Eclipse Foundation open source community website. > Eclipse Classic > and select the download (for 32 or 64 bit). What’s good about Eclipse is that you don’t have to install it, and you can just open the program.
When Eclipse is downloaded, double-click the map and open ‘Eclipse’. This could take some time to load.

Image 1: ImageShack® - Online Photo and Video Hosting

III. Setting up your first library.

Now the IDE is open. Select File > New > Java Project. A pop-up window will appear.

Image 2: ImageShack® - Online Photo and Video Hosting

Enter a project name, this could be anything, the JRE should be JavaSE 1-6 and click on Finish. Now your project name should appear at the left Panel called Package Explorer. Locate your Project Name and press the left arrow next to it. You will see 2 items. Right-click on ‘src’ > new > class.

Image 3: ImageShack® - Online Photo and Video Hosting

A new popup window will appear. Enter the <Name> of the class and don’t forget to also enter a package name. It’s a must to fill in these 2 things. This should be something like example.example. this is your unique identifier.

Image 4: ImageShack® - Online Photo and Video Hosting

And press finish. Now you would have something like
B4X:
package example.example;

public class example {

}
Now you are almost ready, but first we have to link some java archive (jar) files to get it work with android. These are: Android.jar, B4AShared.jar and I always include Core.jar but this is optional. But now we locate them. It’s good to copy them all three in one map, otherwise you have to locate them for every project. I just add a map called ‘Additional Libraries’ in my Anywhere Software map. Ok, so the first .jar file is android. The android.jar file is located here: <android-folder>\platforms\android-4.
If you don't see android-4 then you need to download the SDK with the SDK manager: Android SDK | Android Developers. Then the 2 files is B4AShared.jar and core.jar, you can find these in the Basic4Android Library folder.

Image 5: ImageShack® - Online Photo and Video Hosting

IV. Configure your library.

Now you have the 3 java archive files, go back to Eclipse and right click on your project name in the Package explorer and select Build Path > add external libraries and locate your three jar files.

Image 6: ImageShack® - Online Photo and Video Hosting

Ok, that’s basically everything u need for started programming, but now u also need to know how to export the .jar and .xml file. This is not so hard.

- For the XML File:

In Eclipse, go to Project > Generate Javadoc. Again a new window will popup saying “Generate Javadoc.” In javadoc command: locate your javadoc.exe. mine is: C:\Program Files (x86)\Java\jdk1.6.0_21\bin\javadoc.exe. Make sure you select the right one and don’t just copy mine.
Next check the checkbox next to the library you want to convert. Visibility: public and select the radiobutton saying: use custom doclet. Doclet Name: “BADoclet” and Doclet class path: “C:\Program Files (x86)\Anywhere Software\Doclet“ both without the quotes. Remember that the path may be different from yours.

Image 7: ImageShack® - Online Photo and Video Hosting

Then click on next. In the extra javadoc options u type the following:
-b4atarget "C:\Program Files (x86)\Anywhere Software\Additional Libraries\Example.xml"
Additional Libraries is a map I created myself for all my libraries I make. Change Example.xml with your own output name that will appear in b4Android IDE and click finish. Now the XML file has been created.

Image 8: ImageShack® - Online Photo and Video Hosting

- For the JAR File:

Click on File > Export > and look for JAR file in the Java Map.

Image 9: ImageShack® - Online Photo and Video Hosting

Select it and press next. Now select the resource you want to output, here we use Example.jar and check the box. And in the export destination, you browse for the map. I use C:\Program Files (x86)\Anywhere Software\Additional Libraries\Example.jar. Mmake sure the name is equal as the XML File, except the extension. Click on finish.

Image 10: ImageShack® - Online Photo and Video Hosting

U are almost ready with your first library. What u now have to do every time is copying the xml and jar file from your additional library map to the Library map of anywhere software. You have to keep exporting the xml and jar file and moving it to the new map. (I use another map so if I accidently overwrite another one.)

Congratulations, You are done creating your library. Now go to Basic4Android, click the library tab at the right bottom of the screen, right-click above it( so not on the tab itself) and choose refresh. Now your library should have been added. Also don’t forget that every time you replace your libraries that you have to refresh the library list, otherwise you will keep having your current library using.

Image 11: ImageShack® - Online Photo and Video Hosting

V. Some basic codes.

I will go deeper in my SoundRecorder library and try to explain some codes.

B4X:
package xtremelyvirtualstudios.xvs.Sound;
//This is your package name.

B4X:
import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.Permissions;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;
// these are all your imports that you need for some codes. If you seem to have an error on some code (red line under it), right click on it and see if there’s a import function.

B4X:
@ShortName("AudioRecorder") // this is the name as it appear on the IDE
// for example dim record as AudioRecorder
@Permissions(values = {"android.permission.WRITE_EXTERNAL_STORAGE", "android.permission.RECORD_AUDIO"}) // Permissions must always be declared otherwise your app or library won’t work. Just search on the internet what permissions you need to use.
@Author("XverhelstX") / / your name
@Version(1f) // the version

B4X:
public class SoundRecorder implements OnCompletionListener, OnErrorListener {
   
    
    /**
     * Saves the state of the current recording. IDLE_STATE = 0, RECORDING_STATE = 1 or PLAYING_STATE = 2
     * 
     */
    
    public void saveState(Bundle recorderState) {
        recorderState.putString(SAMPLE_PATH_KEY, mSampleFile.getAbsolutePath());
        recorderState.putInt(SAMPLE_LENGTH_KEY, mSampleLength);
    }
    // the /** tags will give information about the code. This will appear under the code. When you type. (in B4Android)

    /**
     * Gets the maximum amplitude.
     * 
     */
    public int getMaxAmplitude() {
        if (mState != RECORDING_STATE)
            return 0;
        return mRecorder.getMaxAmplitude();
    }

With a public void, you will be able to create your own codes. For example
Record.saveState(…) will execute the code under it.

I hope you understand everything in this tutorial, and if not, ask questions under here.

if i forgot something, please PM me and sorry if I made some english mistakes. :)

Thank you very much

XverhelstX
 
Last edited:

stevel05

Expert
Licensed User
Longtime User
Thank you, I think I may need this in the near future. Then there will undoubtedly be some questions.

Steve
 

joedarock

Member
Licensed User
Longtime User
XverhelstX

Would it be possible for you to post some screen captures of your Eclipse project environment with all the files loaded and ready to build? Ultimately, it would also be nice to have an example zip package with the whole Eclipse project (all referenced files) contained and ready to load into Eclipse.

Thanks again for helping with this

Joe
 

Merlot2309

Active Member
Licensed User
Longtime User
Hello,

Great tut and very clear, thank you/mijn hartelijke dank :icon_clap:

Just started to write a doc myself in order to get it clear how to create a lib and the next lines maybe helpful as well:

Useable features
Right click on any import in the Outline pane on the right side of the screen and select “Open Type Hierarchy”.
On the left side pane the methods, properties, etc. appear.

How to add Referenced Libraries:
Right click Project name in PackageExpl pane (left side of screen)
-> Select: Properties -> Libraries tab -> Add External JARs

At this moment I do have some questions:
1. What is the difference between import and using a referenced lib?
2. Beside the 4 @ statements (ActivityObject, Permissions, ShortName and Version) what is needed to create a B4A lib? I guess that a B4A Lib differs from a Java/Android lib.
3. Finally found a good looking Java/Android calender project (https://github.com/jasonkostempski/Android-CalendarView) and I still don't know how to solve the error: cannot be resolved on the "import com.jasonkostempski.calendar.R;" line

Sorry for my horrible use of terminologie :BangHead:

Thanks in advance!

Helen.

Ps. Ik vraag me wel af of je in België opleidingen hebt voor het schrijven van documentatie. Zo goed en dat heb ik al vaker ervaren.
 

XverhelstX

Well-Known Member
Licensed User
Longtime User
XverhelstX

Would it be possible for you to post some screen captures of your Eclipse project environment with all the files loaded and ready to build? Ultimately, it would also be nice to have an example zip package with the whole Eclipse project (all referenced files) contained and ready to load into Eclipse.

Thanks again for helping with this

Joe

I will do it if I have some time. :)
You can find the most screen captures in Erel's tutorial.

Hello,

Great tut and very clear, thank you/mijn hartelijke dank

Just started to write a doc myself in order to get it clear how to create a lib and the next lines maybe helpful as well:

Useable features
Right click on any import in the Outline pane on the right side of the screen and select “Open Type Hierarchy”.
On the left side pane the methods, properties, etc. appear.

How to add Referenced Libraries:
Right click Project name in PackageExpl pane (left side of screen)
-> Select: Properties -> Libraries tab -> Add External JARs

At this moment I do have some questions:
1. What is the difference between import and using a referenced lib?
2. Beside the 4 @ statements (ActivityObject, Permissions, ShortName and Version) what is needed to create a B4A lib? I guess that a B4A Lib differs from a Java/Android lib.
3. Finally found a good looking Java/Android calender project (https://github.com/jasonkostempski/Android-CalendarView) and I still don't know how to solve the error: cannot be resolved on the "import com.jasonkostempski.calendar.R;" line

Sorry for my horrible use of terminologie

Thanks in advance!

Helen.

Ps. Ik vraag me wel af of je in België opleidingen hebt voor het schrijven van documentatie. Zo goed en dat heb ik al vaker ervaren.

Thanks for those useable features. ;D
Will be helpfull for some people.

I actually don't know much about Java and Eclipse, I just found a way on how to do it and shared it here. I don't think you need more @ tags for creating B4A Libraries.

i guess someone else could help u with your questions.

XverhelstX

PS: Heb ng nooit opleidingen ofzo gevolgd en ben het ook niet van plan ;p.Dit is waarschijnlijk mijn tweede tutorial ever :p
 

Merlot2309

Active Member
Licensed User
Longtime User
Hello XverhelstX,

Wouldn't it be better to move this tut to the Lib forum??? and make it sticky?
Nice blue logo that u have now.

Helen.
 

Merlot2309

Active Member
Licensed User
Longtime User
Hello again,

I know your not a mod :sign0089: but since you made this tut I thought it would be nicer to ask you, ha, ha.

Helen
 

Kiffi

Well-Known Member
Licensed User
Longtime User

what about this one?

15513poe9zqmlzq5y.gif


;)
 

joedarock

Member
Licensed User
Longtime User
XverhelstX

I finally got around to trying to follow your tutorial, step-by-step, and I have to admit that I'm not getting far. I'm having some problems getting the external resources loaded. Some of the screens in my Eclipse don't match the descriptions in the tutorial, and some of the language in the tutorial just isn't clear enough for this dummy. I think a few screen shots of what the Eclipse environment should look like would help me a lot.

Thanks
Joe
 

joedarock

Member
Licensed User
Longtime User
Wow, that was fast! You 'da man XverhelstX! Well, actually Erel's 'da man, but you rock too.

Thanks!
 
Top