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

Commit

Permalink
Flutter support Android, Linux, and Windows 64 bit (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
unsuitable001 committed Aug 4, 2021
1 parent 3fe163a commit c2fb111
Show file tree
Hide file tree
Showing 84 changed files with 2,587 additions and 62 deletions.
31 changes: 19 additions & 12 deletions .github/workflows/test-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ env:

jobs:
# Check code formatting and static analysis on a single OS (linux)
# against Dart stable.
# against Flutter stable.
analyze:
runs-on: ubuntu-latest
strategy:
Expand All @@ -27,17 +27,20 @@ jobs:
sdk: [stable]
steps:
- uses: actions/checkout@v2
- uses: dart-lang/setup-dart@v1.0
- uses: actions/setup-java@v1
with:
sdk: ${{ matrix.sdk }}
java-version: '12.x'
- uses: subosito/flutter-action@v1
with:
channel: ${{ matrix.sdk }}
- id: install
name: Install dependencies
run: dart pub get
run: flutter pub get
- name: Check formatting
run: dart format --output=none --set-exit-if-changed .
run: flutter format --set-exit-if-changed .
if: always() && steps.install.outcome == 'success'
- name: Analyze code
run: dart analyze --fatal-infos
run: flutter analyze --fatal-infos
if: always() && steps.install.outcome == 'success'

# Run tests on a matrix consisting of two dimensions:
Expand All @@ -51,18 +54,22 @@ jobs:
matrix:
# Add macos-latest and/or windows-latest if relevant for this package.
os: [ubuntu-latest, macos-latest, windows-latest]
sdk: [2.13.0, dev]
sdk: [stable, dev]
steps:
- uses: actions/checkout@v2
- uses: dart-lang/setup-dart@v1.0
- uses: actions/setup-java@v1
with:
java-version: '12.x'
- uses: subosito/flutter-action@v1
with:
sdk: ${{ matrix.sdk }}
channel: ${{ matrix.sdk }}
- id: install
name: Install dependencies
run: dart pub get
run: flutter pub get
- id: get_binaries
name: Download/Build Cronet Binaries
run: dart run cronet:setup
run: flutter pub run cronet:setup
if: always() && steps.install.outcome == 'success'
- name: Run VM tests
run: dart test --platform vm
run: flutter test
if: always() && steps.install.outcome == 'success'
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ doc/api/
.history
.vscode
compile_commands.json
*.log

# IntelliJ
*.iml
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.0.4

* Added support for Android and Flutter Desktops (Windows/Linux).

## 0.0.3

* Using `package:args` for handling CLI arguments.
Expand Down
50 changes: 37 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,37 @@ This is a [GSoC 2021 project](https://summerofcode.withgoogle.com/projects/#4757

## Supported Platforms

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

## Requirements

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

## Usage

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

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

**Dart CLI**

```bash
dart pub get
dart run cronet:setup # Downloads the cronet binaries.
flutter pub get
flutter pub run cronet:setup # Downloads the cronet binaries.
```

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.

***Note for Android:** Remember to Add the following permissions in `AndroidManifest.xml` file.

```xml
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
```

Optionally, enable cleartext traffic by adding `android:usesCleartextTraffic="true"` to `AndroidManifest.xml` file.

3. Import

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

## Run Example

### Flutter

```bash
cd example
dart run cronet:setup # Downloads the cronet binaries.
dart run
cd example/flutter
flutter pub get
flutter pub run cronet:setup # Downloads the cronet binaries.
flutter run
```

### Dart CLI

```bash
cd example/cli
flutter pub get
flutter pub run cronet:setup # Downloads the cronet binaries.
dart run bin/example_dart.dart
```

## Run Tests

```bash
dart pub get
dart run cronet:setup # Downloads the cronet binaries.
dart test --platform vm
flutter pub get
flutter pub run cronet:setup # Downloads the cronet binaries.
flutter test
```

You can also verify your cronet binaries using `dart run cronet:setup verify`.
Make sure to have `cmake 3.15`.
Make sure to have `cmake 3.10`.

## Benchmarking

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

**Note for Windows:** Run `step 2` from `x64 Native Tools Command Prompt for VS 2019` shell.

**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.
8 changes: 8 additions & 0 deletions android/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
50 changes: 50 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
group 'dev.google.cronet'
version '1.0-SNAPSHOT'

buildscript {
ext.kotlin_version = '1.3.50'
repositories {
google()
jcenter()
}

dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

rootProject.allprojects {
repositories {
google()
jcenter()
}
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 30

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}

externalNativeBuild {
// Encapsulates your CMake build configurations.
cmake {
// Provides a relative path to your CMake build script.
path "../src/CMakeLists.txt"
}
}

defaultConfig {
minSdkVersion 16
}
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation files(fileTree(dir: 'libs', includes: ['*.jar']))
}
3 changes: 3 additions & 0 deletions android/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true
android.enableJetifier=true
5 changes: 5 additions & 0 deletions android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
1 change: 1 addition & 0 deletions android/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = 'cronet'
6 changes: 6 additions & 0 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dev.google.cronet">
<!-- Flutter Boilerplate Modifications: Internet & network permissions-->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
41 changes: 41 additions & 0 deletions android/src/main/kotlin/dev/google/cronet/CronetPlugin.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package dev.google.cronet

import androidx.annotation.NonNull

import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry.Registrar

import org.chromium.base.ContextUtils

/** CronetPlugin */
class CronetPlugin: FlutterPlugin, MethodCallHandler {
/// The MethodChannel that will the communication between Flutter and native Android
///
/// This local reference serves to register the plugin with the Flutter Engine and unregister it
/// when the Flutter Engine is detached from the Activity
private lateinit var channel : MethodChannel

override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
System.loadLibrary("cronet.86.0.4240.198")
System.loadLibrary("wrapper")
ContextUtils.initApplicationContext(flutterPluginBinding.applicationContext)
channel = MethodChannel(flutterPluginBinding.binaryMessenger, "cronet")
channel.setMethodCallHandler(this)
}

override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
if (call.method == "getPlatformVersion") {
result.success("Android ${android.os.Build.VERSION.RELEASE}")
} else {
result.notImplemented()
}
}

override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
channel.setMethodCallHandler(null)
}
}

0 comments on commit c2fb111

Please sign in to comment.