Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pkgs/jni/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## 0.14.3-wip
## 0.15.0-wip

- **Breaking Change**: Made `Jni.env` internal.
- **Breaking Change**: Renamed `JObjType` to `JType`.
- **Breaking Change**: Made all of the type classes internal.
- Update to the latest lints.

## 0.14.2
Expand Down
45 changes: 0 additions & 45 deletions pkgs/jni/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,11 @@

// ignore_for_file: library_private_types_in_public_api

import 'dart:ffi';
import 'dart:io';

import 'package:ffi/ffi.dart';
import 'package:flutter/material.dart';
import 'package:jni/jni.dart';

extension on String {
/// Returns a Utf-8 encoded `Pointer<Char>` with contents same as this string.
Pointer<Char> toNativeChars(Allocator allocator) {
return toNativeUtf8(allocator: allocator).cast<Char>();
}
}

// An example of calling JNI methods using low level primitives.
// GlobalJniEnv is a thin abstraction over JNIEnv in JNI C API.
//
// For a more ergonomic API for common use cases of calling methods and
// accessing fields, see next examples using JObject and JClass.
String toJavaStringUsingEnv(int n) => using((arena) {
final env = Jni.env;
final cls = env.FindClass("java/lang/String".toNativeChars(arena));
final mId = env.GetStaticMethodID(cls, "valueOf".toNativeChars(arena),
"(I)Ljava/lang/String;".toNativeChars(arena));
final i = arena<JValue>();
i.ref.i = n;
final res = env.CallStaticObjectMethodA(cls, mId, i);
final str = env.toDartString(res);
env.DeleteGlobalRef(res);
env.DeleteGlobalRef(cls);
return str;
});

