1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// Copyright 2014 The Flutter 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:io';
import 'package:flutter_devicelab/framework/apk_utils.dart';
import 'package:flutter_devicelab/framework/framework.dart';
import 'package:flutter_devicelab/framework/utils.dart';
import 'package:path/path.dart' as path;
final String gradlew = Platform.isWindows ? 'gradlew.bat' : 'gradlew';
final String gradlewExecutable = Platform.isWindows ? '.\\$gradlew' : './$gradlew';
final String fileReadWriteMode = Platform.isWindows ? 'rw-rw-rw-' : 'rw-r--r--';
final bool useAndroidEmbeddingV2 = Platform.environment['ENABLE_ANDROID_EMBEDDING_V2'] == 'true';
/// Tests that the Flutter module project template works and supports
/// adding Flutter to an existing Android app.
Future<void> main() async {
await task(() async {
section('Find Java');
final String javaHome = await findJavaHome();
if (javaHome == null)
return TaskResult.failure('Could not find Java');
print('\nUsing JAVA_HOME=$javaHome');
section('Create Flutter module project');
final Directory tempDir = Directory.systemTemp.createTempSync('flutter_module_test.');
final Directory projectDir = Directory(path.join(tempDir.path, 'hello'));
try {
await inDirectory(tempDir, () async {
await flutter(
'create',
options: <String>['--org', 'io.flutter.devicelab', '--template=module', 'hello'],
);
});
section('Add read-only asset');
final File readonlyTxtAssetFile = await File(path.join(
projectDir.path,
'assets/read-only.txt'
))
.create(recursive: true);
if (!exists(readonlyTxtAssetFile)) {
return TaskResult.failure('Failed to create read-only asset');
}
if (!Platform.isWindows) {
await exec('chmod', <String>[
'444',
readonlyTxtAssetFile.path
]);
}
final File pubspec = File(path.join(projectDir.path, 'pubspec.yaml'));
String content = await pubspec.readAsString();
content = content.replaceFirst(
'\n # assets:\n',
'\n assets:\n - assets/read-only.txt\n',
);
await pubspec.writeAsString(content, flush: true);
section('Add plugins');
content = await pubspec.readAsString();
content = content.replaceFirst(
'\ndependencies:\n',
'\ndependencies:\n device_info: 0.4.1\n package_info: 0.4.0+9\n',
);
await pubspec.writeAsString(content, flush: true);
await inDirectory(projectDir, () async {
await flutter(
'packages',
options: <String>['get'],
);
});
section('Build Flutter module library archive');
await inDirectory(Directory(path.join(projectDir.path, '.android')), () async {
await exec(
gradlewExecutable,
<String>['flutter:assembleDebug'],
environment: <String, String>{ 'JAVA_HOME': javaHome },
);
});
final bool aarBuilt = exists(File(path.join(
projectDir.path,
'.android',
'Flutter',
'build',
'outputs',
'aar',
'flutter-debug.aar',
)));
if (!aarBuilt) {
return TaskResult.failure('Failed to build .aar');
}
section('Build ephemeral host app');
await inDirectory(projectDir, () async {
await flutter(
'build',
options: <String>['apk'],
);
});
final bool ephemeralHostApkBuilt = exists(File(path.join(
projectDir.path,
'build',
'host',
'outputs',
'apk',
'release',
'app-release.apk',
)));
if (!ephemeralHostApkBuilt) {
return TaskResult.failure('Failed to build ephemeral host .apk');
}
section('Clean build');
await inDirectory(projectDir, () async {
await flutter('clean');
});
section('Make Android host app editable');
await inDirectory(projectDir, () async {
await flutter(
'make-host-app-editable',
options: <String>['android'],
);
});
section('Build editable host app');
await inDirectory(projectDir, () async {
await flutter(
'build',
options: <String>['apk'],
);
});
final bool editableHostApkBuilt = exists(File(path.join(
projectDir.path,
'build',
'host',
'outputs',
'apk',
'release',
'app-release.apk',
)));
if (!editableHostApkBuilt) {
return TaskResult.failure('Failed to build editable host .apk');
}
section('Add to existing Android app');
final Directory hostApp = Directory(path.join(tempDir.path, 'hello_host_app'));
mkdir(hostApp);
recursiveCopy(
Directory(
path.join(
flutterDirectory.path,
'dev',
'integration_tests',
useAndroidEmbeddingV2 ? 'android_host_app_v2_embedding' : 'android_host_app',
),
),
hostApp,
);
copy(
File(path.join(projectDir.path, '.android', gradlew)),
hostApp,
);
copy(
File(path.join(projectDir.path, '.android', 'gradle', 'wrapper', 'gradle-wrapper.jar')),
Directory(path.join(hostApp.path, 'gradle', 'wrapper')),
);
final File analyticsOutputFile = File(path.join(tempDir.path, 'analytics.log'));
section('Build debug host APK');
await inDirectory(hostApp, () async {
if (!Platform.isWindows) {
await exec('chmod', <String>['+x', 'gradlew']);
}
await exec(gradlewExecutable,
<String>['app:assembleDebug'],
environment: <String, String>{
'JAVA_HOME': javaHome,
'FLUTTER_ANALYTICS_LOG_FILE': analyticsOutputFile.path,
},
);
});
section('Check debug APK exists');
final String debugHostApk = path.join(
hostApp.path,
'app',
'build',
'outputs',
'apk',
'debug',
'app-debug.apk',
);
if (!exists(File(debugHostApk))) {
return TaskResult.failure('Failed to build debug host APK');
}
section('Check files in debug APK');
checkCollectionContains<String>(<String>[
...flutterAssets,
...debugAssets,
...baseApkFiles,
], await getFilesInApk(debugHostApk));
section('Check debug AndroidManifest.xml');
final String androidManifestDebug = await getAndroidManifest(debugHostApk);
if (!androidManifestDebug.contains('''
<meta-data
android:name="flutterProjectType"
android:value="module" />''')
) {
return TaskResult.failure("Debug host APK doesn't contain metadata: flutterProjectType = module ");
}
final String analyticsOutput = analyticsOutputFile.readAsStringSync();
if (!analyticsOutput.contains('cd24: android')
|| !analyticsOutput.contains('cd25: true')
|| !analyticsOutput.contains('viewName: assemble')) {
return TaskResult.failure(
'Building outer app produced the following analytics: "$analyticsOutput" '
'but not the expected strings: "cd24: android", "cd25: true" and '
'"viewName: assemble"'
);
}
section('Check file access modes for read-only asset from Flutter module');
final String readonlyDebugAssetFilePath = path.join(
hostApp.path,
'app',
'build',
'intermediates',
'merged_assets',
'debug',
'out',
'flutter_assets/assets/read-only.txt',
);
final File readonlyDebugAssetFile = File(readonlyDebugAssetFilePath);
if (!exists(readonlyDebugAssetFile)) {
return TaskResult.failure('Failed to copy read-only asset file');
}
String modes = readonlyDebugAssetFile.statSync().modeString();
print('\nread-only.txt file access modes = $modes');
if (modes != null && modes.compareTo(fileReadWriteMode) != 0) {
return TaskResult.failure('Failed to make assets user-readable and writable');
}
section('Build release host APK');
await inDirectory(hostApp, () async {
await exec(gradlewExecutable,
<String>['app:assembleRelease'],
environment: <String, String>{
'JAVA_HOME': javaHome,
'FLUTTER_ANALYTICS_LOG_FILE': analyticsOutputFile.path,
},
);
});
final String releaseHostApk = path.join(
hostApp.path,
'app',
'build',
'outputs',
'apk',
'release',
'app-release-unsigned.apk',
);
if (!exists(File(releaseHostApk))) {
return TaskResult.failure('Failed to build release host APK');
}
section('Check files in release APK');
checkCollectionContains<String>(<String>[
...flutterAssets,
...baseApkFiles,
'lib/arm64-v8a/libapp.so',
'lib/arm64-v8a/libflutter.so',
'lib/armeabi-v7a/libapp.so',
'lib/armeabi-v7a/libflutter.so',
], await getFilesInApk(releaseHostApk));
section('Check release AndroidManifest.xml');
final String androidManifestRelease = await getAndroidManifest(debugHostApk);
if (!androidManifestRelease.contains('''
<meta-data
android:name="flutterProjectType"
android:value="module" />''')
) {
return TaskResult.failure("Release host APK doesn't contain metadata: flutterProjectType = module ");
}
section('Check file access modes for read-only asset from Flutter module');
final String readonlyReleaseAssetFilePath = path.join(
hostApp.path,
'app',
'build',
'intermediates',
'merged_assets',
'release',
'out',
'flutter_assets/assets/read-only.txt',
);
final File readonlyReleaseAssetFile = File(readonlyReleaseAssetFilePath);
if (!exists(readonlyReleaseAssetFile)) {
return TaskResult.failure('Failed to copy read-only asset file');
}
modes = readonlyReleaseAssetFile.statSync().modeString();
print('\nread-only.txt file access modes = $modes');
if (modes != null && modes.compareTo(fileReadWriteMode) != 0) {
return TaskResult.failure('Failed to make assets user-readable and writable');
}
return TaskResult.success(null);
} on TaskResult catch (taskResult) {
return taskResult;
} catch (e) {
return TaskResult.failure(e.toString());
} finally {
rmTree(tempDir);
}
});
}