j2objc 不遵守死代码报告

j2objc not honoring dead code report

我试图在 gradle 项目中的几个 java 模块上将 j2objc 设置为 运行。我正在为 j2objc 使用 gradle 插件,但我认为这不是任何问题的根源。该模块使用 XMLUnit 来检测输入文件中的差异。 XMLUnit 包括一个 class org.xmlunit.builder.JaxbBuilder,由于没有看到一些在 j2objc 中被省略的 javax class,翻译失败,大概是因为它们无法翻译。无论如何,我的代码不使用 JaxbBuilder class 所以我创建了一个死代码报告来忽略它。我知道我可以使用 proguard 来生成一个更完整的集合,但现在,我的重点只是防止这个 class 被尝试,因为我仍在为 gradle CI 管道工作所有这些。该报告的标题为 dead_code.log,仅包含以下行:

org.xmlunit.builder.JaxbBuilder

j2objc 插件配置指定为:

j2objcConfig {
    autoConfigureDeps true
    // Xcode workspace directory
    xcodeProjectDir '../ios'

    translateArgs '--dead-code-report', './dead_code.log'
    translateArgs '--doc-comments'
    translateArgs '-v'

    finalConfigure()          // Must be last call to configuration
}

而且我可以在日志中看到死代码参数已添加到命令行,但在执行过程中我看到:

INEST: parsing jar:file:/Users/jenkins/.gradle/caches/modules-2/files-2.1/org.xmlunit/xmlunit-core/2.0.0-SNAPSHOT/b11b5e5d8e9f223af2fbaa93982d17b55899498/xmlunit-core-2.0.0-SNAPSHOT-sources.jar!org/xmlunit/builder/JaxbBuilder.java error: org/xmlunit/builder/JaxbBuilder.java:19: The import javax.xml.bind cannot be resolved error: org/xmlunit/builder/JaxbBuilder.java:20: The import javax.xml.bind cannot be resolved error: org/xmlunit/builder/JaxbBuilder.java:21: The import javax.xml.bind cannot be resolved error: org/xmlunit/builder/JaxbBuilder.java:22: The import javax.xml.bind cannot be resolved error: org/xmlunit/builder/JaxbBuilder.java:23: The import javax.xml.bind cannot be resolved error: org/xmlunit/builder/JaxbBuilder.java:24: The import javax.xml.bind cannot be resolved error: org/xmlunit/builder/JaxbBuilder.java:25: The import javax.xml.bind cannot be resolved error: org/xmlunit/builder/JaxbBuilder.java:26: The import javax.xml.bind cannot be resolved error: org/xmlunit/builder/JaxbBuilder.java:27: The import javax.xml.bind cannot be resolved error: org/xmlunit/builder/JaxbBuilder.java:31: The import java.beans.Introspector cannot be resolved error: org/xmlunit/builder/JaxbBuilder.java:43: Marshaller cannot be resolved to a type error: org/xmlunit/builder/JaxbBuilder.java:56: Marshaller cannot be resolved to a type error: org/xmlunit/builder/JaxbBuilder.java:57: Marshaller cannot be resolved to a type error: org/xmlunit/builder/JaxbBuilder.java:82: Marshaller cannot be resolved to a type error: org/xmlunit/builder/JaxbBuilder.java:83: The method createDefaultMarshaller() from the type JaxbBuilder refers to the missing type JAXBException error: org/xmlunit/builder/JaxbBuilder.java:87: JAXBSource cannot be resolved to a type error: org/xmlunit/builder/JaxbBuilder.java:87: JAXBSource cannot be resolved to a type error: org/xmlunit/builder/JaxbBuilder.java:87: Marshaller cannot be resolved to a type error: org/xmlunit/builder/JaxbBuilder.java:92: JAXBException cannot be resolved to a type error: org/xmlunit/builder/JaxbBuilder.java:93: DataBindingException cannot be resolved to a type error: org/xmlunit/builder/JaxbBuilder.java:99: JAXBElement cannot be resolved to a type error: org/xmlunit/builder/JaxbBuilder.java:103: XmlRootElement cannot be resolved to a type error: org/xmlunit/builder/JaxbBuilder.java:103: Bound mismatch: The generic method getAnnotation(Class) of type AnnotatedElement is not applicable for the arguments (Class). The inferred type XmlRootElement is not a valid substitute for the bounded parameter error: org/xmlunit/builder/JaxbBuilder.java:103: XmlRootElement cannot be resolved to a type error: org/xmlunit/builder/JaxbBuilder.java:106: The method createJAXBElement(Object) from the type JaxbBuilder refers to the missing type JAXBElement error: org/xmlunit/builder/JaxbBuilder.java:108: The method createInferredJAXBElement(Object) from the type JaxbBuilder refers to the missing type JAXBElement error: org/xmlunit/builder/JaxbBuilder.java:117: JAXBException cannot be resolved to a type error: org/xmlunit/builder/JaxbBuilder.java:117: PropertyException cannot be resolved to a type error: org/xmlunit/builder/JaxbBuilder.java:118: JAXBContext cannot be resolved to a type error: org/xmlunit/builder/JaxbBuilder.java:119: JAXBElement cannot be resolved to a type error: org/xmlunit/builder/JaxbBuilder.java:120: JAXBContext cannot be resolved error: org/xmlunit/builder/JaxbBuilder.java:120: JAXBElement cannot be resolved to a type error: org/xmlunit/builder/JaxbBuilder.java:120: JAXBElement cannot be resolved to a type error: org/xmlunit/builder/JaxbBuilder.java:123: JAXBContext cannot be resolved error: org/xmlunit/builder/JaxbBuilder.java:125: Marshaller cannot be resolved to a type error: org/xmlunit/builder/JaxbBuilder.java:126: Marshaller cannot be resolved to a type error: org/xmlunit/builder/JaxbBuilder.java:126: Marshaller cannot be resolved to a variable error: org/xmlunit/builder/JaxbBuilder.java:130: JAXBElement cannot be resolved to a type error: org/xmlunit/builder/JaxbBuilder.java:133: JAXBElement cannot be resolved to a type error: org/xmlunit/builder/JaxbBuilder.java:136: JAXBElement cannot be resolved to a type error: org/xmlunit/builder/JaxbBuilder.java:137: JAXBElement cannot be resolved to a type error: org/xmlunit/builder/JaxbBuilder.java:137: The method createJaxbElementFromObjectFactory(T) from the type JaxbBuilder refers to the missing type JAXBElement error: org/xmlunit/builder/JaxbBuilder.java:139: The method createInferredJAXBElement(T) from the type JaxbBuilder refers to the missing type JAXBElement error: org/xmlunit/builder/JaxbBuilder.java:146: JAXBElement cannot be resolved to a type error: org/xmlunit/builder/JaxbBuilder.java:156: JAXBElement cannot be resolved to a type error: org/xmlunit/builder/JaxbBuilder.java:161: JAXBElement cannot be resolved to a type error: org/xmlunit/builder/JaxbBuilder.java:174: Introspector cannot be resolved