int randomUsingEnv(int n) => using((arena) {
final env = Jni.env;
final randomCls = env.FindClass("java/util/Random".toNativeChars(arena));
final ctor = env.GetMethodID(
randomCls, "<init>".toNativeChars(arena), "()V".toNativeChars(arena));
final random = env.NewObject(randomCls, ctor);
final nextInt = env.GetMethodID(randomCls, "nextInt".toNativeChars(arena),
"(I)I".toNativeChars(arena));
final res =
env.CallIntMethodA(random, nextInt, toJValues([n], allocator: arena));
env.DeleteGlobalRef(randomCls);
env.DeleteGlobalRef(random);
return res;
});
double randomDouble() {
final math = JClass.forName("java/lang/Math");
final random =
Expand Down Expand Up @@ -108,9 +66,6 @@ void main() {
Jni.spawn();
}
final examples = [
Example("String.valueOf(1332)", () => toJavaStringUsingEnv(1332)),
Example("Generate random number", () => randomUsingEnv(180),
runInitially: false),
Example("Math.random()", () => randomDouble(), runInitially: false),
if (Platform.isAndroid) ...[
Example("Minutes of usage since reboot",
Expand Down
1 change: 1 addition & 0 deletions pkgs/jni/ffigen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ structs:
- 'CallbackResult'
rename:
## opaque struct definitions, base types of jfieldID and jmethodID
'_Dart_FinalizableHandle': 'Dart_FinalizableHandle_'
'_jfieldID': 'jfieldID_'
'_jmethodID':
'jmethodID_'
Expand Down
52 changes: 43 additions & 9 deletions pkgs/jni/lib/_internal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,54 @@ export 'dart:isolate' show RawReceivePort, ReceivePort;
export 'package:meta/meta.dart' show internal;

export 'src/accessors.dart';
export 'src/jarray.dart'
show
$JArray$NullableType$,
$JArray$Type$,
$JBooleanArray$NullableType$,
$JBooleanArray$Type$,
$JByteArray$NullableType$,
$JByteArray$Type$,
$JCharArray$NullableType$,
$JCharArray$Type$,
$JDoubleArray$NullableType$,
$JDoubleArray$Type$,
$JFloatArray$NullableType$,
$JFloatArray$Type$,
$JIntArray$NullableType$,
$JIntArray$Type$,
$JLongArray$NullableType$,
$JLongArray$Type$,
$JShortArray$NullableType$,
$JShortArray$Type$;
export 'src/jni.dart' show ProtectedJniExtensions;
export 'src/jobject.dart' show $JObject$NullableType$, $JObject$Type$;
export 'src/jreference.dart';
export 'src/kotlin.dart'
show coroutineSingletonsClass, failureExceptionField, result$FailureClass;
export 'src/lang/jboolean.dart' show $JBoolean$NullableType$, $JBoolean$Type$;
export 'src/lang/jbyte.dart' show $JByte$NullableType$, $JByte$Type$;
export 'src/lang/jcharacter.dart'
show $JCharacter$NullableType$, $JCharacter$Type$;
export 'src/lang/jdouble.dart' show $JDouble$NullableType$, $JDouble$Type$;
export 'src/lang/jfloat.dart' show $JFloat$NullableType$, $JFloat$Type$;
export 'src/lang/jinteger.dart' show $JInteger$NullableType$, $JInteger$Type$;
export 'src/lang/jlong.dart' show $JLong$NullableType$, $JLong$Type$;
export 'src/lang/jnumber.dart' show $JNumber$NullableType$, $JNumber$Type$;
export 'src/lang/jshort.dart' show $JShort$NullableType$, $JShort$Type$;
export 'src/lang/jstring.dart' show $JString$NullableType$, $JString$Type$;
export 'src/method_invocation.dart';
export 'src/types.dart'
show
JAccessible,
JCallable,
JConstructable,
JObjType,
JType,
lowestCommonSuperType,
referenceType;
export 'src/nio/jbuffer.dart' show $JBuffer$NullableType$, $JBuffer$Type$;
export 'src/nio/jbyte_buffer.dart'
show $JByteBuffer$NullableType$, $JByteBuffer$Type$;
export 'src/third_party/generated_bindings.dart'
show JFieldIDPtr, JMethodIDPtr, JObjectPtr, JThrowablePtr, JniResult;
export 'src/types.dart' show JTypeBase, lowestCommonSuperType, referenceType;
export 'src/util/jiterator.dart'
show $JIterator$NullableType$, $JIterator$Type$;
export 'src/util/jlist.dart' show $JList$NullableType$, $JList$Type$;
export 'src/util/jmap.dart' show $JMap$NullableType$, $JMap$Type$;
export 'src/util/jset.dart' show $JSet$NullableType$, $JSet$Type$;

/// Temporary fix for the macOS arm64 varargs problem.
///
Expand Down
37 changes: 24 additions & 13 deletions pkgs/jni/lib/jni.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,33 @@ library;
export 'package:ffi/ffi.dart' show Arena, using;

export 'src/errors.dart';
export 'src/jarray.dart';
export 'src/jarray.dart'
hide
$JArray$NullableType$,
$JArray$Type$,
$JBooleanArray$NullableType$,
$JBooleanArray$Type$,
$JByteArray$NullableType$,
$JByteArray$Type$,
$JCharArray$NullableType$,
$JCharArray$Type$,
$JDoubleArray$NullableType$,
$JDoubleArray$Type$,
$JFloatArray$NullableType$,
$JFloatArray$Type$,
$JIntArray$NullableType$,
$JIntArray$Type$,
$JLongArray$NullableType$,
$JLongArray$Type$,
$JShortArray$NullableType$,
$JShortArray$Type$;
export 'src/jimplementer.dart';
export 'src/jni.dart' hide ProtectedJniExtensions, StringMethodsForJni;
export 'src/jobject.dart';
export 'src/jni.dart'
hide InternalJniExtension, ProtectedJniExtensions, StringMethodsForJni;
export 'src/jobject.dart' hide $JObject$NullableType$, $JObject$Type$;
export 'src/jreference.dart' hide ProtectedJReference;
export 'src/jvalues.dart';
export 'src/lang/lang.dart';
export 'src/nio/nio.dart';
export 'src/third_party/generated_bindings.dart'
hide JniBindings, JniEnv, JniEnv1, JniExceptionDetails;
export 'src/types.dart'
hide
JAccessible,
JCallable,
JConstructable,
JObjType,
JType,
lowestCommonSuperType;
export 'src/types.dart' hide JTypeBase, lowestCommonSuperType;
export 'src/util/util.dart';
34 changes: 0 additions & 34 deletions pkgs/jni/lib/jni_symbols.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,87 +3,57 @@ files:
'jni.dart':
'java.lang.Object':
name: JObject
type_class: JObjectType
nullable_type_class: JObjectNullableType
super_count: 0
'java.lang.String':
name: JString
type_class: JStringType
nullable_type_class: JStringNullableType
super_count: 1
'java.lang.Number':
name: JNumber
type_class: JNumberType
nullable_type_class: JNumberNullableType
super_count: 1
'java.lang.Byte':
name: JByte
type_class: JByteType
nullable_type_class: JByteNullableType
super_count: 2
'java.lang.Short':
name: JShort
type_class: JShortType
nullable_type_class: JShortNullableType
super_count: 2
'java.lang.Integer':
name: JInteger
type_class: JIntegerType
nullable_type_class: JIntegerNullableType
super_count: 2
'java.lang.Long':
name: JLong
type_class: JLongType
nullable_type_class: JLongNullableType
super_count: 2
'java.lang.Float':
name: JFloat
type_class: JFloatType
nullable_type_class: JFloatNullableType
super_count: 2
'java.lang.Double':
name: JDouble
type_class: JDoubleType
nullable_type_class: JDoubleNullableType
super_count: 2
'java.lang.Boolean':
name: JBoolean
type_class: JBooleanType
nullable_type_class: JBooleanNullableType
super_count: 1
'java.lang.Character':
name: JCharacter
type_class: JCharacterType
nullable_type_class: JCharacterNullableType
super_count: 1
'java.util.Set':
name: JSet
type_class: JSetType
nullable_type_class: JSetNullableType
super_count: 1
type_params:
E:
'java.lang.Object': DECLARED
'java.util.List':
name: JList
type_class: JListType
nullable_type_class: JListNullableType
super_count: 1
type_params:
E:
'java.lang.Object': DECLARED
'java.util.Iterator':
name: JIterator
type_class: JIteratorType
nullable_type_class: JIteratorNullableType
super_count: 1
type_params:
E:
'java.lang.Object': DECLARED
'java.util.Map':
name: JMap
type_class: JMapType
nullable_type_class: JMapNullableType
super_count: 1
type_params:
K:
Expand All @@ -92,11 +62,7 @@ files:
'java.lang.Object': DECLARED
'java.nio.Buffer':
name: JBuffer
type_class: JBufferType
nullable_type_class: JBufferNullableType
super_count: 1
'java.nio.ByteBuffer':
name: JByteBuffer
type_class: JByteBufferType
nullable_type_class: JByteBufferNullableType
super_count: 2
2 changes: 1 addition & 1 deletion pkgs/jni/lib/src/accessors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ extension JniResultMethods on JniResult {
return pointer == nullptr ? jNullReference : JGlobalReference(pointer);
}

T object<T extends JObject?>(JObjType<T> type) {
T object<T extends JObject?>(JType<T> type) {
return type.fromReference(reference);
}

Expand Down
3 changes: 3 additions & 0 deletions pkgs/jni/lib/src/errors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import 'dart:io';

import 'package:meta/meta.dart' show internal;

import 'third_party/generated_bindings.dart';

// TODO(#567): Add the fact that [JException] is now a [JObject] to the
Expand Down Expand Up @@ -37,6 +39,7 @@ sealed class JniError extends Error {

JniError(this.message);

@internal
factory JniError.of(JniErrorCode status) {
if (!_errors.containsKey(status)) {
status = JniErrorCode.ERR;
Expand Down
Loading
Loading