Adds vmservices to retrieve android applink settings (#125998)

fixes https://github.com/flutter/flutter/issues/120408

Added two gradle tasks, one for grabing the application id, one for grabbing app link domains.

Added a new vmservices to call these two gradle tasks and return the result.

The expected work flow is that the devtool will first call a vmservices to grab all avaliable build variants. It will then choose one of the build variant and call this new services to get application id and app link domains.
This commit is contained in:
chunhtai
2023-06-07 15:43:11 -07:00
committed by GitHub
parent 2754514ce2
commit 5328bd9ae0
11 changed files with 672 additions and 24 deletions

View File

@@ -869,6 +869,133 @@ Gradle Crashed
AndroidStudio: () => FakeAndroidStudio(),
});
testUsingContext('can call custom gradle task getApplicationIdForVariant and parse the result', () async {
final AndroidGradleBuilder builder = AndroidGradleBuilder(
java: FakeJava(),
logger: logger,
processManager: processManager,
fileSystem: fileSystem,
artifacts: Artifacts.test(),
usage: testUsage,
gradleUtils: FakeGradleUtils(),
platform: FakePlatform(),
androidStudio: FakeAndroidStudio(),
);
processManager.addCommand(const FakeCommand(
command: <String>[
'gradlew',
'-q',
'printFreeDebugApplicationId',
],
stdout: '''
ApplicationId: com.example.id
''',
));
final String actual = await builder.getApplicationIdForVariant(
'freeDebug',
project: FlutterProject.fromDirectoryTest(fileSystem.currentDirectory),
);
expect(actual, 'com.example.id');
}, overrides: <Type, Generator>{
AndroidStudio: () => FakeAndroidStudio(),
});
testUsingContext('can call custom gradle task getApplicationIdForVariant with unknown crash', () async {
final AndroidGradleBuilder builder = AndroidGradleBuilder(
java: FakeJava(),
logger: logger,
processManager: processManager,
fileSystem: fileSystem,
artifacts: Artifacts.test(),
usage: testUsage,
gradleUtils: FakeGradleUtils(),
platform: FakePlatform(),
androidStudio: FakeAndroidStudio(),
);
processManager.addCommand(const FakeCommand(
command: <String>[
'gradlew',
'-q',
'printFreeDebugApplicationId',
],
stdout: '''
unknown crash
''',
));
final String actual = await builder.getApplicationIdForVariant(
'freeDebug',
project: FlutterProject.fromDirectoryTest(fileSystem.currentDirectory),
);
expect(actual, '');
}, overrides: <Type, Generator>{
AndroidStudio: () => FakeAndroidStudio(),
});
testUsingContext('can call custom gradle task getAppLinkDomainsForVariant and parse the result', () async {
final AndroidGradleBuilder builder = AndroidGradleBuilder(
java: FakeJava(),
logger: logger,
processManager: processManager,
fileSystem: fileSystem,
artifacts: Artifacts.test(),
usage: testUsage,
gradleUtils: FakeGradleUtils(),
platform: FakePlatform(),
androidStudio: FakeAndroidStudio(),
);
processManager.addCommand(const FakeCommand(
command: <String>[
'gradlew',
'-q',
'printFreeDebugAppLinkDomains',
],
stdout: '''
Domain: example.com
Domain: example2.com
''',
));
final List<String> actual = await builder.getAppLinkDomainsForVariant(
'freeDebug',
project: FlutterProject.fromDirectoryTest(fileSystem.currentDirectory),
);
expect(actual, <String>['example.com', 'example2.com']);
}, overrides: <Type, Generator>{
AndroidStudio: () => FakeAndroidStudio(),
});
testUsingContext('can call custom gradle task getAppLinkDomainsForVariant with unknown crash', () async {
final AndroidGradleBuilder builder = AndroidGradleBuilder(
java: FakeJava(),
logger: logger,
processManager: processManager,
fileSystem: fileSystem,
artifacts: Artifacts.test(),
usage: testUsage,
gradleUtils: FakeGradleUtils(),
platform: FakePlatform(),
androidStudio: FakeAndroidStudio(),
);
processManager.addCommand(const FakeCommand(
command: <String>[
'gradlew',
'-q',
'printFreeDebugAppLinkDomains',
],
stdout: '''
unknown crash
''',
));
final List<String> actual = await builder.getAppLinkDomainsForVariant(
'freeDebug',
project: FlutterProject.fromDirectoryTest(fileSystem.currentDirectory),
);
expect(actual.isEmpty, isTrue);
}, overrides: <Type, Generator>{
AndroidStudio: () => FakeAndroidStudio(),
});
testUsingContext("doesn't indicate how to consume an AAR when printHowToConsumeAar is false", () async {
final AndroidGradleBuilder builder = AndroidGradleBuilder(
java: FakeJava(),

View File

@@ -86,6 +86,10 @@ const List<VmServiceExpectation> kAttachIsolateExpectations =
'service': kFlutterGetIOSUniversalLinkSettingsServiceName,
'alias': kFlutterToolAlias,
}),
FakeVmServiceRequest(method: 'registerService', args: <String, Object>{
'service': kFlutterGetAndroidAppLinkSettingsName,
'alias': kFlutterToolAlias,
}),
FakeVmServiceRequest(
method: 'streamListen',
args: <String, Object>{

View File

@@ -0,0 +1,189 @@
// 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:convert';
import 'dart:io' as io;
import 'package:collection/collection.dart';
import 'package:file/file.dart';
import 'package:flutter_tools/src/android/gradle_utils.dart'
show getGradlewFileName;
import 'package:flutter_tools/src/base/io.dart';
import 'package:xml/xml.dart';
import '../src/common.dart';
import 'test_utils.dart';
final XmlElement pureHttpIntentFilter = XmlElement(
XmlName('intent-filter'),
<XmlAttribute>[XmlAttribute(XmlName('autoVerify', 'android'), 'true')],
<XmlElement>[
XmlElement(
XmlName('action'),
<XmlAttribute>[XmlAttribute(XmlName('name', 'android'), 'android.intent.action.VIEW')],
),
XmlElement(
XmlName('category'),
<XmlAttribute>[XmlAttribute(XmlName('name', 'android'), 'android.intent.category.DEFAULT')],
),
XmlElement(
XmlName('category'),
<XmlAttribute>[XmlAttribute(XmlName('name', 'android'), 'android.intent.category.BROWSABLE')],
),
XmlElement(
XmlName('data'),
<XmlAttribute>[
XmlAttribute(XmlName('scheme', 'android'), 'http'),
XmlAttribute(XmlName('host', 'android'), 'pure-http.com'),
],
),
],
);
final XmlElement nonHttpIntentFilter = XmlElement(
XmlName('intent-filter'),
<XmlAttribute>[XmlAttribute(XmlName('autoVerify', 'android'), 'true')],
<XmlElement>[
XmlElement(
XmlName('action'),
<XmlAttribute>[XmlAttribute(XmlName('name', 'android'), 'android.intent.action.VIEW')],
),
XmlElement(
XmlName('category'),
<XmlAttribute>[XmlAttribute(XmlName('name', 'android'), 'android.intent.category.DEFAULT')],
),
XmlElement(
XmlName('category'),
<XmlAttribute>[XmlAttribute(XmlName('name', 'android'), 'android.intent.category.BROWSABLE')],
),
XmlElement(
XmlName('data'),
<XmlAttribute>[
XmlAttribute(XmlName('scheme', 'android'), 'custom'),
XmlAttribute(XmlName('host', 'android'), 'custom.com'),
],
),
],
);
final XmlElement hybridIntentFilter = XmlElement(
XmlName('intent-filter'),
<XmlAttribute>[XmlAttribute(XmlName('autoVerify', 'android'), 'true')],
<XmlElement>[
XmlElement(
XmlName('action'),
<XmlAttribute>[XmlAttribute(XmlName('name', 'android'), 'android.intent.action.VIEW')],
),
XmlElement(
XmlName('category'),
<XmlAttribute>[XmlAttribute(XmlName('name', 'android'), 'android.intent.category.DEFAULT')],
),
XmlElement(
XmlName('category'),
<XmlAttribute>[XmlAttribute(XmlName('name', 'android'), 'android.intent.category.BROWSABLE')],
),
XmlElement(
XmlName('data'),
<XmlAttribute>[
XmlAttribute(XmlName('scheme', 'android'), 'custom'),
XmlAttribute(XmlName('host', 'android'), 'hybrid.com'),
],
),
XmlElement(
XmlName('data'),
<XmlAttribute>[
XmlAttribute(XmlName('scheme', 'android'), 'http'),
],
),
],
);
final XmlElement nonAutoVerifyIntentFilter = XmlElement(
XmlName('intent-filter'),
<XmlAttribute>[],
<XmlElement>[
XmlElement(
XmlName('action'),
<XmlAttribute>[XmlAttribute(XmlName('name', 'android'), 'android.intent.action.VIEW')],
),
XmlElement(
XmlName('category'),
<XmlAttribute>[XmlAttribute(XmlName('name', 'android'), 'android.intent.category.DEFAULT')],
),
XmlElement(
XmlName('category'),
<XmlAttribute>[XmlAttribute(XmlName('name', 'android'), 'android.intent.category.BROWSABLE')],
),
XmlElement(
XmlName('data'),
<XmlAttribute>[
XmlAttribute(XmlName('scheme', 'android'), 'http'),
XmlAttribute(XmlName('host', 'android'), 'non-auto-verify.com'),
],
),
],
);
void main() {
late Directory tempDir;
setUp(() async {
tempDir = createResolvedTempDirectorySync('run_test.');
});
tearDown(() async {
tryToDelete(tempDir);
});
testWithoutContext(
'gradle task exists named print<mode>AppLinkDomains that prints app link domains', () async {
// Create a new flutter project.
final String flutterBin =
fileSystem.path.join(getFlutterRoot(), 'bin', 'flutter');
ProcessResult result = await processManager.run(<String>[
flutterBin,
'create',
tempDir.path,
'--project-name=testapp',
], workingDirectory: tempDir.path);
expect(result.exitCode, 0);
// Adds intent filters for app links
final String androidManifestPath = fileSystem.path.join(tempDir.path, 'android', 'app', 'src', 'main', 'AndroidManifest.xml');
final io.File androidManifestFile = io.File(androidManifestPath);
final XmlDocument androidManifest = XmlDocument.parse(androidManifestFile.readAsStringSync());
final XmlElement activity = androidManifest.findAllElements('activity').first;
activity.children.add(pureHttpIntentFilter);
activity.children.add(nonHttpIntentFilter);
activity.children.add(hybridIntentFilter);
activity.children.add(nonAutoVerifyIntentFilter);
androidManifestFile.writeAsStringSync(androidManifest.toString(), flush: true);
// Ensure that gradle files exists from templates.
result = await processManager.run(<String>[
flutterBin,
'build',
'apk',
'--config-only',
], workingDirectory: tempDir.path);
expect(result.exitCode, 0);
final Directory androidApp = tempDir.childDirectory('android');
result = await processManager.run(<String>[
'.${platform.pathSeparator}${getGradlewFileName(platform)}',
...getLocalEngineArguments(),
'-q', // quiet output.
'printDebugAppLinkDomains',
], workingDirectory: androidApp.path);
expect(result.exitCode, 0);
const List<String> expectedLines = <String>[
// Should only pick up the pure and hybrid intent filters
'Domain: pure-http.com',
'Domain: hybrid.com',
];
final List<String> actualLines = LineSplitter.split(result.stdout.toString()).toList();
expect(const ListEquality<String>().equals(actualLines, expectedLines), isTrue);
});
}

View File

@@ -0,0 +1,64 @@
// 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:convert';
import 'package:collection/collection.dart';
import 'package:file/file.dart';
import 'package:flutter_tools/src/android/gradle_utils.dart'
show getGradlewFileName;
import 'package:flutter_tools/src/base/io.dart';
import '../src/common.dart';
import 'test_utils.dart';
void main() {
late Directory tempDir;
setUp(() async {
tempDir = createResolvedTempDirectorySync('run_test.');
});
tearDown(() async {
tryToDelete(tempDir);
});
testWithoutContext(
'gradle task exists named print<mode>ApplicationId that prints application id', () async {
// Create a new flutter project.
final String flutterBin =
fileSystem.path.join(getFlutterRoot(), 'bin', 'flutter');
ProcessResult result = await processManager.run(<String>[
flutterBin,
'create',
tempDir.path,
'--project-name=testapp',
], workingDirectory: tempDir.path);
expect(result.exitCode, 0);
// Ensure that gradle files exists from templates.
result = await processManager.run(<String>[
flutterBin,
'build',
'apk',
'--config-only',
], workingDirectory: tempDir.path);
expect(result.exitCode, 0);
final Directory androidApp = tempDir.childDirectory('android');
result = await processManager.run(<String>[
'.${platform.pathSeparator}${getGradlewFileName(platform)}',
...getLocalEngineArguments(),
'-q', // quiet output.
'printDebugApplicationId',
], workingDirectory: androidApp.path);
// Verify that gradlew has a javaVersion task.
expect(result.exitCode, 0);
// Verify the format is a number on its own line.
const List<String> expectedLines = <String>[
'ApplicationId: com.example.testapp',
];
final List<String> actualLines = LineSplitter.split(result.stdout.toString()).toList();
expect(const ListEquality<String>().equals(actualLines, expectedLines), isTrue);
});
}

View File

@@ -39,6 +39,12 @@ class FakeAndroidBuilder implements AndroidBuilder {
@override
Future<List<String>> getBuildVariants({required FlutterProject project}) async => const <String>[];
@override
Future<List<String>> getAppLinkDomainsForVariant(String buildVariant, {required FlutterProject project}) async => const <String>[];
@override
Future<String> getApplicationIdForVariant(String buildVariant, {required FlutterProject project}) async => '';
}
/// Creates a [FlutterProject] in a directory named [flutter_project]