flutter/packages/flutter_tools/test/android/android_sdk_test.dart
Martin Kustermann 545ec9efb7
Add support for NDK discovery and add --prefer-shared-library option (#12788)
* Add support for NDK discovery and add --prefer-shared-library option

We would like to be able to use native tools (e.g. simpleperf, gdb) with
precompiled flutter apps.  The native tools work much better with *.so
files instead of the custom formats the Dart VM uses by default.

The reason for using blobs / instruction snapshots is that we do not
want to force flutter users to install the Android NDK.

This CL adds a `--prefer-shared-library` flag to e.g. `flutter build
apk` which will use the NDK compiler (if available) to turn the
precompiled app assembly file to an `*.so` file.  If the NDK compiler is
not available it will default to the default behavior.

* Rebase, add test for NDK detection, augment flutter.gradle with @Input for flag

* Use InMemoryFileSystem for test

* Remove unused import

* Address some analyzer warnings
2017-11-21 15:44:03 +01:00

162 lines
5.2 KiB
Dart

// 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 'package:file/memory.dart';
import 'package:flutter_tools/src/android/android_sdk.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/platform.dart';
import 'package:flutter_tools/src/base/config.dart';
import 'package:test/test.dart';
import '../src/context.dart';
void main() {
MemoryFileSystem fs;
setUp(() {
fs = new MemoryFileSystem();
});
group('android_sdk AndroidSdk', () {
Directory sdkDir;
tearDown(() {
sdkDir?.deleteSync(recursive: true);
sdkDir = null;
});
testUsingContext('parse sdk', () {
sdkDir = _createSdkDirectory();
Config.instance.setValue('android-sdk', sdkDir.path);
final AndroidSdk sdk = AndroidSdk.locateAndroidSdk();
expect(sdk.latestVersion, isNotNull);
expect(sdk.latestVersion.sdkLevel, 23);
}, overrides: <Type, Generator>{
FileSystem: () => fs,
});
testUsingContext('parse sdk N', () {
sdkDir = _createSdkDirectory(withAndroidN: true);
Config.instance.setValue('android-sdk', sdkDir.path);
final AndroidSdk sdk = AndroidSdk.locateAndroidSdk();
expect(sdk.latestVersion, isNotNull);
expect(sdk.latestVersion.sdkLevel, 24);
}, overrides: <Type, Generator>{
FileSystem: () => fs,
});
group('ndk', () {
const <String, String>{
'linux': 'linux-x86_64',
'macos': 'darwin-x86_64',
}.forEach((String os, String osDir) {
testUsingContext('detection on $os', () {
sdkDir = _createSdkDirectory(
withAndroidN: true, withNdkDir: osDir, withNdkSysroot: true);
Config.instance.setValue('android-sdk', sdkDir.path);
final String realSdkDir = sdkDir.path;
final String realNdkDir = fs.path.join(realSdkDir, 'ndk-bundle');
final String realNdkCompiler = fs.path.join(
realNdkDir,
'toolchains',
'arm-linux-androideabi-4.9',
'prebuilt',
osDir,
'bin',
'arm-linux-androideabi-gcc');
final String realNdkSysroot =
fs.path.join(realNdkDir, 'platforms', 'android-9', 'arch-arm');
final AndroidSdk sdk = AndroidSdk.locateAndroidSdk();
expect(sdk.directory, realSdkDir);
expect(sdk.ndkDirectory, realNdkDir);
expect(sdk.ndkCompiler, realNdkCompiler);
expect(sdk.ndkCompilerArgs, <String>['--sysroot', realNdkSysroot]);
}, overrides: <Type, Generator>{
FileSystem: () => fs,
Platform: () => new FakePlatform(operatingSystem: os),
});
});
for (String os in <String>['linux', 'macos']) {
testUsingContext('detection on $os (no ndk available)', () {
sdkDir = _createSdkDirectory(withAndroidN: true);
Config.instance.setValue('android-sdk', sdkDir.path);
final String realSdkDir = sdkDir.path;
final AndroidSdk sdk = AndroidSdk.locateAndroidSdk();
expect(sdk.directory, realSdkDir);
expect(sdk.ndkDirectory, null);
expect(sdk.ndkCompiler, null);
expect(sdk.ndkCompilerArgs, null);
}, overrides: <Type, Generator>{
FileSystem: () => fs,
Platform: () => new FakePlatform(operatingSystem: os),
});
}
});
});
}
Directory _createSdkDirectory(
{bool withAndroidN: false, String withNdkDir, bool withNdkSysroot: false}) {
final Directory dir = fs.systemTempDirectory.createTempSync('android-sdk');
_createSdkFile(dir, 'platform-tools/adb');
_createSdkFile(dir, 'build-tools/19.1.0/aapt');
_createSdkFile(dir, 'build-tools/22.0.1/aapt');
_createSdkFile(dir, 'build-tools/23.0.2/aapt');
if (withAndroidN)
_createSdkFile(dir, 'build-tools/24.0.0-preview/aapt');
_createSdkFile(dir, 'platforms/android-22/android.jar');
_createSdkFile(dir, 'platforms/android-23/android.jar');
if (withAndroidN) {
_createSdkFile(dir, 'platforms/android-N/android.jar');
_createSdkFile(dir, 'platforms/android-N/build.prop', contents: _buildProp);
}
if (withNdkDir != null) {
final String ndkCompiler = fs.path.join(
'ndk-bundle',
'toolchains',
'arm-linux-androideabi-4.9',
'prebuilt',
withNdkDir,
'bin',
'arm-linux-androideabi-gcc');
_createSdkFile(dir, ndkCompiler);
}
if (withNdkSysroot) {
final String armPlatform =
fs.path.join('ndk-bundle', 'platforms', 'android-9', 'arch-arm');
_createDir(dir, armPlatform);
}
return dir;
}
void _createSdkFile(Directory dir, String filePath, { String contents }) {
final File file = dir.childFile(filePath);
file.createSync(recursive: true);
if (contents != null) {
file.writeAsStringSync(contents, flush: true);
}
}
void _createDir(Directory dir, String path) {
final Directory directory = fs.directory(fs.path.join(dir.path, path));
directory.createSync(recursive: true);
}
const String _buildProp = r'''
ro.build.version.incremental=1624448
ro.build.version.sdk=24
ro.build.version.codename=REL
''';