我将其解释为它试图处理 JaxbBuilder class。配置中是否缺少某些内容?

j2objc 的第一个翻译步骤是调用 Eclipse Java 编译器,因此需要解析死代码引用以便翻译器继续,即使带有这些引用的 class 后来被删除死代码消除步骤。添加一个 jar 文件来解决 javax.xml.bind classes(如 jaxb-api.jar)将清除大部分这些错误。

java.beans.Introspector 仍然是一个问题,因为它不是 Android API 的一部分,并且 j2objc 的 JRE 支持基于 Android 来源。由于此 class 及其引用将被删除,您可以尝试将 JVM 的 rt.jar 指定为翻译器的引导 class 路径(-Xbootclasspath: 标志)。翻译通常使用与 j2objc 的 JRE 库匹配的 jar,因此不支持的 classes 会提前报告。将 JVM rt.jar 指定为引导class 路径会使此错误检查失败,但允许解决不受支持的 classes.

由于以上听起来工作量很大,您还有另一个选择是请求 j2objc 的 gradle 插件有一个排除选项,例如:

j2objcConfig {
    ...
    translateArgs '-v'
    exclude 'org.xmlunit.builder.JaxbBuilder'
    ...

j2objc 团队的 gradle 插件与 j2objc 的插件是分开的,但他们对像这样的合理功能请求非常敏感。