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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.2.3

* Android fixes to make it work on some devices when installed via Play Store.

## 0.2.2

* Exclude `x86` from supported ABIs.
Expand Down
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group 'com.flet.serious_python'
version '0.2.2'
version '0.2.3'

buildscript {
repositories {
Expand Down
16 changes: 7 additions & 9 deletions example/flask_example/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
# flask_example

A new Flutter project.
Important: to make `serious_python` work in your own Android app:

## Getting Started
If you build an App Bundle Edit `android/gradle.properties` and add the flag:

This project is a starting point for a Flutter application.
```
android.bundle.enableUncompressedNativeLibs=false
```

A few resources to get you started if this is your first Flutter project:

- [Lab: Write your first Flutter app](https://docs.flutter.dev/get-started/codelab)
- [Cookbook: Useful Flutter samples](https://docs.flutter.dev/cookbook)
If you build an APK Make sure `android/app/src/AndroidManifest.xml` has `android:extractNativeLibs="true"` in the `<application>` tag.

For help getting started with Flutter development, view the
[online documentation](https://docs.flutter.dev/), which offers tutorials,
samples, guidance on mobile development, and a full API reference.
For more information, see the [public issue](https://issuetracker.google.com/issues/147096055).
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
<application
android:label="flask_example"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
android:icon="@mipmap/ic_launcher"
android:extractNativeLibs="true">
<activity
android:name=".MainActivity"
android:exported="true"
Expand Down
1 change: 1 addition & 0 deletions example/flask_example/android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true
android.enableJetifier=true
android.bundle.enableUncompressedNativeLibs=false
2 changes: 1 addition & 1 deletion example/flask_example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ packages:
path: "../.."
relative: true
source: path
version: "0.2.2"
version: "0.2.3"
sky_engine:
dependency: transitive
description: flutter
Expand Down
16 changes: 7 additions & 9 deletions example/flet_example/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
# flet_example

A new Flutter project.
Important: to make `serious_python` work in your own Android app:

## Getting Started
If you build an App Bundle Edit `android/gradle.properties` and add the flag:

This project is a starting point for a Flutter application.
```
android.bundle.enableUncompressedNativeLibs=false
```

A few resources to get you started if this is your first Flutter project:

- [Lab: Write your first Flutter app](https://docs.flutter.dev/get-started/codelab)
- [Cookbook: Useful Flutter samples](https://docs.flutter.dev/cookbook)
If you build an APK Make sure `android/app/src/AndroidManifest.xml` has `android:extractNativeLibs="true"` in the `<application>` tag.

For help getting started with Flutter development, view the
[online documentation](https://docs.flutter.dev/), which offers tutorials,
samples, guidance on mobile development, and a full API reference.
For more information, see the [public issue](https://issuetracker.google.com/issues/147096055).
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
<application
android:label="flet_example"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
android:icon="@mipmap/ic_launcher"
android:extractNativeLibs="true">
<activity
android:name=".MainActivity"
android:exported="true"
Expand Down
1 change: 1 addition & 0 deletions example/flet_example/android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true
android.enableJetifier=true
android.bundle.enableUncompressedNativeLibs=false
2 changes: 1 addition & 1 deletion example/flet_example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ packages:
path: "../.."
relative: true
source: path
version: "0.2.2"
version: "0.2.3"
shake:
dependency: transitive
description:
Expand Down
2 changes: 1 addition & 1 deletion ios/serious_python.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
Pod::Spec.new do |s|
s.name = 'serious_python'
s.version = '0.2.2'
s.version = '0.2.3'
s.summary = 'A cross-platform plugin for adding embedded Python runtime to your Flutter apps.'
s.description = <<-DESC
A cross-platform plugin for adding embedded Python runtime to your Flutter apps.
Expand Down
59 changes: 46 additions & 13 deletions lib/src/android/serious_python_android.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,14 @@ class SeriousPythonAndroid extends SeriousPythonPlatform {
await methodChannel.invokeMethod<String>('getNativeLibraryDir');
debugPrint("getNativeLibraryDir: $nativeLibraryDir");

var pythonLibPath = await extractFileZip(
"$nativeLibraryDir/libpythonbundle.so",
targetPath: "python_bundle");
var bundlePath = "$nativeLibraryDir/libpythonbundle.so";

if (!await File(bundlePath).exists()) {
throw Exception("Python bundle not found: $bundlePath");
}

var pythonLibPath =
await extractFileZip(bundlePath, targetPath: "python_bundle");

debugPrint("pythonLibPath: $pythonLibPath");

Expand All @@ -60,8 +65,11 @@ class SeriousPythonAndroid extends SeriousPythonPlatform {
receivePort.close();
isolate.kill();

var r2 = File("$pythonLibPath/out.txt").readAsStringSync();
debugPrint("Result out.txt: $r2");
var out = File("out.txt");
if (out.existsSync()) {
var r = out.readAsStringSync();
debugPrint("Result from out.txt: $r");
}
});
} else {
// sync run
Expand Down Expand Up @@ -121,7 +129,16 @@ void runPythonProgram(List<Object> arguments) async {
var programDirPath = p.dirname(pythonProgramPath);
var programModuleName = p.basenameWithoutExtension(pythonProgramPath);

var loadLynamicLibraries = [
var programDirPathFiles = await getDirFiles(programDirPath);
var pythonLibPathFiles = await getDirFiles(pythonLibPath);

debugPrint("programDirPath: $programDirPath");
debugPrint("programModuleName: $programModuleName");
debugPrint("pythonLibPath: $pythonLibPath");
debugPrint("programDirPathFiles: $programDirPathFiles");
debugPrint("pythonLibPathFiles: $pythonLibPathFiles");

var loadDynamicLibraries = [
"libffi.so",
"libcrypto1.1.so",
"libsqlite3.so",
Expand All @@ -138,13 +155,8 @@ void runPythonProgram(List<Object> arguments) async {
"$pythonLibPath/stdlib.zip"
];

var currentDir = pythonLibPath;

// set current dir
Directory.current = currentDir;

// load dynamic libraries
for (var loadDynamicLibrary in loadLynamicLibraries) {
for (var loadDynamicLibrary in loadDynamicLibraries) {
DynamicLibrary.open(loadDynamicLibrary);
}

Expand Down Expand Up @@ -208,7 +220,18 @@ void runPythonProgram(List<Object> arguments) async {

// run user program
final moduleNamePtr = programModuleName.toNativeUtf8();
cpython.PyImport_ImportModule(moduleNamePtr.cast<Char>());
var modulePtr = cpython.PyImport_ImportModule(moduleNamePtr.cast<Char>());
if (modulePtr == nullptr) {
final pType =
calloc.allocate<Pointer<PyObject>>(sizeOf<Pointer<PyObject>>());
final pValue =
calloc.allocate<Pointer<PyObject>>(sizeOf<Pointer<PyObject>>());
final pTrace =
calloc.allocate<Pointer<PyObject>>(sizeOf<Pointer<PyObject>>());
cpython.PyErr_Fetch(pType, pValue, pTrace);
cpython.PyErr_NormalizeException(pType, pValue, pTrace);
cpython.PyErr_Display(pType.value, pValue.value, pTrace.value);
}
// final pythonCodePtr = pythonCode.toNativeUtf8();
// int r = dartpyc.PyRun_SimpleString(pythonCodePtr.cast<Char>());
// debugPrint("PyRun_SimpleString result: $r");
Expand All @@ -218,3 +241,13 @@ void runPythonProgram(List<Object> arguments) async {
debugPrint("after Py_Finalize");
sendPort.send("Python program exited");
}

Future<String> getDirFiles(String path, {bool recursive = false}) async {
final dir = Directory(path);
if (!await dir.exists()) {
return "<not found>";
}
return (await dir.list(recursive: recursive).toList())
.map((file) => file.path)
.join('\n');
}
16 changes: 11 additions & 5 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,17 @@ Future<String> extractAssetOrFile(String path,
// unpack from asset or file
debugPrint("Start unpacking app archive");
List<int> data;
if (isAsset) {
final bytes = await rootBundle.load(path);
data = bytes.buffer.asUint8List();
} else {
data = await File(path).readAsBytes();

try {
if (isAsset) {
final bytes = await rootBundle.load(path);
data = bytes.buffer.asUint8List();
} else {
data = await File(path).readAsBytes();
}
} catch (_) {
await destDir.delete(recursive: true);
rethrow;
}

Archive archive = ZipDecoder().decodeBytes(data);
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: serious_python
description: A cross-platform plugin for adding embedded Python runtime to your Flutter apps.
homepage: https://flet.dev
repository: https://github.com/flet-dev/serious-python
version: 0.2.2
version: 0.2.3

platforms:
ios:
Expand Down