Skip to content

[jnigen] Jnigen fails with FormatException #877

Closed

Description

@glanium

Jnigen fails with the following error.

This error message does not help me know what to do next.
What file does this error message mention about?

dart run jnigen --config .\jnigen.yaml
(jnigen) INFO: ApiSummarizer.jar exists. Skipping build..
(jnigen) INFO: execute java -jar .dart_tool/jnigen/ApiSummarizer.jar -c jar\okio-3.0.0.jar;jar\okhttp-4.10.0.jar --backend asm okhttp3.OkHttpClient    
Unhandled exception:
FormatException: Unexpected extension byte (at offset 1)
#0      _Utf8Decoder.convertChunked (dart:convert-patch/convert_patch.dart:1852:7)
#1      _Utf8ConversionSink.addSlice (dart:convert/string_conversion.dart:304:28)
#2      _Utf8ConversionSink.add (dart:convert/string_conversion.dart:300:5)
#3      _ConverterStreamEventSink.add (dart:convert/chunked_conversion.dart:69:18)
#4      _SinkTransformerStreamSubscription._handleData (dart:async/stream_transformers.dart:111:24)
#5      _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#6      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#7      _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#8      _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:784:19)
#9      _StreamController._add (dart:async/stream_controller.dart:658:7)
#10     _StreamController.add (dart:async/stream_controller.dart:606:5)
#11     _Socket._onData (dart:io-patch/socket_patch.dart:2447:41)
#12     _RootZone.runUnaryGuarded (dart:async/zone.dart:1594:10)
#13     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:339:11)
#14     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:271:7)
#15     _SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:784:19)
#16     _StreamController._add (dart:async/stream_controller.dart:658:7)
#17     _StreamController.add (dart:async/stream_controller.dart:606:5)
#18     new _RawSocket.<anonymous closure> (dart:io-patch/socket_patch.dart:1936:33)
#19     _NativeSocket.issueReadEvent.issue (dart:io-patch/socket_patch.dart:1379:14)
#20     _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
#21     _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)
#22     _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:118:13)
#23     _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:185:5)
 Flutter (Channel stable, 3.16.4, on Microsoft Windows [Version 10.0.22631.2428], locale ja-JP)
    • Flutter version 3.16.4 on channel stable at C:\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 2e9cb0aa71 (3 weeks ago), 2023-12-11 14:35:13 -0700
    • Engine revision 54a7145303
    • Dart version 3.2.3
    • DevTools version 2.28.4
dependencies:  
  jni: 0.7.2  

dev_dependencies:
  jnigen: 0.7.0

