Android Example Library (JAR) interface using inline Java code and "direct calling"

Attached project demonstrates two ways to interface with a JAR file:

1. Using the JavaObject run method "directly"
2. Making use of inline Java code

For method 1 you need to make use of
#AdditionalJar: fiddleAround in the B4A code (see the code)

For method 2 you need to make use of
import com.johan.Geometry.Geometry; (see inline Java code)

com.johan.Geometry is a package name within fiddleAround.Jar
com.johan.Geometry.Geometry
is a class within package com.johan.Geometry
sphereVolume
and sphereSurface are methods within class Geometry

Project prints to the project log only.

Amazing what you can see inside a JAR when using a Java decompiler
It can make life much simpler when trying to use a/interface with a foreign JAR file.

fiddleAround.jar and fiddleAround.xml that are used in this example are inside the /files folder of the attached project.
 

Attachments

  • JHS_JO_IJC.zip
    70.9 KB · Views: 731

Johan Schoeman

Expert
Licensed User
Longtime User
Ooooops - small error inside the code. The last bit of Sub Activity_Create should look like this:

B4X:
    'Now we are going to call the same methods making use of inline java code
    'This initialization needs to be here and not earlier in Sub Activity_Create
    jo.InitializeContext
    Dim e As Double = 4
    e = jo.RunMethod("logVolume",Array(e))
    Log("ILJ sphereVolume = " & e)
   
    jo.InitializeContext
    e = 4
    e = jo.RunMethod("logSurface",Array(e))
    Log("ILJ sphereSurface = " & e)
 

Johan Schoeman

Expert
Licensed User
Longtime User
By the way...something unique about using 3 as the radius when calculating the volume and the surface area of a sphere. Another interesting fact about spheres:

The derivative of the volume (to R) yields the surface area....

Volume = 4/3 PI R^3
Surface Area = 4 PI R^2

Interesting...
 

Johan Schoeman

Expert
Licensed User
Longtime User
There is another way to interface with the fiddleAround library when fiddleAround is selected/enabled in the Libs tab of the project.

Dim sur As B4Ageometry

e = sur.sphereVolume(4.0)
Log("Using Library --> sphereVolume = " & e)

e = sur.sphereSurface(4.0)
Log("Using Library --> sphereSurface = " & e)

See additional code added to the attached project. Wow! B4A is flexible delux....!
 

Attachments

  • JHS_JO_IJC_3_methods.zip
    71 KB · Views: 441
Top