Skip to content

Commit 79683d7

Browse files
Added Java loadLibrary to Android plugin to support pyjnius (#128)
* pyjnius support * Install Flutter 3.24 * Test on iOS only * Try Flutter 3.24.5 * flutter doctor * Call flutter doctor after FVM * Run all jobs * MAIN_ACTIVITY_JAVA_CLASS_NAME env var * Renamed to MAIN_ACTIVITY_HOST_CLASS_NAME * Package version bumped to 0.8.5
1 parent e678424 commit 79683d7

File tree

25 files changed

+147
-40
lines changed

25 files changed

+147
-40
lines changed

.appveyor.yml

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
skip_branch_with_pr: true
22

33
environment:
4+
FLUTTER_VERSION: 3.24.5
45
GITHUB_TOKEN:
56
secure: 9SKIwc3VSfYJ5IChvNR74mEv2nb0ZFftUzn3sGRdXipXEfKSxY50DoodChHvlqZduQNhjg0oyLWAAa3n+iwWvVM2yI7Cgb14lFNClijz/kHI/PibnjDMNvLKaAygcfAc
67

@@ -51,9 +52,9 @@ for:
5152

5253
install:
5354
- HOMEBREW_NO_AUTO_UPDATE=1 brew install cocoapods
54-
- flutter upgrade
55+
- source ci/install_flutter.sh
5556
- flutter config --enable-macos-desktop
56-
- flutter doctor
57+
- flutter doctor -v
5758

5859
build: off
5960

@@ -72,16 +73,14 @@ for:
7273

7374
install:
7475
- HOMEBREW_NO_AUTO_UPDATE=1 brew install cocoapods
75-
- flutter upgrade
76-
- flutter config --enable-macos-desktop
76+
- source ci/install_flutter.sh
7777
# - xcrun simctl list runtimes
7878
# - xcrun simctl create "e2e test" "iPhone 12" "com.apple.CoreSimulator.SimRuntime.iOS-17-2"
7979
# - xcrun xctrace list devices
8080
# - |
8181
# UDID=$(xcrun xctrace list devices | grep "^e2e test Simulator (17.2)" | awk '{gsub(/[()]/,""); print $NF}')
8282
# echo $UDID
8383
# xcrun simctl boot "${UDID:?No Simulator with this name found}"
84-
#- flutter doctor -v
8584

8685
build: off
8786

@@ -117,8 +116,7 @@ for:
117116
- sudo chown $USER /dev/kvm
118117
- emulator -avd "${DEVICE_NAME}" -memory 2048 -wipe-data -no-boot-anim -cache-size 1000 -noaudio -no-window -partition-size 8192 &
119118
- adb wait-for-device shell 'while [[ -z $(getprop dev.bootcomplete) ]]; do sleep 1; done;'
120-
- flutter upgrade --force
121-
- flutter doctor -v
119+
- source ci/install_flutter.sh
122120

123121
build: off
124122

@@ -141,7 +139,12 @@ for:
141139
# VC_REDIST_DIR: 'C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Redist\MSVC\14.29.30133\x64\Microsoft.VC142.CRT'
142140

143141
install:
144-
- flutter upgrade --force
142+
- dart pub global activate fvm
143+
- set PATH=%LOCALAPPDATA%\Pub\Cache\bin;%USERPROFILE%\fvm\default\bin;%PATH%
144+
- fvm install %FLUTTER_VERSION%
145+
- fvm global %FLUTTER_VERSION%
146+
- flutter --version
147+
- flutter doctor
145148

146149
build: off
147150

@@ -161,7 +164,7 @@ for:
161164
install:
162165
- sudo apt update --allow-releaseinfo-change
163166
- sudo apt install -y xvfb libgtk-3-dev libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libgstreamer-plugins-bad1.0-dev gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-libav gstreamer1.0-doc gstreamer1.0-tools gstreamer1.0-x gstreamer1.0-alsa gstreamer1.0-gl gstreamer1.0-gtk3 gstreamer1.0-qt5 gstreamer1.0-pulseaudio
164-
- flutter upgrade --force
167+
- source ci/install_flutter.sh
165168

166169
build: off
167170

@@ -185,8 +188,7 @@ for:
185188
- sudo apt install -y clang ninja-build xvfb libgtk-3-dev gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-libav
186189
- git clone https://github.com/flutter/flutter.git -b stable "$HOME/flutter"
187190
- export PATH="$PATH:$HOME/flutter/bin"
188-
- flutter upgrade
189-
- flutter doctor
191+
- source ci/install_flutter.sh
190192

191193
build: off
192194

ci/install_flutter.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
dart pub global activate fvm
2+
export PATH=$HOME/.pub-cache/bin:$HOME/fvm/default/bin:$PATH
3+
fvm install $FLUTTER_VERSION
4+
fvm global $FLUTTER_VERSION
5+
flutter --version
6+
flutter doctor

src/serious_python/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.8.5
2+
3+
* Added Java `loadLibrary` to Android plugin to support `pyjnius` ([#128](https://github.com/flet-dev/serious-python/issues/128)).
4+
15
## 0.8.4
26

37
* Copy `site-packages/flutter` contents to `SERIOUS_PYTHON_FLUTTER_PACKAGES`.
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
cc3d88a7793723d9bdec03813d7a47d758276bef6e78107138fd0acb7d386fca
1+
43f8a7b00e44a09647dda36299259976950ba7259c5060de20677c449690fc73

src/serious_python/example/flet_example/pubspec.lock

+6-6
Original file line numberDiff line numberDiff line change
@@ -482,42 +482,42 @@ packages:
482482
path: "../.."
483483
relative: true
484484
source: path
485-
version: "0.8.2"
485+
version: "0.8.4"
486486
serious_python_android:
487487
dependency: transitive
488488
description:
489489
path: "../../../serious_python_android"
490490
relative: true
491491
source: path
492-
version: "0.8.2"
492+
version: "0.8.4"
493493
serious_python_darwin:
494494
dependency: transitive
495495
description:
496496
path: "../../../serious_python_darwin"
497497
relative: true
498498
source: path
499-
version: "0.8.2"
499+
version: "0.8.4"
500500
serious_python_linux:
501501
dependency: transitive
502502
description:
503503
path: "../../../serious_python_linux"
504504
relative: true
505505
source: path
506-
version: "0.8.2"
506+
version: "0.8.4"
507507
serious_python_platform_interface:
508508
dependency: transitive
509509
description:
510510
path: "../../../serious_python_platform_interface"
511511
relative: true
512512
source: path
513-
version: "0.8.2"
513+
version: "0.8.4"
514514
serious_python_windows:
515515
dependency: transitive
516516
description:
517517
path: "../../../serious_python_windows"
518518
relative: true
519519
source: path
520-
version: "0.8.2"
520+
version: "0.8.4"
521521
shared_preferences:
522522
dependency: transitive
523523
description:
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
86fcefd284f101b084bfb8b72d9eebe97d65c880b1dc97a9b8719ed40c4a3e24
1+
081229a0c5551f4c29faa2b6e8a23cba9e971d66f95565892561898e146a5d5e

src/serious_python/example/run_example/app/src/main.py

+33-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
print("Hello from Python program!")
22

3-
import _imp
43
import os
54
from pathlib import Path
65
from time import sleep
76

7+
import _imp
8+
89
_imp.extension_suffixes()
910

1011
print("HELLO!")
@@ -94,12 +95,14 @@ def test_sqlite():
9495
out_dir = Path(result_filename).parent
9596
conn = sqlite3.connect(str(out_dir.joinpath("mydb.db")))
9697

97-
conn.execute("""CREATE TABLE COMPANY
98+
conn.execute(
99+
"""CREATE TABLE COMPANY
98100
(ID INT PRIMARY KEY NOT NULL,
99101
NAME TEXT NOT NULL,
100102
AGE INT NOT NULL,
101103
ADDRESS CHAR(50),
102-
SALARY REAL);""")
104+
SALARY REAL);"""
105+
)
103106

104107
conn.execute(
105108
"INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \
@@ -131,7 +134,34 @@ def test_sqlite():
131134
return f"\nsqlite: test_basic - error: {e}"
132135

133136

137+
def test_pyjnius():
138+
from time import sleep
139+
140+
from jnius import autoclass
141+
142+
activity = autoclass(os.getenv("MAIN_ACTIVITY_HOST_CLASS_NAME")).mActivity
143+
Secure = autoclass("android.provider.Settings$Secure")
144+
145+
version = autoclass("android.os.Build$VERSION")
146+
os_build = autoclass("android.os.Build")
147+
base_os = version.BASE_OS
148+
149+
DisplayMetrics = autoclass("android.util.DisplayMetrics")
150+
metrics = DisplayMetrics()
151+
152+
return (
153+
str(activity.getClass().getName())
154+
+ " os: "
155+
+ str(os_build)
156+
+ " FLET_JNI_READY: "
157+
+ str(os.getenv("FLET_JNI_READY"))
158+
+ " DPI: "
159+
+ str(metrics.getDeviceDensity())
160+
)
161+
162+
134163
r += test_sqlite()
164+
r += test_pyjnius()
135165
# test_lru()
136166
# test_numpy_basic()
137167
# test_numpy_performance()
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
#numpy
2-
lru-dict
2+
lru-dict
3+
pyjnius

src/serious_python/example/run_example/lib/main.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class _MyAppState extends State<MyApp> {
7676
title: const Text('Serious Python example app'),
7777
),
7878
body: Center(
79-
child: Text(_pyResult),
79+
child: SelectableText(_pyResult),
8080
),
8181
),
8282
);

src/serious_python/example/run_example/pubspec.lock

+6-6
Original file line numberDiff line numberDiff line change
@@ -296,42 +296,42 @@ packages:
296296
path: "../.."
297297
relative: true
298298
source: path
299-
version: "0.8.1"
299+
version: "0.8.4"
300300
serious_python_android:
301301
dependency: transitive
302302
description:
303303
path: "../../../serious_python_android"
304304
relative: true
305305
source: path
306-
version: "0.8.1"
306+
version: "0.8.4"
307307
serious_python_darwin:
308308
dependency: transitive
309309
description:
310310
path: "../../../serious_python_darwin"
311311
relative: true
312312
source: path
313-
version: "0.8.1"
313+
version: "0.8.4"
314314
serious_python_linux:
315315
dependency: transitive
316316
description:
317317
path: "../../../serious_python_linux"
318318
relative: true
319319
source: path
320-
version: "0.8.1"
320+
version: "0.8.4"
321321
serious_python_platform_interface:
322322
dependency: transitive
323323
description:
324324
path: "../../../serious_python_platform_interface"
325325
relative: true
326326
source: path
327-
version: "0.8.1"
327+
version: "0.8.4"
328328
serious_python_windows:
329329
dependency: transitive
330330
description:
331331
path: "../../../serious_python_windows"
332332
relative: true
333333
source: path
334-
version: "0.8.1"
334+
version: "0.8.4"
335335
shelf:
336336
dependency: transitive
337337
description:

src/serious_python/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: serious_python
22
description: A cross-platform plugin for adding embedded Python runtime to your Flutter apps.
33
homepage: https://flet.dev
44
repository: https://github.com/flet-dev/serious-python
5-
version: 0.8.4
5+
version: 0.8.5
66

77
platforms:
88
ios:

src/serious_python_android/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.8.5
2+
3+
* Added Java `loadLibrary` to Android plugin to support `pyjnius` ([#128](https://github.com/flet-dev/serious-python/issues/128)).
4+
15
## 0.8.4
26

37
* Copy `site-packages/flutter` contents to `SERIOUS_PYTHON_FLUTTER_PACKAGES`.

src/serious_python_android/android/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
group 'com.flet.serious_python_android'
2-
version '0.8.4'
2+
version '0.8.5'
33

44
def python_version = '3.12'
55

src/serious_python_android/android/src/main/java/com/flet/serious_python_android/AndroidPlugin.java

+37-2
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
package com.flet.serious_python_android;
2-
2+
import java.lang.*;
33
import android.content.Context;
44
import android.content.ContextWrapper;
55
import androidx.annotation.NonNull;
66
import android.system.Os;
7+
import android.content.Intent;
8+
import android.app.Activity;
79

810
import io.flutter.embedding.engine.plugins.FlutterPlugin;
11+
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
12+
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
913
import io.flutter.plugin.common.MethodCall;
1014
import io.flutter.plugin.common.MethodChannel;
1115
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
1216
import io.flutter.plugin.common.MethodChannel.Result;
1317

1418
/** AndroidPlugin */
15-
public class AndroidPlugin implements FlutterPlugin, MethodCallHandler {
19+
public class AndroidPlugin implements FlutterPlugin, MethodCallHandler, ActivityAware {
20+
21+
public static final String MAIN_ACTIVITY_HOST_CLASS_NAME = "MAIN_ACTIVITY_HOST_CLASS_NAME";
22+
public static Activity mActivity = null;
23+
1624
/// The MethodChannel that will the communication between Flutter and native
1725
/// Android
1826
///
@@ -30,6 +38,16 @@ public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBindin
3038
this.context = flutterPluginBinding.getApplicationContext();
3139
}
3240

41+
@Override
42+
public void onAttachedToActivity(@NonNull ActivityPluginBinding activityPluginBinding) {
43+
mActivity = activityPluginBinding.getActivity();
44+
try {
45+
Os.setenv(MAIN_ACTIVITY_HOST_CLASS_NAME, this.getClass().getCanonicalName(), true);
46+
} catch (Exception e) {
47+
// nothing to do
48+
}
49+
}
50+
3351
@Override
3452
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
3553
if (call.method.equals("getPlatformVersion")) {
@@ -38,6 +56,13 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
3856
ContextWrapper contextWrapper = new ContextWrapper(context);
3957
String nativeLibraryDir = contextWrapper.getApplicationInfo().nativeLibraryDir;
4058
result.success(nativeLibraryDir);
59+
} else if (call.method.equals("loadLibrary")) {
60+
try {
61+
System.loadLibrary(call.argument("libname"));
62+
result.success(null);
63+
} catch (Throwable e) {
64+
result.error("Error", e.getMessage(), null);
65+
}
4166
} else if (call.method.equals("setEnvironmentVariable")) {
4267
String name = call.argument("name");
4368
String value = call.argument("value");
@@ -56,4 +81,14 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
5681
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
5782
channel.setMethodCallHandler(null);
5883
}
84+
85+
@Override
86+
public void onDetachedFromActivity() {}
87+
88+
@Override
89+
public void onDetachedFromActivityForConfigChanges() {}
90+
91+
@Override
92+
public void onReattachedToActivityForConfigChanges(ActivityPluginBinding binding) {}
93+
5994
}

0 commit comments

Comments
 (0)