Skip to content

Commit

Permalink
feat: Use cronet client on android (Support device certificates) (#544)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomBursch authored Nov 15, 2024
1 parent 53b0b92 commit e8a694f
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 4 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ jobs:
asset_content_type: application/gzip
- os: ubuntu-latest
target: Android
build_target: apk
build_target: apk --dart-define=cronetHttpNoPlay=true
build_path: kitchenowl/build/app/outputs/flutter-apk
asset_extension: .apk
asset_content_type: application/vnd.android.package-archive
Expand Down Expand Up @@ -147,8 +147,7 @@ jobs:
# Checkout code, recreate missing files, and get packages.
- name: Checkout code
uses: actions/checkout@v2
- run: flutter create .
- run: flutter pub get
- run: flutter packages get

- name: Configure Keystore for Android
if: matrix.target == 'Android'
Expand Down
20 changes: 19 additions & 1 deletion kitchenowl/lib/services/api/api_service.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import 'dart:convert';
import 'dart:io';
import 'package:cronet_http/cronet_http.dart';
import 'package:flutter/foundation.dart';
import 'package:http/http.dart' as http;
import 'package:http/io_client.dart';
import 'package:kitchenowl/config.dart';
import 'package:kitchenowl/helpers/named_bytearray.dart';
import 'package:kitchenowl/models/household.dart';
Expand Down Expand Up @@ -49,7 +52,7 @@ class ApiService {
String householdPath(Household household) => "/household/${household.id}";

static ApiService? _instance;
final _client = http.Client();
late final http.Client _client;
late final Socket socket;
final String baseUrl;
String? _refreshToken;
Expand All @@ -68,6 +71,21 @@ class ApiService {
ApiService._internal(String baseUrl)
: baseUrl = baseUrl.isNotEmpty ? baseUrl + _API_PATH : "" {
_connectionNotifier.value = Connection.undefined;
if (!kIsWeb && Platform.isAndroid) {
final engine = CronetEngine.build(
cacheMode: CacheMode.memory,
cacheMaxSize: 2 * 1024 * 1024,
userAgent:
"KitchenOwl-${Platform.operatingSystem}/${Config.packageInfoSync?.version}",
);
_client = CronetClient.fromCronetEngine(engine, closeEngine: true);
} else if (!kIsWeb) {
_client = IOClient(HttpClient()
..userAgent =
"KitchenOwl-${Platform.operatingSystem}/${Config.packageInfoSync?.version}");
} else {
_client = http.Client();
}
socket = io(
baseUrl,
OptionBuilder()
Expand Down
1 change: 1 addition & 0 deletions kitchenowl/linux/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ list(APPEND FLUTTER_PLUGIN_LIST
)

list(APPEND FLUTTER_FFI_PLUGIN_LIST
jni
)

set(PLUGIN_BUNDLED_LIBRARIES)
Expand Down
32 changes: 32 additions & 0 deletions kitchenowl/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.18.0"
cronet_http:
dependency: "direct main"
description:
name: cronet_http
sha256: "3af9c4d57bf07ef4b307e77b22be4ad61bea19ee6ff65e62184863f3a09f1415"
url: "https://pub.dev"
source: hosted
version: "1.3.2"
cross_file:
dependency: transitive
description:
Expand Down Expand Up @@ -511,6 +519,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "4.0.2"
http_profile:
dependency: transitive
description:
name: http_profile
sha256: "7e679e355b09aaee2ab5010915c932cce3f2d1c11c3b2dc177891687014ffa78"
url: "https://pub.dev"
source: hosted
version: "0.1.0"
icons_launcher:
dependency: "direct dev"
description:
Expand Down Expand Up @@ -604,6 +620,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.19.0"
jni:
dependency: transitive
description:
name: jni
sha256: f377c585ea9c08d48b427dc2e03780af2889d1bb094440da853c6883c1acba4b
url: "https://pub.dev"
source: hosted
version: "0.10.1"
js:
dependency: transitive
description:
Expand Down Expand Up @@ -708,6 +732,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.0"
package_config:
dependency: transitive
description:
name: package_config
sha256: "1c5b77ccc91e4823a5af61ee74e6b972db1ef98c2ff5a18d3161c982a55448bd"
url: "https://pub.dev"
source: hosted
version: "2.1.0"
package_info_plus:
dependency: "direct main"
description:
Expand Down
1 change: 1 addition & 0 deletions kitchenowl/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ dependencies:
package_info_plus: ^8.0.0
device_info_plus: ^11.1.0
http: ^1.1.2
cronet_http: ^1.3.2
diffutil_dart: ^4.0.0

responsive_builder: ^0.7.0
Expand Down
1 change: 1 addition & 0 deletions kitchenowl/windows/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ list(APPEND FLUTTER_PLUGIN_LIST
)

list(APPEND FLUTTER_FFI_PLUGIN_LIST
jni
)

set(PLUGIN_BUNDLED_LIBRARIES)
Expand Down

0 comments on commit e8a694f

Please sign in to comment.