Merge hello_android into hello_services (#4366)

The hello_services example can now be built both for Android and iOS.
This commit is contained in:
Adam Barth 2016-06-03 16:15:03 -07:00
parent df1c158e0b
commit 423a31f473
18 changed files with 117 additions and 173 deletions

View File

@ -1,36 +0,0 @@
# Example of building a Flutter app for Android using Gradle
This project demonstrates how to embed Flutter within an Android application
and build the Android and Flutter components with Gradle.
To build this project:
* Create a `local.properties` file with these entries:
* `sdk.dir=[path to the Android SDK]`
* `flutter.sdk=[path to the Flutter SDK]`
Then run:
* `gradle wrapper`
* `./gradlew build`
## Updating the Dart code
You can push new Dart code to a Flutter app during development without performing
a full rebuild of the Android app package.
The `flutter refresh` tool manages this process. `flutter refresh` will build
a snapshot of an app's Dart code, copy it to an Android device, and send an
intent instructing the Android app to load the snapshot.
To try this out:
* Install and run this app on your device
* Edit the Dart code in `app/src/flutter/lib`
* cd `app/src/flutter`
* `flutter refresh --activity com.example.flutter/.ExampleActivity`
`flutter refresh` sends an `ACTION_RUN` intent with an extra containing the
device filesystem path where the snapshot was copied. `ExampleActivity.java`
shows how an activity can handle this intent and load the new snapshot into
a Flutter view.

View File

@ -1,72 +0,0 @@
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'dart:convert';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
final Random random = new Random();
Future<String> handleGetRandom(String json) async {
Map<String, dynamic> message = JSON.decode(json);
double min = message['min'].toDouble();
double max = message['max'].toDouble();
double value = (random.nextDouble() * (max - min)) + min;
Map<String, double> reply = <String, double>{'value': value};
return JSON.encode(reply);
}
class HelloAndroid extends StatefulWidget {
@override
_HelloAndroidState createState() => new _HelloAndroidState();
}
class _HelloAndroidState extends State<HelloAndroid> {
double _latitude;
double _longitude;
@override
Widget build(BuildContext context) {
return new Material(
child: new Center(
child: new Column(
children: <Widget>[
new Text('Hello from Flutter!'),
new RaisedButton(
child: new Text('Get Location'),
onPressed: _getLocation
),
new Text('Latitude: $_latitude, Longitude: $_longitude'),
]
)
)
);
}
void _getLocation() {
Map<String, String> message = <String, String>{'provider': 'network'};
HostMessages.sendToHost('getLocation', JSON.encode(message))
.then(_onReceivedLocation);
}
void _onReceivedLocation(String json) {
Map<String, double> reply = JSON.decode(json);
setState(() {
_latitude = reply['latitude'];
_longitude = reply['longitude'];
});
}
}
void main() {
runApp(new HelloAndroid());
HostMessages.addMessageHandler('getRandom', handleGetRandom);
}

View File

@ -1,9 +0,0 @@
name: gradle
dependencies:
flutter:
path: ../../../../../packages/flutter
dev_dependencies:
flutter_test:
path: ../../../../../packages/flutter_test

View File

@ -1,45 +1,8 @@
.idea/
.vagrant/
.sconsign.dblite
.svn/
.DS_Store
*.swp
*.lock
profile
DerivedData/
build/
*.pbxuser
*.mode1v3
*.mode2v3
*.perspectivev3
!default.pbxuser
!default.mode1v3
!default.mode2v3
!default.perspectivev3
xcuserdata
*.moved-aside
*.pyc
*sync/
Icon?
.tags*
.atom/
.buildlog
.idea
.packages
.pub/
build/
packages/
pubspec.lock
ios/Flutter/app.flx
ios/Flutter/app.so
ios/Flutter/app.zip
ios/Flutter/Flutter.framework
ios/Flutter/Generated.xcconfig

View File

@ -1,25 +1,68 @@
# Example of using FlutterView in an iOS app.
# Example of embedding Flutter using FlutterView
This project demonstrates how to embed Flutter within an iOS application
and build the iOS and Flutter components with Xcode.
This project demonstrates how to embed Flutter within an iOS or Android
application. On iOS, the iOS and Flutter components are built with Xcode. On
Android, the Android and Flutter components are built with Android Studio or
gradle.
## Configure
## iOS
### Configure
Create an `ios/Flutter/Generated.xcconfig` file with this entry:
* `FLUTTER_ROOT=[absolute path to the Flutter SDK]`
* `FLUTTER_ROOT=[absolute path to the Flutter SDK]`
There are a number of other parameters you can control with this file:
* `FLUTTER_APPLICATION_PATH`: The path that contains your `pubspec.yaml` file
relative to your `xcodeproj` file.
* `FLUTTER_TARGET`: The path to your `main.dart` relative to your
`pubspec.yaml`. Defaults to `lib/main.dart`.
* `FLUTTER_FRAMEWORK_DIR`: The absolute path to the directory that contains
`Flutter.framework`. Defaults to the `ios-release` version of
`Flutter.framework` in the `bin/cache` directory of the Flutter SDK.
* `FLUTTER_APPLICATION_PATH`: The path that contains your `pubspec.yaml` file
relative to your `xcodeproj` file.
* `FLUTTER_TARGET`: The path to your `main.dart` relative to your
`pubspec.yaml`. Defaults to `lib/main.dart`.
* `FLUTTER_FRAMEWORK_DIR`: The absolute path to the directory that contains
`Flutter.framework`. Defaults to the `ios-release` version of
`Flutter.framework` in the `bin/cache` directory of the Flutter SDK.
## Build
### Build
Once you've configured your project, you can open `ios/HelloServices.xcodeproj`
in Xcode and build the project as usual.
## Android
### Configure
Create an `android/local.properties` file with these entries:
* `sdk.dir=[path to the Android SDK]`
* `flutter.sdk=[path to the Flutter SDK]`
### Build
To build direction with gradle, use the following commands:
* `gradle wrapper`
* `./gradlew build`
To build with Android Studio, open the `android` folder in Android Studio and
build the project as usual.
### Updating the Dart code
You can push new Dart code to a Flutter app during development without performing
a full rebuild of the Android app package.
The `flutter refresh` tool manages this process. `flutter refresh` will build
a snapshot of an app's Dart code, copy it to an Android device, and send an
intent instructing the Android app to load the snapshot.
To try this out:
* Install and run the app on your device
* Edit the Dart code
* `flutter refresh --activity com.example.flutter/.ExampleActivity`
`flutter refresh` sends an `ACTION_RUN` intent with an extra containing the
device filesystem path where the snapshot was copied. `ExampleActivity.java`
shows how an activity can handle this intent and load the new snapshot into
a FlutterView.

View File

@ -0,0 +1,12 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
/gradle
/gradlew
/gradlew.bat

View File

@ -0,0 +1 @@
/build

View File

@ -11,5 +11,5 @@ android {
}
flutter {
source 'src/flutter'
source '../..'
}

View File

@ -35,7 +35,7 @@ public class ExampleActivity extends Activity {
super.onCreate(savedInstanceState);
FlutterMain.ensureInitializationComplete(getApplicationContext(), null);
setContentView(R.layout.flutter_layout);
setContentView(R.layout.hello_services_layout);
flutterView = (FlutterView) findViewById(R.id.flutter_view);
File appBundle = new File(PathUtils.getDataDirectory(this), FlutterMain.APP_BUNDLE);
@ -150,8 +150,13 @@ public class ExampleActivity extends Activity {
JSONObject reply = new JSONObject();
try {
reply.put("latitude", location.getLatitude());
reply.put("longitude", location.getLongitude());
if (location != null) {
reply.put("latitude", location.getLatitude());
reply.put("longitude", location.getLongitude());
} else {
reply.put("latitude", 0);
reply.put("longitude", 0);
}
} catch (JSONException e) {
Log.e(TAG, "JSON exception", e);
return null;

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Flutter App</string>
<string name="app_name">Hello Services</string>
<string name="title">Flutter Application</string>
<string name="get_random">Get Random Number</string>
</resources>

37
examples/hello_services/ios/.gitignore vendored Normal file
View File

@ -0,0 +1,37 @@
.idea/
.vagrant/
.sconsign.dblite
.svn/
.DS_Store
*.swp
*.lock
profile
DerivedData/
build/
*.pbxuser
*.mode1v3
*.mode2v3
*.perspectivev3
!default.pbxuser
!default.mode1v3
!default.mode2v3
!default.perspectivev3
xcuserdata
*.moved-aside
*.pyc
*sync/
Icon?
.tags*
/Flutter/app.flx
/Flutter/app.so
/Flutter/app.zip
/Flutter/Flutter.framework
/Flutter/Generated.xcconfig