|
| 1 | +# JNIgen record_use |
| 2 | + |
| 3 | +JNIgen generates Dart bindings to Java/Kotlin code. After tree-shaking the generated |
| 4 | +Dart code not all Java/Kotlin code is reachable. To tree-shake the unreachable |
| 5 | +Java/Kotlin code we want to generate ProGuard rules that list all the |
| 6 | +reachable Java/Kotlin classes, methods, and fields. |
| 7 | + |
| 8 | +## Information needed for tree-shaking |
| 9 | + |
| 10 | +### 1. Which classes are possibly instantiated. |
| 11 | + |
| 12 | +```dart |
| 13 | +class PDDocument extends jni$_.JObject { |
| 14 | + PDDocument.fromReference( |
| 15 | + jni$_.JReference reference, |
| 16 | + ); |
| 17 | +
|
| 18 | + factory PDDocument() { |
| 19 | + return PDDocument.fromReference( |
| 20 | + _new$(_class.reference.pointer, _id_new$ as jni$_.JMethodIDPtr) |
| 21 | + .reference); |
| 22 | + } |
| 23 | +
|
| 24 | + factory PDDocument.new$1( |
| 25 | + jni$_.JObject? memUsageSetting, |
| 26 | + ); |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +This requires tracking non-const (factory) constructor calls. |
| 31 | + |
| 32 | +* https://github.com/dart-lang/native/issues/2907 |
| 33 | + |
| 34 | +Because the `JObjects` are wrappers around native pointers, none of the JNIgen |
| 35 | +generated classes will ever have const constructors. |
| 36 | + |
| 37 | +### 2. Which methods, getters and setters are reachable. |
| 38 | + |
| 39 | +```dart |
| 40 | +class PDDocument extends jni$_.JObject { |
| 41 | + void addPage( |
| 42 | + jni$_.JObject? page, |
| 43 | + ) |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +This requires tracking instance calls, or generating a static call inside the |
| 48 | +instance call. JNIgen already generates a static field (with a static getter) |
| 49 | +that could be annotated to record a static call: |
| 50 | + |
| 51 | +```dart |
| 52 | +class PDDocument extends jni$_.JObject { |
| 53 | + static final _addPage = jni$_.ProtectedJniExtensions.lookup< |
| 54 | + jni$_.NativeFunction< |
| 55 | + jni$_.JThrowablePtr Function( |
| 56 | + jni$_.Pointer<jni$_.Void>, |
| 57 | + jni$_.JMethodIDPtr, |
| 58 | + jni$_.VarArgs<(jni$_.Pointer<jni$_.Void>,)>)>>( |
| 59 | + 'globalEnv_CallVoidMethod') |
| 60 | + .asFunction< |
| 61 | + jni$_.JThrowablePtr Function(jni$_.Pointer<jni$_.Void>, |
| 62 | + jni$_.JMethodIDPtr, jni$_.Pointer<jni$_.Void>)>(); |
| 63 | +
|
| 64 | + void addPage( |
| 65 | + final _$page = page?.reference ?? jni$_.jNullReference; |
| 66 | + _addPage(reference.pointer, _id_addPage as jni$_.JMethodIDPtr, |
| 67 | + _$page.pointer) |
| 68 | + .check(); |
| 69 | + ) |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +Java fields in JNIgen generated code are Dart getters and setters. |
| 74 | + |
| 75 | +* https://github.com/dart-lang/native/issues/2906 |
| 76 | + |
| 77 | +### 3. Mapping Dart identifiers to native identifiers |
| 78 | + |
| 79 | +For each class-use and method/field-use the link hook in JNIgen needs to know |
| 80 | +the original Java/Kotlin unique name. |
| 81 | + |
| 82 | +Since JNIgen is already a code generator, this can be achieved by generating a |
| 83 | +file that maps Dart identifiers to Java identifiers: |
| 84 | + |
| 85 | +```dart |
| 86 | +const dartToJava = { |
| 87 | + DartMethodIdentifer( |
| 88 | + importUrl: 'package:foo/foo.dart', |
| 89 | + name: 'Bar', |
| 90 | + methodName: 'baz', |
| 91 | + ) : JavaMethodIdentifier( |
| 92 | + qualifiedImport: 'org.foo.foo', |
| 93 | + name: 'Bar', |
| 94 | + methodName: 'baz', |
| 95 | + ), |
| 96 | + // ... |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +## Links |
| 101 | + |
| 102 | +* https://pub.dev/packages/jnigen |
0 commit comments