Skip to content

Commit 6e0a46f

Browse files
authored
[cronet_http] 🏗️ Use dart-define to determine dependency (#1111)
1 parent d5cab74 commit 6e0a46f

File tree

7 files changed

+65
-205
lines changed

7 files changed

+65
-205
lines changed

.github/workflows/cronet.yml

+6-12
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ jobs:
2626
runs-on: macos-latest
2727
strategy:
2828
matrix:
29-
package: ['cronet_http', 'cronet_http_embedded']
29+
cronetHttpNoPlay: ['false', 'true']
30+
defaults:
31+
run:
32+
working-directory: pkgs/cronet_http
3033
steps:
3134
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
3235
- uses: actions/setup-java@387ac29b308b003ca37ba93a6cab5eb57c8f5f93
@@ -36,23 +39,14 @@ jobs:
3639
- uses: subosito/flutter-action@2783a3f08e1baf891508463f8c6653c258246225
3740
with:
3841
channel: 'stable'
39-
- name: Make cronet_http_embedded copy
40-
if: ${{ matrix.package == 'cronet_http_embedded' }}
41-
run: |
42-
mv pkgs/cronet_http pkgs/cronet_http_embedded
43-
cd pkgs/cronet_http_embedded
44-
flutter pub get && dart tool/prepare_for_embedded.dart
4542
- id: install
4643
name: Install dependencies
47-
working-directory: 'pkgs/${{ matrix.package }}'
4844
run: flutter pub get
4945
- name: Check formatting
5046
if: always() && steps.install.outcome == 'success'
51-
working-directory: 'pkgs/${{ matrix.package }}'
5247
run: dart format --output=none --set-exit-if-changed .
5348
- name: Analyze code
5449
if: always() && steps.install.outcome == 'success'
55-
working-directory: 'pkgs/${{ matrix.package }}'
5650
run: flutter analyze --fatal-infos
5751
- name: Run tests
5852
uses: reactivecircus/android-emulator-runner@6b0df4b0efb23bb0ec63d881db79aefbc976e4b2
@@ -64,6 +58,6 @@ jobs:
6458
# - pkgs/cronet_http/example/android/app/build.gradle
6559
api-level: 21
6660
arch: x86_64
67-
target: ${{ matrix.package == 'cronet_http_embedded' && 'default' || 'google_apis' }}
61+
target: ${{ matrix.cronetHttpNoPlay == 'true' && 'default' || 'google_apis' }}
6862
profile: pixel
69-
script: cd pkgs/${{ matrix.package }}/example && flutter test --timeout=1200s integration_test/
63+
script: cd pkgs/cronet_http/example && flutter test --dart-define=cronetHttpNoPlay=${{ matrix.cronetHttpNoPlay }} --timeout=1200s integration_test/

pkgs/cronet_http/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.2.0-wip
2+
3+
* Support the Cronet embedding dependency with `--dart-define=cronetHttpNoPlay=true`.
4+
15
## 1.1.1
26

37
* Make it possible to construct `CronetClient` with custom a `CronetEngine`

pkgs/cronet_http/README.md

+38-17
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,20 @@
22
[![package publisher](https://img.shields.io/pub/publisher/cronet_http.svg)](https://pub.dev/packages/cronet_http/publisher)
33

44
An Android Flutter plugin that provides access to the
5-
[Cronet][]
6-
HTTP client.
5+
[Cronet][] HTTP client.
76

8-
Cronet is available as part of
9-
[Google Play Services][].
7+
Cronet is available as part of [Google Play Services][]
8+
and as [a standalone embedded library][].
109

11-
This package depends on [Google Play Services][] for its [Cronet][]
12-
implementation.
13-
[`package:cronet_http_embedded`](https://pub.dev/packages/cronet_http_embedded)
14-
is functionally identical to this package but embeds [Cronet][] directly
15-
instead of relying on [Google Play Services][].
10+
This package depends on [Google Play Services][]
11+
for its [Cronet][] implementation.
12+
To use the embedded version of [Cronet][] without [Google Play Services][],
13+
see [Use embedded Cronet](#use-embedded-cronet).
1614

1715
## Motivation
1816

19-
Using [Cronet][], rather than the socket-based [dart:io HttpClient][]
20-
implemententation, has several advantages:
17+
Using [Cronet][], rather than the socket-based
18+
[dart:io HttpClient][] implementation, has several advantages:
2119

2220
1. It automatically supports Android platform features such as HTTP proxies.
2321
2. It supports configurable caching.
@@ -40,23 +38,46 @@ void main() async {
4038
final Client httpClient;
4139
if (Platform.isAndroid) {
4240
final engine = CronetEngine.build(
43-
cacheMode: CacheMode.memory,
44-
cacheMaxSize: 2 * 1024 * 1024,
45-
userAgent: 'Book Agent');
41+
cacheMode: CacheMode.memory,
42+
cacheMaxSize: 2 * 1024 * 1024,
43+
userAgent: 'Book Agent',
44+
);
4645
httpClient = CronetClient.fromCronetEngine(engine, isOwned: true);
4746
} else {
4847
httpClient = IOClient(HttpClient()..userAgent = 'Book Agent');
4948
}
5049
51-
final response = await client.get(Uri.https(
50+
final response = await client.get(
51+
Uri.https(
5252
'www.googleapis.com',
5353
'/books/v1/volumes',
54-
{'q': 'HTTP', 'maxResults': '40', 'printType': 'books'}));
54+
{'q': 'HTTP', 'maxResults': '40', 'printType': 'books'},
55+
),
56+
);
5557
httpClient.close();
5658
}
5759
```
5860

61+
### Use embedded Cronet
62+
63+
If you want your application to work without [Google Play Services][],
64+
you can instead depend on the `org.chromium.net:cronet-embedded` package
65+
by using `dart-define` to set `cronetHttpNoPlay` is set to `true`.
66+
67+
For example:
68+
69+
```
70+
flutter run --dart-define=cronetHttpNoPlay=true
71+
```
72+
73+
To use the embedded version in `flutter test`:
74+
75+
```
76+
flutter test --dart-define=cronetHttpNoPlay=true
77+
```
78+
5979
[Cronet]: https://developer.android.com/guide/topics/connectivity/cronet/reference/org/chromium/net/package-summary
60-
[dart:io HttpClient]: https://api.dart.dev/stable/dart-io/HttpClient-class.html
6180
[Google Play Services]: https://developers.google.com/android/guides/overview
81+
[a standalone embedded library]: https://mvnrepository.com/artifact/org.chromium.net/cronet-embedded
82+
[dart:io HttpClient]: https://api.dart.dev/stable/dart-io/HttpClient-class.html
6283
[package:http Client]: https://pub.dev/documentation/http/latest/http/Client-class.html

pkgs/cronet_http/README_EMBEDDED.md

-12
This file was deleted.

pkgs/cronet_http/android/build.gradle

+16-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ rootProject.allprojects {
2121
}
2222
}
2323

24+
def dartDefines = [
25+
cronetHttpNoPlay: 'false'
26+
]
27+
if (project.hasProperty('dart-defines')) {
28+
def defines = project.property('dart-defines').split(',').collectEntries { entry ->
29+
def pair = new String(entry.decodeBase64(), 'UTF-8').split('=')
30+
[(pair.first()): pair.last()]
31+
}
32+
dartDefines = dartDefines + defines
33+
}
34+
2435
apply plugin: 'com.android.library'
2536
apply plugin: 'kotlin-android'
2637

@@ -65,5 +76,9 @@ android {
6576
}
6677

6778
dependencies {
68-
implementation "com.google.android.gms:play-services-cronet:18.0.1"
79+
if (dartDefines.cronetHttpNoPlay == 'true') {
80+
implementation 'org.chromium.net:cronet-embedded:113.5672.61'
81+
} else {
82+
implementation "com.google.android.gms:play-services-cronet:18.0.1"
83+
}
6984
}

pkgs/cronet_http/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: cronet_http
2-
version: 1.1.1
2+
version: 1.2.0-wip
33
description: >-
44
An Android Flutter plugin that provides access to the Cronet HTTP client.
55
repository: https://github.com/dart-lang/http/tree/master/pkgs/cronet_http

pkgs/cronet_http/tool/prepare_for_embedded.dart

-162
This file was deleted.

0 commit comments

Comments
 (0)