无法将 Java 类型转换为 Yail 类型

Cannot convert Java type to Yail type

我正在尝试为 AppInventor 构建一个扩展,但是当我执行 ant extensions 时,我得到了这个错误:

[javac] An annotation processor threw an uncaught exception.

[javac] Consult the following stack trace for details.

[javac] java.lang.RuntimeException: Cannot convert Java type 'android.content.Context' to Yail type


完整错误日志:

    [javac]
    [javac] An annotation processor threw an uncaught exception.
    [javac] Consult the following stack trace for details.
    [javac] java.lang.RuntimeException: Cannot convert Java type 'android.content.Context' to Yail type
    [javac]     at com.google.appinventor.components.scripts.ComponentProcessor.javaTypeToYailType(ComponentProcessor.ja
va:1184)
    [javac]     at com.google.appinventor.components.scripts.ComponentDescriptorGenerator.outputParameters(ComponentDesc
riptorGenerator.java:208)
    [javac]     at com.google.appinventor.components.scripts.ComponentDescriptorGenerator.outputBlockMethod(ComponentDes
criptorGenerator.java:187)
    [javac]     at com.google.appinventor.components.scripts.ComponentDescriptorGenerator.outputComponent(ComponentDescr
iptorGenerator.java:125)
    [javac]     at com.google.appinventor.components.scripts.ComponentDescriptorGenerator.outputResults(ComponentDescrip
torGenerator.java:226)
    [javac]     at com.google.appinventor.components.scripts.ComponentProcessor.process(ComponentProcessor.java:731)
    [javac]     at com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.ja
va:794)
    [javac]     at com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironm
ent.java:705)
    [javac]     at com.sun.tools.javac.processing.JavacProcessingEnvironment.access00(JavacProcessingEnvironment.java
:91)
    [javac]     at com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.run(JavacProcessingEnvironment.java:1
035)
    [javac]     at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.jav
a:1176)
    [javac]     at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1170)
    [javac]     at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:856)
    [javac]     at com.sun.tools.javac.main.Main.compile(Main.java:523)
    [javac]     at com.sun.tools.javac.main.Main.compile(Main.java:381)
    [javac]     at com.sun.tools.javac.main.Main.compile(Main.java:370)
    [javac]     at com.sun.tools.javac.main.Main.compile(Main.java:361)
    [javac]     at com.sun.tools.javac.Main.compile(Main.java:56)
    [javac]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    [javac]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    [javac]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    [javac]     at java.lang.reflect.Method.invoke(Method.java:498)
    [javac]     at org.apache.tools.ant.taskdefs.compilers.Javac13.execute(Javac13.java:58)
    [javac]     at org.apache.tools.ant.taskdefs.Javac.compile(Javac.java:1395)
    [javac]     at org.apache.tools.ant.taskdefs.Javac.execute(Javac.java:1121)
    [javac]     at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
    [javac]     at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
    [javac]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    [javac]     at java.lang.reflect.Method.invoke(Method.java:498)
    [javac]     at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    [javac]     at org.apache.tools.ant.Task.perform(Task.java:348)
    [javac]     at org.apache.tools.ant.taskdefs.Sequential.execute(Sequential.java:68)
    [javac]     at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
    [javac]     at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
    [javac]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    [javac]     at java.lang.reflect.Method.invoke(Method.java:498)
    [javac]     at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    [javac]     at org.apache.tools.ant.Task.perform(Task.java:348)
    [javac]     at org.apache.tools.ant.taskdefs.MacroInstance.execute(MacroInstance.java:402)
    [javac]     at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
    [javac]     at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
    [javac]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    [javac]     at java.lang.reflect.Method.invoke(Method.java:498)
    [javac]     at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    [javac]     at org.apache.tools.ant.Task.perform(Task.java:348)
    [javac]     at org.apache.tools.ant.Target.execute(Target.java:435)
    [javac]     at org.apache.tools.ant.Target.performTasks(Target.java:456)
    [javac]     at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1405)
    [javac]     at org.apache.tools.ant.helper.SingleCheckExecutor.executeTargets(SingleCheckExecutor.java:38)
    [javac]     at org.apache.tools.ant.Project.executeTargets(Project.java:1260)
    [javac]     at org.apache.tools.ant.taskdefs.Ant.execute(Ant.java:441)
    [javac]     at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
    [javac]     at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
    [javac]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    [javac]     at java.lang.reflect.Method.invoke(Method.java:498)
    [javac]     at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
    [javac]     at org.apache.tools.ant.Task.perform(Task.java:348)
    [javac]     at org.apache.tools.ant.Target.execute(Target.java:435)
    [javac]     at org.apache.tools.ant.Target.performTasks(Target.java:456)
    [javac]     at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1405)
    [javac]     at org.apache.tools.ant.Project.executeTarget(Project.java:1376)
    [javac]     at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
    [javac]     at org.apache.tools.ant.Project.executeTargets(Project.java:1260)
    [javac]     at org.apache.tools.ant.Main.runBuild(Main.java:857)
    [javac]     at org.apache.tools.ant.Main.startAnt(Main.java:236)
    [javac]     at org.apache.tools.ant.launch.Launcher.run(Launcher.java:287)
    [javac]     at org.apache.tools.ant.launch.Launcher.main(Launcher.java:113)

BUILD FAILED
D:\appinventor-sources\appinventor\build.xml:37: The following error occurred while executing this line:
D:\appinventor-sources\appinventor\components\build.xml:343: The following error occurred while executing this line:
D:\appinventor-sources\appinventor\components\build.xml:230: Compile failed; see the compiler error output for details.

可在此处找到导致此错误的文件:

https://github.com/barreeeiroo/appinventor-sources/blob/master/appinventor/components/src/com/google/appinventor/components/runtime/VersionName.java

我认为该行是: import android.content.Context; 第 24 行

当我构建扩展时,该文件没有错误,就像导入该库时出错一样,因为我尝试 import android.content.pm.Context 但它在读取文件时崩溃了,所以我想是 Javac 或 Java 错误


我该如何解决?

通常你会得到这样的上下文

public class TaifunTools extends AndroidNonvisibleComponent {
  private static Context context;
  private static final String LOG_TAG = "TaifunTools";

  public TaifunTools(ComponentContainer container) {    
    super(container.$form());
    this.container = container;
    context = (Context) container.$context();
  }

所以稍后您将使用此代码段来获取版本名称

  @SimpleFunction(description = "Returns the version name of the app")
  public String VersionName() {
    Log.d(LOG_TAG, "VersionName");
    try {
      PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
      String version = pInfo.versionName;
      return version;
    } catch (NameNotFoundException e) {
      Log.e(LOG_TAG, e.getMessage());
      e.printStackTrace();
      return "";
    }
  }

顺便说一句。有关使用 App Inventor 资源的问题,最好的地方是在 App Inventor Open Source forum...

中提问

另见 App Inventor Extensions document and look into the example extensions provided by MIT...