Skip to content

Commit ead5b1c

Browse files
authored
Retire v1 embedding compatibility from automatic multidex support (#100685)
1 parent 1a072f9 commit ead5b1c

File tree

3 files changed

+48
-7
lines changed

3 files changed

+48
-7
lines changed

packages/flutter_tools/lib/src/android/multidex.dart

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,35 @@ File _getMultiDexApplicationFile(Directory projectDir) {
2525
void ensureMultiDexApplicationExists(final Directory projectDir) {
2626
final File applicationFile = _getMultiDexApplicationFile(projectDir);
2727
if (applicationFile.existsSync()) {
28-
return;
28+
// This checks for instances of legacy versions of this file. Legacy versions maintained
29+
// compatibility with v1 embedding by extending FlutterApplication. If we detect this,
30+
// we replace the file with the modern v2 embedding version.
31+
if (applicationFile.readAsStringSync().contains('android.app.Application;')) {
32+
return;
33+
}
2934
}
3035
applicationFile.createSync(recursive: true);
3136

3237
final StringBuffer buffer = StringBuffer();
3338
buffer.write('''
3439
// Generated file.
40+
//
3541
// If you wish to remove Flutter's multidex support, delete this entire file.
42+
//
43+
// Modifications to this file should be done in a copy under a different name
44+
// as this file may be regenerated.
3645
3746
package io.flutter.app;
3847
48+
import android.app.Application;
3949
import android.content.Context;
4050
import androidx.annotation.CallSuper;
4151
import androidx.multidex.MultiDex;
4252
4353
/**
44-
* Extension of {@link io.flutter.app.FlutterApplication}, adding multidex support.
54+
* Extension of {@link android.app.Application}, adding multidex support.
4555
*/
46-
public class FlutterMultiDexApplication extends FlutterApplication {
56+
public class FlutterMultiDexApplication extends Application {
4757
@Override
4858
@CallSuper
4959
protected void attachBaseContext(Context base) {

packages/flutter_tools/test/general.shard/android/multidex_test.dart

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import '../../src/common.dart';
1414
import '../../src/context.dart';
1515

1616
void main() {
17-
testUsingContext('ensureMultidexUtilsExists returns when exists', () async {
17+
testUsingContext('ensureMultidexUtilsExists patches file when invalid', () async {
1818
final Directory directory = globals.fs.currentDirectory;
1919
final File applicationFile = directory.childDirectory('android')
2020
.childDirectory('app')
@@ -32,7 +32,33 @@ void main() {
3232
ensureMultiDexApplicationExists(directory);
3333

3434
// File should remain untouched
35-
expect(applicationFile.readAsStringSync(), 'hello');
35+
expect(applicationFile.readAsStringSync(), '''
36+
// Generated file.
37+
//
38+
// If you wish to remove Flutter's multidex support, delete this entire file.
39+
//
40+
// Modifications to this file should be done in a copy under a different name
41+
// as this file may be regenerated.
42+
43+
package io.flutter.app;
44+
45+
import android.app.Application;
46+
import android.content.Context;
47+
import androidx.annotation.CallSuper;
48+
import androidx.multidex.MultiDex;
49+
50+
/**
51+
* Extension of {@link android.app.Application}, adding multidex support.
52+
*/
53+
public class FlutterMultiDexApplication extends Application {
54+
@Override
55+
@CallSuper
56+
protected void attachBaseContext(Context base) {
57+
super.attachBaseContext(base);
58+
MultiDex.install(this);
59+
}
60+
}
61+
''');
3662
}, overrides: <Type, Generator>{
3763
FileSystem: () => MemoryFileSystem.test(),
3864
ProcessManager: () => FakeProcessManager.any(),

packages/flutter_tools/test/integration.shard/test_data/multidex_project.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,18 +298,23 @@ class MultidexProject extends Project {
298298

299299
String get appMultidexApplication => r'''
300300
// Generated file.
301+
//
301302
// If you wish to remove Flutter's multidex support, delete this entire file.
303+
//
304+
// Modifications to this file should be done in a copy under a different name
305+
// as this file may be regenerated.
302306
303307
package io.flutter.app;
304308
309+
import android.app.Application;
305310
import android.content.Context;
306311
import androidx.annotation.CallSuper;
307312
import androidx.multidex.MultiDex;
308313
309314
/**
310-
* Extension of {@link io.flutter.app.FlutterApplication}, adding multidex support.
315+
* Extension of {@link android.app.Application}, adding multidex support.
311316
*/
312-
public class FlutterMultiDexApplication extends FlutterApplication {
317+
public class FlutterMultiDexApplication extends Application {
313318
@Override
314319
@CallSuper
315320
protected void attachBaseContext(Context base) {

0 commit comments

Comments
 (0)