JsonFactory in extension cratch app

Hello.
I try to write an extension to get calendar events.
in this extension i use:
JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
of course i have import com.google.api.client.json.JsonFactory;

there is no compil error, but when i import my extension in app inventor and built app, app can't works it cratch as soon as it open

I refine my extention in order to point where was the problem,
so here it is. it does nothing but declare a JsonFactory ....

to built the extension i use git bash ant extensions

package jml.CalendarManager;

import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.gson.GsonFactory;

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

@DesignerComponent(version =1 ,  description = "USE_YOUR_DESCRIPTION ",
        category = ComponentCategory.EXTENSION,
        nonVisible = true   
		)
		//@UsesPermissions(permissionNames = "android.permission.WRITE_EXTERNAL_STORAGE")

@SimpleObject(external = true)
public class CalendarManager extends AndroidNonvisibleComponent {
    private ComponentContainer container;
    public CalendarManager(ComponentContainer container) {
        super(container.$form());
        this.container = container;
    }

    private static String TOKENS_DIRECTORY_PATH = "YOUR_TOKEN_DIRECTORY_PATH";
    private static JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();

	}

is someone could help me ?

You need to include the jar library in the extension. It is not included by ai2 in the apk during compilation.

import com.google.api.client.json.JsonFactory; - google-http-client-1.22.0.jar

1 Like

Thanks Patryk_F.

how do I include the jar in the extension ?
if i write this line import com.google.api.client.json.JsonFactory; - google-http-client-1.22.0.jar
i get a compil error:
error: class, interface, or enum expected
[javac] import com.google.api.client.json.JsonFactory; - google-http-client-1.22.0.jar

For one jar you can use:
@UsesLibraries("google-http-client-1.22.0.jar")

You can add more jar files as needed, separating them with a comma.
@UsesLibraries({"google-http-client-1.22.0.jar", "next.jar"})

You would have an easier time working with extensions if you moved to the Fast environment.

1 Like

I added librairies as you told me but it doesn't work either....even if there is no problem in companion...only my phone crashs

(I stored .jar in app inventor lib folder)

you're brobably rigth but i spent lot of time to find a way to built extensions few years ago and i use it may be once a year so i'm not motivated to leanr an other way...:see_no_evil:

It's possible that some dependencies are still needed. Or entries in build.xml. It's been a while since I used building an extension in ai2 sources.

Follow the documentation, App Inventor Extensions chapter 3 2.3

3.2.3 Using external libraries

In addition to your own Java code, you can also include external libraries, by following the steps in How to Add a Component. The library jar files will be included in the aix file when you package your extension

Taifun

Hello Taifun
i already edited the components/build.xml to include the folder where i put my jar files.

it seems there were a mistake in syntax so i corrected my line with:
@UsesLibraries(libraries ="google-api-client-1.22.0.jar,"+"google-api-services-calendar-v3-rev20220715-2.0.0.jar,"+"google-api-services-storage-v1-rev78-1.22.0.jar,"+"google-http-client-1.22.0.jar,"+"google-http-client-gson-1.42.1.jar,"+"google-http-client-jackson2-1.22.0.jar,"+"google-oauth-client-1.22.0.jar" )

but my app always crashs

This was not a syntax error since this annotation accepts both forms.

public @interface UsesLibraries {

  /**
   * The names of the libraries separated by commas.
   *
   * @return  the library name
   */
  String libraries() default "";

  /**
   * The names of the libraries (as an array)
   *
   * @return  the array of library names
   */
  String[] value() default {};
}

Debug your application using Logcat. You may find that some classes are still missing.
You can also open your extension with e.g. winrar, and see if it contains the right classes.

There was a mistake in my the components/build.xml file !!!
it works now !

thanks Patryk_F and Patryk_F

but now I got a runtime error JKS not found... :sleepy:

Why are you using JsonFactory?

I am using google Calendar API which needs a Jsonfactory for getting a GoogleClientSecrets and a GoogleAuthorizationCodeFlow.

But Jsonfactory seems not to work on android even though it works well on computer...