App Inventor build extension fails to import Android class in helpers subdirectory

Description

I can only import the Android classes in the main TextOptions.java file, but it gives an error when trying to import in TextInputWithButton.java which is in the helpers subdirectory. I found the source but the proposed solution doesn't work.

Structure

  • io:
    • textoptions:
      • TextOptions.java
    • helpers:
      • TextInputWithButton.java

TextInputWithButton.java:

package io.textoptions.helpers;

import android.content.Context;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class TextInputWithButton extends LinearLayout {

    private EditText editText;
    private Button button;

    public TextInputWithButton(Context context) {
        super(context);
        setOrientation(HORIZONTAL);

        editText = new EditText(context);
        button = new Button(context);

        // Personalize o botão, como cor de fundo e texto, conforme necessário
        button.setBackgroundColor(0xFF00FF00); // Exemplo: cor verde
        button.setText("Ok"); // Exemplo: texto do botão

        addView(editText);
        addView(button);
    }

    public EditText getEditText() {
        return editText;
    }

    public Button getButton() {
        return button;
    }
}

TextOptions.java:

package io.textoptions;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.provider.Settings;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.EditText;
import android.view.ViewGroup;

import com.google.appinventor.components.annotations.*;
import com.google.appinventor.components.runtime.*;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.errors.PermissionException;

import java.util.ArrayList;
import java.util.List;

import android.widget.Button;

import io.textoptions.helpers.TextInputWithButton;

@DesignerComponent(
        version = 2,
        description = "This extension displays a dialog with options to copy or share text from a Label component.",
        category = ComponentCategory.EXTENSION,
        nonVisible = true,
        iconName = "images/extension.png"
)
@SimpleObject(external = true)
public class TextOptions extends AndroidNonvisibleComponent {
...
}

Error