Activity

  1. HosseinYousefi commented on Jan 16, 2024

    @HosseinYousefi
    Contributor

    Can you share your jnigen.yaml?

  2. glanium commented on Jan 17, 2024

    @glanium
    Author

    It's simple for test purpose.

    summarizer:
      backend: asm
    
    output:
      dart:
        path: lib/src/okhttp3.dart
        structure: single_file
      bindings_type: dart_only
    
    class_path:
      - 'jar/okio-3.0.0.jar'
      - 'jar/okhttp-4.10.0.jar'
    
    classes:
      - 'okhttp3.OkHttpClient'
    
  3. marcoedomingos commented on Jan 26, 2024

    @marcoedomingos
    Contributor

    Hello @glanium, dont' know if you still facing this issue, but, from I can see from your config file, you trying to access a dependency that it is not implemented in the project, you need to fetch the dependency to be able to import it, like this:

    summarizer:
      backend: asm
    
    output:
      dart:
        path: lib/src/okhttp3.dart
        structure: single_file
      bindings_type: dart_only
    
    class_path:
      - 'jar/okio-3.0.0.jar'
      - 'jar/okhttp-4.10.0.jar'
    
    classes:
      - 'okhttp3.OkHttpClient'
    
    #Add this lines
    maven_downloads:
      source_deps:
        - "com.squareup.okhttp3:okhttp:4.12.0"
        - "com.squareup.okhttp3:logging-interceptor:4.12.0"
    

    Don't forget to run flutter build apk before generate the bindings, tell me if this help

  4. HosseinYousefi commented on Jan 26, 2024

    @HosseinYousefi
    Contributor

    I had assumed that the .jar files do exist in the jar/ directory. I tried running it on my machine and it "works", meaning that it produces the bindings, however since okhttp is a Kotlin library, there are functions like public final okhttp3.OkHttpClient$Builder -addNetworkInterceptor(kotlin.jvm.functions.Function1 function1) (with a hyphen in the name) which break the code, as having - in the identifiers is not allowed in Dart.

    This is done in okhttp to prevent ambiguous overloads from Java: https://github.com/square/okhttp/blob/76acdc5d0d437253d01dc4f90e43247d1ad22682/okhttp/src/main/kotlin/okhttp3/OkHttpClient.kt#L649-L664

    The fix is to either exclude the generation of this method for now, or a patch to jnigen to convert - in the name to something else. Unfortunately we don't yet support renaming. See #787.

  5. marcoedomingos commented on Jan 26, 2024

    @marcoedomingos
    Contributor

    @HosseinYousefi you managed to run without fetch the dependencies in the config file?

  6. HosseinYousefi commented on Jan 26, 2024

    @HosseinYousefi
    Contributor

    @HosseinYousefi you managed to run without fetch the dependencies in the config file?

    If you download the jars yourself, and put them in jars/ directory, you can generate bindings. maven_downloads is a convenience tool which I proposed to be eventually removed #576.

  7. glanium commented on Jan 26, 2024

    @glanium
    Author

    I had downloaded jar files manually and put them in jars directory but dose not work with that error.
    that error message does not help me at all.
    Then I don't know what to do next.

  8. HosseinYousefi commented on Jan 26, 2024

    @HosseinYousefi
    Contributor

    I had downloaded jar files manually and put them in jars directory but dose not work with that error. that error message does not help me at all. Then I don't know what to do next.

    It might be a windows problem, let me try it with that as well.

  9. HosseinYousefi commented on Jan 26, 2024

    @HosseinYousefi
    Contributor

    Are you sure that lib/src/okhttp3.dart is not being generated? I downloaded the jar file from https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp/4.10.0 and with the exact same config yaml, it works on both mac and Windows.

  10. glanium commented on Jan 26, 2024

    @glanium
    Author

    umm.
    I'm sure lib/src/okhttp3.dart is not generated.

    screenshot

  11. glanium commented on Jan 26, 2024

    @glanium
    Author

    I've checked jar's contents by renaming to .zip.
    It seems correct one.

    okhttp-4.10.0.jar 807,357 bytes
    okio-3.0.0.jar 24,754 bytes

  12. glanium commented on Jan 26, 2024

    @glanium
    Author

    JAVA_HOME=C:\Program Files\Microsoft\jdk-11.0.16.101-hotspot

  13. marcoedomingos commented on Jan 29, 2024

    @marcoedomingos
    Contributor

    I had assumed that the .jar files do exist in the jar/ directory. I tried running it on my machine and it "works", meaning that it produces the bindings, however since okhttp is a Kotlin library, there are functions like public final okhttp3.OkHttpClient$Builder -addNetworkInterceptor(kotlin.jvm.functions.Function1 function1) (with a hyphen in the name) which break the code, as having - in the identifiers is not allowed in Dart.

    This is done in okhttp to prevent ambiguous overloads from Java: https://github.com/square/okhttp/blob/76acdc5d0d437253d01dc4f90e43247d1ad22682/okhttp/src/main/kotlin/okhttp3/OkHttpClient.kt#L649-L664

    The fix is to either exclude the generation of this method for now, or a patch to jnigen to convert - in the name to something else. Unfortunately we don't yet support renaming. See #787.

    About that, what do you think about this proposal #956?

  14. 1 remaining item

  15. glanium commented on Apr 8, 2024

    @glanium
    Author

    yes.
    i got same error with jni 0.8.0 and jnigen 0.8.0

  16. HosseinYousefi commented on Apr 8, 2024

    @HosseinYousefi
    Contributor

    I'll try to fix this issue for the next version then. Could you please create a small reproducible example repository that fails?

  17. HosseinYousefi commented on Apr 23, 2024

    @HosseinYousefi
    Contributor

    @glanium

    • What version of java (JDK) are you using?
    • Can you send the output of java -jar .dart_tool/jnigen/ApiSummarizer.jar -c jar\okio-3.0.0.jar;jar\okhttp-4.10.0.jar --backend asm okhttp3.OkHttpClient?
  18. glanium commented on Apr 24, 2024

    @glanium
    Author

    java -version
    openjdk version "11.0.16.1" 2022-08-12 LTS
    OpenJDK Runtime Environment Microsoft-40648 (build 11.0.16.1+1-LTS)
    OpenJDK 64-Bit Server VM Microsoft-40648 (build 11.0.16.1+1-LTS, mixed mode)

    java -jar ..dart_tool\jnigen\ApiSummarizer.jar -c .\jar\okhttp-4.10.0.jar;.\jar\okio-3.0.0.jar -b asm okhttp3.OkHttpClient
    Need to specify paths to source files
    usage: java -jar [-s <SOURCE_DIR=.>] [-c <CLASSES_JAR>]
    <CLASS_OR_PACKAGE_NAMES>
    Class or package names should be fully qualified.
    -b,--backend backend to use for summary generation
    ('doclet', 'asm' or 'auto' (default)).
    -c,--classes paths to search for compiled classes
    -D,--doctool-args arguments to pass to the documentation tool
    -M,--use-modules use Java modules

    oh, this might be cause.

  19. HosseinYousefi commented on Apr 24, 2024

    @HosseinYousefi
    Contributor

    That's odd, it seems that the okhttp3.OkHttpClient is not being parsed as an argument in your machine, causing getArgs().length to be < 1! Again everything seems to be working on my machine on the same OpenJDK version. The only thing that might cause this issue which is different in my setup is your ja-JP locale. So this is most likely an issue in Apache Commons CLI and/or Windows terminal. I'll investigate this further.

    Edit: yes, I can reproduce the issue with ja-JP locale.

    P.S. I assume ..dart_tool with two dots is a typo, otherwise it wouldn't have run at all?

  20. added a commit that references this issue on Apr 24, 2024
    778ea98
  21. moved this from Todo to In Progress in JNIgen trackeron Apr 24, 2024
  22. added a commit that references this issue on Apr 25, 2024
    3bc1eb8
  23. moved this from In Progress to Done in JNIgen trackeron Apr 25, 2024
  24. added a commit that references this issue on May 2, 2024
    ed4628b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions