How do you import a second .java class to MIT App Inventor extension?

import io.deps.Currency;

//@UsesLibraries(libraries = "Currency.jar")
@DesignerComponent(
        version = 1,
        description = "Extension to assist financial and statistical calculations",
        category = ComponentCategory.EXTENSION,
        nonVisible = true,
        iconName = "images/extension.png"
)
@SimpleObject(external = true)
public class FinancialCalculator extends AndroidNonvisibleComponent {

    public FinancialCalculator(ComponentContainer container) {
        super(container.$form());
    }
    

    @SimpleFunction(description = "Format an integer as Brazilian currency")
    public String FormatCurrency(int amount, @Options(Currency.class) final String currencyCode) {

I created the Currency class (Currency.java) that works with financial calculations. I'm having trouble importing the Currency class correctly into the main FinancialCalculator.java file. Please help me!

Show your Package declaration and write where the Currency class is located. Ok, I see that the Currency class is a helpers class. This class should be placed in the "helpers" folder, this folder should be in the same place as the main class of your extension.
If the Package for your extension is "io.deps", then the import for Currency should then look like this:
"io.deps.helpers.Currency"

  • io:
    • deps:
      FinancjalCalculator.java
      • helpers:
        Currency.java

Thank you!

How could I include an external library like apache Math – Download Apache Commons Math and how to import the methods?

You need to download the JAR library, place it in the "deps" folder, and then use the annotations in the extension:
@UsesLibraries(libraries = "class.jar")

thanks

The "deps" folder will be in a different location depending on whether you are using appinventor-sources or extension-template.

1 Like
import org.apache.commons.math3.distribution.NormalDistribution;
import org.apache.commons.math3.distribution.ExponentialDistribution;
import org.apache.commons.math3.distribution.PoissonDistribution;

@UsesLibraries(libraries = "commons-math3-3.6.1.jar")
@DesignerComponent(
        version = 1,
        description = "Extension to assist financial and statistical calculations",
        category = ComponentCategory.EXTENSION,
        nonVisible = true,
        iconName = "images/extension.png"
)
@SimpleObject(external = true)
public class FinancialCalculator extends AndroidNonvisibleComponent {

Like this?

1 Like

Yes

import org.apache.commons.math3.distribution.NormalDistribution;

excellent! It worked!

AndroidRuntime:
    [mkdir] Created dir: /home/iaiaia/Documentos/Kodular/CriarExtensoes/appinventor-sources/appinventor/components/build/classes/AndroidRuntime
    [javac] Compiling 366 source files to /home/iaiaia/Documentos/Kodular/CriarExtensoes/appinventor-sources/appinventor/components/build/classes/AndroidRuntime
    [javac] warning: [options] bootstrap class path not set in conjunction with -source 7
    [javac] Note: Wrote file file:///home/iaiaia/Documentos/Kodular/CriarExtensoes/appinventor-sources/appinventor/components/build/classes/AndroidRuntime/simple_components.json
    [javac] Note: Wrote file file:///home/iaiaia/Documentos/Kodular/CriarExtensoes/appinventor-sources/appinventor/components/build/classes/AndroidRuntime/simple_components.txt
    [javac] Note: Wrote file file:///home/iaiaia/Documentos/Kodular/CriarExtensoes/appinventor-sources/appinventor/components/build/classes/AndroidRuntime/simple_components_build_info.json
    [javac] Note: Wrote file file:///home/iaiaia/Documentos/Kodular/CriarExtensoes/appinventor-sources/appinventor/components/build/classes/AndroidRuntime/AutogeneratedOdeMessages.java
    [javac] Note: Wrote file file:///home/iaiaia/Documentos/Kodular/CriarExtensoes/appinventor-sources/appinventor/components/build/classes/AndroidRuntime/ComponentsTranslation.java
    [javac] /home/iaiaia/Documentos/Kodular/CriarExtensoes/appinventor-sources/appinventor/components/src/io/kodular/FinancialCalculator.java:38: error: package org.apache.commons.math3.distribution does not exist
    [javac] import org.apache.commons.math3.distribution.NormalDistribution;
    [javac]                                             ^
    [javac] /home/iaiaia/Documentos/Kodular/CriarExtensoes/appinventor-sources/appinventor/components/src/io/kodular/FinancialCalculator.java:39: error: package org.apache.commons.math3.distribution does not exist
    [javac] import org.apache.commons.math3.distribution.ExponentialDistribution;
    [javac]                                             ^
    [javac] /home/iaiaia/Documentos/Kodular/CriarExtensoes/appinventor-sources/appinventor/components/src/io/kodular/FinancialCalculator.java:40: error: package org.apache.commons.math3.distribution does not exist
    [javac] import org.apache.commons.math3.distribution.PoissonDistribution;
    [javac]                                             ^
    [javac] Note: Some input files use or override a deprecated API.
    [javac] Note: Recompile with -Xlint:deprecation for details.
    [javac] 3 errors
    [javac] 1 warning

BUILD FAILED
/home/iaiaia/Documentos/Kodular/CriarExtensoes/appinventor-sources/appinventor/build.xml:73: The following error occurred while executing this line:
/home/iaiaia/Documentos/Kodular/CriarExtensoes/appinventor-sources/appinventor/components/build.xml:228: The following error occurred while executing this line:
/home/iaiaia/Documentos/Kodular/CriarExtensoes/appinventor-sources/appinventor/build-common.xml:125: Compile failed; see the compiler error output for details.
import org.apache.commons.math3.distribution.NormalDistribution;
import org.apache.commons.math3.distribution.ExponentialDistribution;
import org.apache.commons.math3.distribution.PoissonDistribution;

@DesignerComponent(
        version = 1,
        description = "Extension to assist financial and statistical calculations",
        category = ComponentCategory.EXTENSION,
        nonVisible = true,
        iconName = "images/extension.png"
)
@UsesLibraries(libraries = "commons-math3-3.4.jar")
@SimpleObject(external = true)
public class FinancialCalculator extends AndroidNonvisibleComponent {

JAR file: https://repo1.maven.org/maven2/org/apache/commons/commons-math3/3.4/commons-math3-3.4.jar

I don't know where you placed the jar file.


All files: kodular - Google Drive
in deps

You cannot create the deps folder. This folder already exists in appinventor-sources, you need to add your library to it.

home/iaiaia/Documentos/Kodular/CriarExtensoes/appinventor-sources/appinventor/build/components/deps/

1 Like

Please see the Extensions document about how to work with external libraries

http://ai2.appinventor.mit.edu/reference/other/extensions.html

Taifun

1 Like

I did this but I still get the error "error: package org.apache.commons.math3.distribution does not exist"

I already solved this part :pray:

What was the reason?

These files are deleted every time I run the "ant extensions" command. This is a problem?

home/iaiaia/Documentos/Kodular/CriarExtensoes/appinventor-sources/appinventor/build/components/deps/

It is not possible to delete files from the deps folder because all the jar libraries built into appinventor are in this directory.