Android Tutorial [java] Creating libraries for B4A

emresensoy

New Member
Licensed User
Longtime User
i did all things but there is no jar file, only xml file;
B4X:
<?xml version="1.0" encoding="UTF-8"?>
<root>
    <doclet-version-NOT-library-version>1.02</doclet-version-NOT-library-version>
    <class>
        <name>b4a.deneme.DenemeLib</name>
        <shortname>DenLib</shortname>
        <owner>process</owner>
        <method>
            <name>mul</name>
            <comment></comment>
            <returntype>int</returntype>
            <parameter>
                <name>x</name>
                <type>int</type>
            </parameter>
        </method>
    </class>
</root>
 

socialnetis

Active Member
Licensed User
Longtime User
I'm algo having a "No class def found". And I have set the Eclipse project configuration Java compiler compliance to Java 1.6.

In a package I can have 2 or more classes, do I need to put "@ShortName("somename")" to every class right? Is there a problem if the shortname matches the class name?
 

agraham

Expert
Licensed User
Longtime User
A package can have more than one class. The classes can either be in separate .java files or can be static classes within a single main class in a single .java file.

You need an @ShortName("somename") annotation on each class that you want exposed to Basic4android. the name can be whatever you want.

Note that if you have more than .java file when you do Project -> Generate Javadoc if your package shows a filled in square and not a tick then not all .java files are selected and the missing ones will not be included in the xml. You will need to click the box until it shows a tick.
 

socialnetis

Active Member
Licensed User
Longtime User
What am I doing wrong?

I want to make a little library to access some functions of the Telephony library.

Here is my b4a code:
B4X:
Sub Globals
   Dim t As Telephony
End Sub

Sub Activity_Create(FirstTime As Boolean)
   'Activity.LoadLayout("Layout1")
   Dim l As List
End Sub

I'm not even using the telephony object and I get this error:
main_globals (java line: 266)

java.lang.NoClassDefFoundError: socialnetis.telephony.SNTelephony

at b4a.example.main._globals(main.java:266)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:167)
at b4a.example.main.initializeGlobals(main.java:222)
at b4a.example.main.afterFirstLayout(main.java:82)
at b4a.example.main.access$100(main.java:16)
at b4a.example.main$WaitForLayout.run(main.java:72)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4611)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
at dalvik.system.NativeStart.main(Native Method)

This are the Java files:
SNTelephony.java:
B4X:
package socialnetis.telephony;

import android.content.Context;
import android.telephony.NeighboringCellInfo;
import android.telephony.TelephonyManager;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.objects.collections.List;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;

@ShortName("Telephony")
@Version(1.0f)

public class SNTelephony {

   public List getAllCellInfo(final BA ba) {
      TelephonyManager t = (TelephonyManager)ba.context.getSystemService(Context.TELEPHONY_SERVICE);
      List l;
      l = new List();
      l.Initialize();
      for (NeighboringCellInfo n : t.getNeighboringCellInfo()) {  
         SNNeighboringCellInfo s;
         s = new SNNeighboringCellInfo();
         s.Initialize(n);
         l.Add(s);
      }
      return l;
    }
}

SNNeighboringCellInfo.java:
B4X:
package socialnetis.telephony;

import android.telephony.NeighboringCellInfo;
import anywheresoftware.b4a.BA.ShortName;

@ShortName("NeighboringCellInfo")

public class SNNeighboringCellInfo {
   NeighboringCellInfo n;

   public void Initialize(NeighboringCellInfo ncf) {
      n = ncf;
   }
   public int getCid () {
      return n.getCid();
   } 
      
   public int getRssi () {
      return n.getRssi();      
   }
}

I do mark with a tick when I want to make the javadoc, and the .jar and .xml files are in the AddLibraries.

What am I missing?

EDIT:
The project had to target Java 6 (Project-Propertis-Java).
What I did before was Windows-preferences-Java and taget java 6
 
Last edited:

agraham

Expert
Licensed User
Longtime User
I've just built a library by cutting and pasting your code and it compiles and creates a Telephony instance just fine. I can't immediately think of something that would let the Class be used at compile time but fail to find it at run time. I've attached my version for you to try.
 

Attachments

  • Telephony.zip
    2.3 KB · Views: 783
Last edited:

jalle007

Active Member
Licensed User
Longtime User
Eclipse is telling me that it can't find BADoclet class althouh I set it like from the manual

RBp9MxH.png


any idea ?
 

Attachments

  • RBp9MxH.png
    RBp9MxH.png
    25.9 KB · Views: 713
Last edited:

socialnetis

Active Member
Licensed User
Longtime User
I've just built a library by cutting and pasting your code and it compiles and creates a Telephony instance just fine. I can't immediately think of something that would let the Class be used at compile time but fail to find it at run time. I've attached my version for you to try.

Hey, thanks for answering, It was a problem with JDK 7, i needed to use JDK 6 in the project to compile. Now it works
 

jalle007

Active Member
Licensed User
Longtime User
OK that helped. but still no success.
here is output from Eclipse

B4X:
Loading source files for package anywheresoftware.b4a.sample...
Constructing Javadoc information...
[-doclet, BADoclet]
[-docletpath, c:\Users\xxx\Dropbox\_ANDROID\B4A\BADoclet\]
[-sourcepath, C:\Users\xxx\workspace\TestLib\src]
[-classpath, C:\Users\xxx\AppData\Local\Android\android-sdk\platforms\android-8\android.jar;C:\Program Files (x86)\Anywhere Software\Basic4android\Libraries\Core.jar;C:\Program Files (x86)\Anywhere Software\Basic4android\Libraries\B4AShared.jar]
[-public]
[-b4atarget, c:\Users\xxx\Dropbox\_ANDROID\B4A\_JAVA\TestLib.xml]
starting....
Working with class: anywheresoftware.b4a.sample.TestLib
warning: no version field.
finish: c:\Users\xxx\Dropbox\_ANDROID\B4A\_JAVA\TestLib.xml

I have TestLib.xml
but no TestLib.jar

any idea ?
 

lorebarita

Member
Licensed User
Longtime User
Error:

I am getting the following error after i have loaded the library to b4a and i try to use it. Please help:sign0085:

java.lang.NoClassDefFoundError: anywheresoftware.b4a.sample.CardReader


at anywheresoftware.b4a.sample.main._globals(main.java:331)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:167)
at anywheresoftware.b4a.sample.main.initializeGlobals(main.java:222)
at anywheresoftware.b4a.sample.main.afterFirstLayout(main.java:82)
at anywheresoftware.b4a.sample.main.access$100(main.java:16)
at anywheresoftware.b4a.sample.main$WaitForLayout.run(main.java:72)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
 

ebalvis

Member
Licensed User
Longtime User
Lib fail

I am making a library for use printers star.
When I try to run the application I get the following error and the application closes.
I'm using in eclipse, JRE System Library [jdk1.6.0_26]

main_process_globals (java line: 329)


java.lang.VerifyError: innebo/ioport/printer/IOPortLib


at b4a.example.main._process_globals(main.java:329)
at b4a.example.main.initializeProcessGlobals(main.java:299)
at b4a.example.main.afterFirstLayout(main.java:85)
at b4a.example.main.access$100(main.java:16)
at b4a.example.main$WaitForLayout.run(main.java:74)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4898)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
at dalvik.system.NativeStart.main(Native Method)
 
Top