CommonConstants:
    [mkdir] Created dir: /home/iaiaia/Documentos/Kodular/CriarExtensoes/appinventor-sources/appinventor/components/build/classes/CommonConstants
    [javac] Compiling 42 source files to /home/iaiaia/Documentos/Kodular/CriarExtensoes/appinventor-sources/appinventor/components/build/classes/CommonConstants
    [javac] warning: [options] bootstrap class path not set in conjunction with -source 7
    [javac] /home/iaiaia/Documentos/Kodular/CriarExtensoes/appinventor-sources/appinventor/components/src/io/textoptions/helpers/TextInputWithButton.java:6: error: cannot find symbol
    [javac] public class TextInputWithButton extends LinearLayout {
    [javac]                                          ^
    [javac]   symbol: class LinearLayout
    [javac] /home/iaiaia/Documentos/Kodular/CriarExtensoes/appinventor-sources/appinventor/components/src/io/textoptions/helpers/TextInputWithButton.java:8: error: cannot find symbol
    [javac]     private EditText editText;
    [javac]             ^
    [javac]   symbol:   class EditText
    [javac]   location: class TextInputWithButton
    [javac] /home/iaiaia/Documentos/Kodular/CriarExtensoes/appinventor-sources/appinventor/components/src/io/textoptions/helpers/TextInputWithButton.java:9: error: cannot find symbol
    [javac]     private Button button;
    [javac]             ^
    [javac]   symbol:   class Button
    [javac]   location: class TextInputWithButton
    [javac] /home/iaiaia/Documentos/Kodular/CriarExtensoes/appinventor-sources/appinventor/components/src/io/textoptions/helpers/TextInputWithButton.java:11: error: cannot find symbol
    [javac]     public TextInputWithButton(Context context) {
    [javac]                                ^
    [javac]   symbol:   class Context
    [javac]   location: class TextInputWithButton
    [javac] /home/iaiaia/Documentos/Kodular/CriarExtensoes/appinventor-sources/appinventor/components/src/io/textoptions/helpers/TextInputWithButton.java:26: error: cannot find symbol
    [javac]     public EditText getEditText() {
    [javac]            ^
    [javac]   symbol:   class EditText
    [javac]   location: class TextInputWithButton
    [javac] /home/iaiaia/Documentos/Kodular/CriarExtensoes/appinventor-sources/appinventor/components/src/io/textoptions/helpers/TextInputWithButton.java:30: error: cannot find symbol
    [javac]     public Button getButton() {
    [javac]            ^
    [javac]   symbol:   class Button
    [javac]   location: class TextInputWithButton
    [javac] /home/iaiaia/Documentos/Kodular/CriarExtensoes/appinventor-sources/appinventor/components/src/io/textoptions/helpers/TextInputWithButton.java:3: error: package android.content does not exist
    [javac] import android.content.*;
    [javac] ^
    [javac] /home/iaiaia/Documentos/Kodular/CriarExtensoes/appinventor-sources/appinventor/components/src/io/textoptions/helpers/TextInputWithButton.java:4: error: package android.widget does not exist
    [javac] import android.widget.*;
    [javac] ^
    [javac] /home/iaiaia/Documentos/Kodular/CriarExtensoes/appinventor-sources/appinventor/components/src/io/textoptions/helpers/TextInputWithButton.java:13: error: cannot find symbol
    [javac]         setOrientation(HORIZONTAL);
    [javac]                        ^
    [javac]   symbol:   variable HORIZONTAL
    [javac]   location: class TextInputWithButton
    [javac] /home/iaiaia/Documentos/Kodular/CriarExtensoes/appinventor-sources/appinventor/components/src/io/textoptions/helpers/TextInputWithButton.java:15: error: cannot find symbol
    [javac]         editText = new EditText(context);
    [javac]                        ^
    [javac]   symbol:   class EditText
    [javac]   location: class TextInputWithButton
    [javac] /home/iaiaia/Documentos/Kodular/CriarExtensoes/appinventor-sources/appinventor/components/src/io/textoptions/helpers/TextInputWithButton.java:16: error: cannot find symbol
    [javac]         button = new Button(context);
    [javac]                      ^
    [javac]   symbol:   class Button
    [javac]   location: class TextInputWithButton
    [javac] 11 errors
    [javac] 1 warning

Directory image

Please check the documentation

Adding Helper Blocks to a Component and How to add a Dropdown Block to a Component
Taifun

1 Like

Thanks! I can import classes from the helpers directory correctly but I'm not succeeding when I try to import classes from Android (import android)..

There is no need to do this
Just folliw the examples

Taifun

Out of curiosity, is there a way to do this in helpers?

import android.content.Context;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

No.

Thanks

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

Context

  • Recently I researched how to import Android classes into classes that are in the helpers directory, look here. Not finding a solution, I went searching and modified the build.xml file. So, to be able to import the classes, follow the steps below:

  • Change the code below:

   <ai.javac destdir="${CommonConstants-class.dir}">
      <include name="**/helpers/*.java" />
      <include name="${components.pkg}/common/*.java" />
      <exclude name="${components.pkg}/common/HtmlEntities.java" />
    </ai.javac>
  • per:
    <ai.javac destdir="${CommonConstants-class.dir}">
      <include name="**/helpers/*.java" />
      <include name="${components.pkg}/common/*.java" />
      <exclude name="${components.pkg}/common/HtmlEntities.java" />
 
	        <classpath>
	            <pathelement location="${android.lib}" />
	        </classpath>
    </ai.javac>

Can you write what is the purpose of this? What do you really want to achieve?

I asked the community a question about importing the Android libraries into the helpers directory. I tried to send the solution via the original post, but it was already closed.

Then why didn't you remove the solution marker?

I understand, but I'm asking about programming, what are you trying to do with the helpers?

2 Likes

I was trying to import classes of the type below in the helpers directory:

import android.media.MediaRecorder;

By default, when using "ant extensions" it gives an error, classes of this type are not found in the helpers directory, but only in the main file.

Yes, but why do you think, you need this there?

Taifun

It is not necessary to return values ​​from other classes. Just return invented Integer values ​​from the helpers, and in the main extension code use the "Switch Case" to use the appropriate values ​​from the MediaRecorder class.

I understand, it was a class example. The tip applies as a general rule.

1 Like

Have you checked your method with companion and apk?

Yes!