Skip to content
This repository was archived by the owner on Feb 11, 2024. It is now read-only.

Commit c2fb111

Browse files
Flutter support Android, Linux, and Windows 64 bit (#18)
1 parent 3fe163a commit c2fb111

File tree

84 files changed

+2587
-62
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+2587
-62
lines changed

.github/workflows/test-package.yml

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ env:
1818

1919
jobs:
2020
# Check code formatting and static analysis on a single OS (linux)
21-
# against Dart stable.
21+
# against Flutter stable.
2222
analyze:
2323
runs-on: ubuntu-latest
2424
strategy:
@@ -27,17 +27,20 @@ jobs:
2727
sdk: [stable]
2828
steps:
2929
- uses: actions/checkout@v2
30-
- uses: dart-lang/setup-dart@v1.0
30+
- uses: actions/setup-java@v1
3131
with:
32-
sdk: ${{ matrix.sdk }}
32+
java-version: '12.x'
33+
- uses: subosito/flutter-action@v1
34+
with:
35+
channel: ${{ matrix.sdk }}
3336
- id: install
3437
name: Install dependencies
35-
run: dart pub get
38+
run: flutter pub get
3639
- name: Check formatting
37-
run: dart format --output=none --set-exit-if-changed .
40+
run: flutter format --set-exit-if-changed .
3841
if: always() && steps.install.outcome == 'success'
3942
- name: Analyze code
40-
run: dart analyze --fatal-infos
43+
run: flutter analyze --fatal-infos
4144
if: always() && steps.install.outcome == 'success'
4245

4346
# Run tests on a matrix consisting of two dimensions:
@@ -51,18 +54,22 @@ jobs:
5154
matrix:
5255
# Add macos-latest and/or windows-latest if relevant for this package.
5356
os: [ubuntu-latest, macos-latest, windows-latest]
54-
sdk: [2.13.0, dev]
57+
sdk: [stable, dev]
5558
steps:
5659
- uses: actions/checkout@v2
57-
- uses: dart-lang/[email protected]
60+
- uses: actions/setup-java@v1
61+
with:
62+
java-version: '12.x'
63+
- uses: subosito/flutter-action@v1
5864
with:
59-
sdk: ${{ matrix.sdk }}
65+
channel: ${{ matrix.sdk }}
6066
- id: install
6167
name: Install dependencies
62-
run: dart pub get
68+
run: flutter pub get
6369
- id: get_binaries
6470
name: Download/Build Cronet Binaries
65-
run: dart run cronet:setup
71+
run: flutter pub run cronet:setup
72+
if: always() && steps.install.outcome == 'success'
6673
- name: Run VM tests
67-
run: dart test --platform vm
74+
run: flutter test
6875
if: always() && steps.install.outcome == 'success'

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ doc/api/
2727
.history
2828
.vscode
2929
compile_commands.json
30+
*.log
3031

3132
# IntelliJ
3233
*.iml

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.0.4
2+
3+
* Added support for Android and Flutter Desktops (Windows/Linux).
4+
15
## 0.0.3
26

37
* Using `package:args` for handling CLI arguments.

README.md

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,37 @@ This is a [GSoC 2021 project](https://summerofcode.withgoogle.com/projects/#4757
66

77
## Supported Platforms
88

9-
Currently, 64 bit Desktop Platforms (Linux, Windows and MacOS) are supported.
9+
Currently, 64 bit Android and Desktop Platforms (Linux, Windows and MacOS) are supported.
1010

1111
## Requirements
1212

1313
1. Dart SDK 2.12.0 or above.
14-
2. CMake 3.15 or above. (If on windows, Visual Studio 2019 with C++ tools)
14+
2. CMake 3.10 or above. (If on windows, Visual Studio 2019 with C++ tools)
1515
3. C++ compiler. (g++/clang/msvc)
16+
4. Android NDK if targeting Android.
1617

1718
## Usage
1819

1920
1. Add package as a dependency in your `pubspec.yaml`.
2021

2122
2. Run this from the `root` of your project.
2223

23-
**Dart CLI**
24-
2524
```bash
26-
dart pub get
27-
dart run cronet:setup # Downloads the cronet binaries.
25+
flutter pub get
26+
flutter pub run cronet:setup # Downloads the cronet binaries.
27+
```
28+
29+
We need to use `flutter pub` even if we want to use it with Dart CLI. See <https://github.com/dart-lang/pub/issues/2606> for further details.
30+
31+
***Note for Android:** Remember to Add the following permissions in `AndroidManifest.xml` file.
32+
33+
```xml
34+
<uses-permission android:name="android.permission.INTERNET" />
35+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
2836
```
2937

38+
Optionally, enable cleartext traffic by adding `android:usesCleartextTraffic="true"` to `AndroidManifest.xml` file.
39+
3040
3. Import
3141

3242
```dart
@@ -56,22 +66,34 @@ Currently, 64 bit Desktop Platforms (Linux, Windows and MacOS) are supported.
5666

5767
## Run Example
5868

69+
### Flutter
70+
5971
```bash
60-
cd example
61-
dart run cronet:setup # Downloads the cronet binaries.
62-
dart run
72+
cd example/flutter
73+
flutter pub get
74+
flutter pub run cronet:setup # Downloads the cronet binaries.
75+
flutter run
76+
```
77+
78+
### Dart CLI
79+
80+
```bash
81+
cd example/cli
82+
flutter pub get
83+
flutter pub run cronet:setup # Downloads the cronet binaries.
84+
dart run bin/example_dart.dart
6385
```
6486

6587
## Run Tests
6688

6789
```bash
68-
dart pub get
69-
dart run cronet:setup # Downloads the cronet binaries.
70-
dart test --platform vm
90+
flutter pub get
91+
flutter pub run cronet:setup # Downloads the cronet binaries.
92+
flutter test
7193
```
7294

7395
You can also verify your cronet binaries using `dart run cronet:setup verify`.
74-
Make sure to have `cmake 3.15`.
96+
Make sure to have `cmake 3.10`.
7597

7698
## Benchmarking
7799

@@ -98,3 +120,5 @@ Note: Test results may get affected by: <https://github.com/google/cronet.dart/i
98120
2. Run `dart run cronet:setup build` from the root of your project.
99121

100122
**Note for Windows:** Run `step 2` from `x64 Native Tools Command Prompt for VS 2019` shell.
123+
124+
**Note for Android:** Copy the produced jar files in `android/libs` and `.so` files in `android/src/main/jniLibs` subdirectory from the root of this package.

android/.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/workspace.xml
5+
/.idea/libraries
6+
.DS_Store
7+
/build
8+
/captures

android/build.gradle

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
group 'dev.google.cronet'
2+
version '1.0-SNAPSHOT'
3+
4+
buildscript {
5+
ext.kotlin_version = '1.3.50'
6+
repositories {
7+
google()
8+
jcenter()
9+
}
10+
11+
dependencies {
12+
classpath 'com.android.tools.build:gradle:4.1.0'
13+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
14+
}
15+
}
16+
17+
rootProject.allprojects {
18+
repositories {
19+
google()
20+
jcenter()
21+
}
22+
}
23+
24+
apply plugin: 'com.android.library'
25+
apply plugin: 'kotlin-android'
26+
27+
android {
28+
compileSdkVersion 30
29+
30+
sourceSets {
31+
main.java.srcDirs += 'src/main/kotlin'
32+
}
33+
34+
externalNativeBuild {
35+
// Encapsulates your CMake build configurations.
36+
cmake {
37+
// Provides a relative path to your CMake build script.
38+
path "../src/CMakeLists.txt"
39+
}
40+
}
41+
42+
defaultConfig {
43+
minSdkVersion 16
44+
}
45+
}
46+
47+
dependencies {
48+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
49+
implementation files(fileTree(dir: 'libs', includes: ['*.jar']))
50+
}

android/gradle.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
org.gradle.jvmargs=-Xmx1536M
2+
android.useAndroidX=true
3+
android.enableJetifier=true
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
zipStoreBase=GRADLE_USER_HOME
4+
zipStorePath=wrapper/dists
5+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip

android/settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rootProject.name = 'cronet'

android/src/main/AndroidManifest.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="dev.google.cronet">
3+
<!-- Flutter Boilerplate Modifications: Internet & network permissions-->
4+
<uses-permission android:name="android.permission.INTERNET" />
5+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
6+
</manifest>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package dev.google.cronet
2+
3+
import androidx.annotation.NonNull
4+
5+
import io.flutter.embedding.engine.plugins.FlutterPlugin
6+
import io.flutter.plugin.common.MethodCall
7+
import io.flutter.plugin.common.MethodChannel
8+
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
9+
import io.flutter.plugin.common.MethodChannel.Result
10+
import io.flutter.plugin.common.PluginRegistry.Registrar
11+
12+
import org.chromium.base.ContextUtils
13+
14+
/** CronetPlugin */
15+
class CronetPlugin: FlutterPlugin, MethodCallHandler {
16+
/// The MethodChannel that will the communication between Flutter and native Android
17+
///
18+
/// This local reference serves to register the plugin with the Flutter Engine and unregister it
19+
/// when the Flutter Engine is detached from the Activity
20+
private lateinit var channel : MethodChannel
21+
22+
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
23+
System.loadLibrary("cronet.86.0.4240.198")
24+
System.loadLibrary("wrapper")
25+
ContextUtils.initApplicationContext(flutterPluginBinding.applicationContext)
26+
channel = MethodChannel(flutterPluginBinding.binaryMessenger, "cronet")
27+
channel.setMethodCallHandler(this)
28+
}
29+
30+
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
31+
if (call.method == "getPlatformVersion") {
32+
result.success("Android ${android.os.Build.VERSION.RELEASE}")
33+
} else {
34+
result.notImplemented()
35+
}
36+
}
37+
38+
override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
39+
channel.setMethodCallHandler(null)
40+
}
41+
}

0 commit comments

Comments
 (0)