Skip to content

Commit ad07c40

Browse files
authored
Delete unused NDK location checks (flutter#53694)
1 parent 572680e commit ad07c40

File tree

5 files changed

+2
-312
lines changed

5 files changed

+2
-312
lines changed

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

Lines changed: 2 additions & 175 deletions
Original file line numberDiff line numberDiff line change
@@ -104,169 +104,8 @@ String getAvdManagerPath([ AndroidSdk existingSdk ]) {
104104
AndroidSdk.locateAndroidSdk()?.avdManagerPath;
105105
}
106106

107-
class AndroidNdkSearchError {
108-
AndroidNdkSearchError(this.reason);
109-
110-
/// The message explaining why NDK was not found.
111-
final String reason;
112-
}
113-
114-
class AndroidNdk {
115-
AndroidNdk._(this.directory, this.compiler, this.compilerArgs);
116-
117-
/// The path to the NDK.
118-
final String directory;
119-
120-
/// The path to the NDK compiler.
121-
final String compiler;
122-
123-
/// The mandatory arguments to the NDK compiler.
124-
final List<String> compilerArgs;
125-
126-
/// Locate NDK within the given SDK or throw [AndroidNdkSearchError].
127-
static AndroidNdk locateNdk(String androidHomeDir) {
128-
if (androidHomeDir == null) {
129-
throw AndroidNdkSearchError('Can not locate NDK because no SDK is found');
130-
}
131-
132-
String findBundle(String androidHomeDir) {
133-
final String ndkDirectory = globals.fs.path.join(androidHomeDir, 'ndk-bundle');
134-
if (!globals.fs.isDirectorySync(ndkDirectory)) {
135-
throw AndroidNdkSearchError('Can not locate ndk-bundle, tried: $ndkDirectory');
136-
}
137-
return ndkDirectory;
138-
}
139-
140-
// Returns list that contains toolchain bin folder and compiler binary name.
141-
List<String> findToolchainAndCompiler(String ndkDirectory) {
142-
String directory;
143-
if (globals.platform.isLinux) {
144-
directory = 'linux-x86_64';
145-
} else if (globals.platform.isMacOS) {
146-
directory = 'darwin-x86_64';
147-
} else {
148-
throw AndroidNdkSearchError('Only Linux and macOS are supported');
149-
}
150-
151-
final String toolchainBin = globals.fs.path.join(ndkDirectory,
152-
'toolchains', 'arm-linux-androideabi-4.9', 'prebuilt', directory,
153-
'bin');
154-
final String ndkCompiler = globals.fs.path.join(toolchainBin,
155-
'arm-linux-androideabi-gcc');
156-
if (!globals.fs.isFileSync(ndkCompiler)) {
157-
throw AndroidNdkSearchError('Can not locate GCC binary, tried $ndkCompiler');
158-
}
159-
160-
return <String>[toolchainBin, ndkCompiler];
161-
}
162-
163-
List<String> findSysroot(String ndkDirectory) {
164-
// If entity represents directory with name android-<version> that
165-
// contains arch-arm subdirectory then returns version, otherwise
166-
// returns null.
167-
int toPlatformVersion(FileSystemEntity entry) {
168-
if (entry is! Directory) {
169-
return null;
170-
}
171-
172-
if (!globals.fs.isDirectorySync(globals.fs.path.join(entry.path, 'arch-arm'))) {
173-
return null;
174-
}
175-
176-
final String name = globals.fs.path.basename(entry.path);
177-
178-
const String platformPrefix = 'android-';
179-
if (!name.startsWith(platformPrefix)) {
180-
return null;
181-
}
182-
183-
return int.tryParse(name.substring(platformPrefix.length));
184-
}
185-
186-
final String platformsDir = globals.fs.path.join(ndkDirectory, 'platforms');
187-
final List<int> versions = globals.fs
188-
.directory(platformsDir)
189-
.listSync()
190-
.map(toPlatformVersion)
191-
.where((int version) => version != null)
192-
.toList(growable: false);
193-
versions.sort();
194-
195-
final int suitableVersion = versions
196-
.firstWhere((int version) => version >= 9, orElse: () => null);
197-
if (suitableVersion == null) {
198-
throw AndroidNdkSearchError('Can not locate a suitable platform ARM sysroot (need android-9 or newer), tried to look in $platformsDir');
199-
}
200-
201-
final String armPlatform = globals.fs.path.join(ndkDirectory, 'platforms',
202-
'android-$suitableVersion', 'arch-arm');
203-
return <String>['--sysroot', armPlatform];
204-
}
205-
206-
int findNdkMajorVersion(String ndkDirectory) {
207-
final String propertiesFile = globals.fs.path.join(ndkDirectory, 'source.properties');
208-
if (!globals.fs.isFileSync(propertiesFile)) {
209-
throw AndroidNdkSearchError('Can not establish ndk-bundle version: $propertiesFile not found');
210-
}
211-
212-
// Parse source.properties: each line has Key = Value format.
213-
final Iterable<String> propertiesFileLines = globals.fs.file(propertiesFile)
214-
.readAsStringSync()
215-
.split('\n')
216-
.map<String>((String line) => line.trim())
217-
.where((String line) => line.isNotEmpty);
218-
final Map<String, String> properties = <String, String>{};
219-
for (final String line in propertiesFileLines) {
220-
final List<String> parts = line.split(' = ');
221-
if (parts.length == 2) {
222-
properties[parts[0]] = parts[1];
223-
} else {
224-
globals.printError('Malformed line in ndk source.properties: "$line".');
225-
}
226-
}
227-
228-
if (!properties.containsKey('Pkg.Revision')) {
229-
throw AndroidNdkSearchError('Can not establish ndk-bundle version: $propertiesFile does not contain Pkg.Revision');
230-
}
231-
232-
// Extract major version from Pkg.Revision property which looks like <ndk-version>.x.y.
233-
return int.parse(properties['Pkg.Revision'].split('.').first);
234-
}
235-
236-
final String ndkDir = findBundle(androidHomeDir);
237-
final int ndkVersion = findNdkMajorVersion(ndkDir);
238-
final List<String> ndkToolchainAndCompiler = findToolchainAndCompiler(ndkDir);
239-
final String ndkToolchain = ndkToolchainAndCompiler[0];
240-
final String ndkCompiler = ndkToolchainAndCompiler[1];
241-
final List<String> ndkCompilerArgs = findSysroot(ndkDir);
242-
if (ndkVersion >= 18) {
243-
// Newer versions of NDK use clang instead of gcc, which falls back to
244-
// system linker instead of using toolchain linker. Force clang to
245-
// use appropriate linker by passing -fuse-ld=<path-to-ld> command line
246-
// flag.
247-
final String ndkLinker = globals.fs.path.join(ndkToolchain, 'arm-linux-androideabi-ld');
248-
if (!globals.fs.isFileSync(ndkLinker)) {
249-
throw AndroidNdkSearchError('Can not locate linker binary, tried $ndkLinker');
250-
}
251-
ndkCompilerArgs.add('-fuse-ld=$ndkLinker');
252-
}
253-
return AndroidNdk._(ndkDir, ndkCompiler, ndkCompilerArgs);
254-
}
255-
256-
/// Returns a descriptive message explaining why NDK can not be found within
257-
/// the given SDK.
258-
static String explainMissingNdk(String androidHomeDir) {
259-
try {
260-
locateNdk(androidHomeDir);
261-
return 'Unexpected error: found NDK on the second try';
262-
} on AndroidNdkSearchError catch (e) {
263-
return e.reason;
264-
}
265-
}
266-
}
267-
268107
class AndroidSdk {
269-
AndroidSdk(this.directory, [this.ndk]) {
108+
AndroidSdk(this.directory) {
270109
reinitialize();
271110
}
272111

@@ -276,9 +115,6 @@ class AndroidSdk {
276115
/// The path to the Android SDK.
277116
final String directory;
278117

279-
/// Android NDK (can be `null`).
280-
final AndroidNdk ndk;
281-
282118
List<AndroidSdkVersion> _sdkVersions;
283119
AndroidSdkVersion _latestVersion;
284120

@@ -380,16 +216,7 @@ class AndroidSdk {
380216
return null;
381217
}
382218

383-
// Try to find the NDK compiler. If we can't find it, it's also ok.
384-
AndroidNdk ndk;
385-
try {
386-
ndk = AndroidNdk.locateNdk(androidHomeDir);
387-
} on AndroidNdkSearchError {
388-
// Ignore AndroidNdkSearchError's but don't ignore any other
389-
// exceptions.
390-
}
391-
392-
return AndroidSdk(androidHomeDir, ndk);
219+
return AndroidSdk(androidHomeDir);
393220
}
394221

395222
static bool validSdkDirectory(String dir) {

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,6 @@ class AndroidValidator extends DoctorValidator {
166166

167167
messages.add(ValidationMessage(_userMessages.androidSdkLocation(_androidSdk.directory)));
168168

169-
messages.add(ValidationMessage(_androidSdk.ndk == null
170-
? _userMessages.androidMissingNdk
171-
: _userMessages.androidNdkLocation(_androidSdk.ndk.directory)));
172-
173169
String sdkVersionText;
174170
if (_androidSdk.latestVersion != null) {
175171
if (_androidSdk.latestVersion.sdkLevel < 28 || _androidSdk.latestVersion.buildToolsVersion < kAndroidSdkBuildToolsMinVersion) {

packages/flutter_tools/lib/src/base/user_messages.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ class UserMessages {
7777
String androidSdkInstallHelp(Platform platform) =>
7878
'Try re-installing or updating your Android SDK,\n'
7979
'visit ${_androidSdkInstallUrl(platform)} for detailed instructions.';
80-
String get androidMissingNdk => 'Android NDK location not configured (optional; useful for native profiling support)';
81-
String androidNdkLocation(String directory) => 'Android NDK at $directory';
8280
// Also occurs in AndroidLicenseValidator
8381
String androidStatusInfo(String version) => 'Android SDK version $version';
8482

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

Lines changed: 0 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import 'package:flutter_tools/src/base/io.dart' show ProcessResult;
99
import 'package:flutter_tools/src/globals.dart' as globals;
1010
import 'package:mockito/mockito.dart';
1111
import 'package:process/process.dart';
12-
import 'package:platform/platform.dart';
1312

1413
import '../../src/common.dart';
1514
import '../../src/context.dart';
@@ -159,105 +158,13 @@ void main() {
159158
FileSystem: () => fs,
160159
ProcessManager: () => processManager,
161160
});
162-
163-
group('ndk', () {
164-
const <String, String>{
165-
'linux': 'linux-x86_64',
166-
'macos': 'darwin-x86_64',
167-
}.forEach((String os, String osDir) {
168-
testUsingContext('detection on $os', () {
169-
sdkDir = MockAndroidSdk.createSdkDirectory(
170-
withAndroidN: true, withNdkDir: osDir, withNdkSysroot: true);
171-
globals.config.setValue('android-sdk', sdkDir.path);
172-
173-
final String realSdkDir = sdkDir.path;
174-
final String realNdkDir = globals.fs.path.join(realSdkDir, 'ndk-bundle');
175-
final String realNdkCompiler = globals.fs.path.join(
176-
realNdkDir,
177-
'toolchains',
178-
'arm-linux-androideabi-4.9',
179-
'prebuilt',
180-
osDir,
181-
'bin',
182-
'arm-linux-androideabi-gcc');
183-
final String realNdkSysroot =
184-
globals.fs.path.join(realNdkDir, 'platforms', 'android-9', 'arch-arm');
185-
186-
final AndroidSdk sdk = AndroidSdk.locateAndroidSdk();
187-
expect(sdk.directory, realSdkDir);
188-
expect(sdk.ndk, isNotNull);
189-
expect(sdk.ndk.directory, realNdkDir);
190-
expect(sdk.ndk.compiler, realNdkCompiler);
191-
expect(sdk.ndk.compilerArgs, <String>['--sysroot', realNdkSysroot]);
192-
}, overrides: <Type, Generator>{
193-
FileSystem: () => fs,
194-
ProcessManager: () => FakeProcessManager.any(),
195-
Platform: () => FakePlatform(operatingSystem: os),
196-
});
197-
198-
testUsingContext('newer NDK require explicit -fuse-ld on $os', () {
199-
sdkDir = MockAndroidSdk.createSdkDirectory(
200-
withAndroidN: true, withNdkDir: osDir, withNdkSysroot: true, ndkVersion: 18);
201-
globals.config.setValue('android-sdk', sdkDir.path);
202-
203-
final String realSdkDir = sdkDir.path;
204-
final String realNdkDir = globals.fs.path.join(realSdkDir, 'ndk-bundle');
205-
final String realNdkToolchainBin = globals.fs.path.join(
206-
realNdkDir,
207-
'toolchains',
208-
'arm-linux-androideabi-4.9',
209-
'prebuilt',
210-
osDir,
211-
'bin');
212-
final String realNdkCompiler = globals.fs.path.join(
213-
realNdkToolchainBin,
214-
'arm-linux-androideabi-gcc');
215-
final String realNdkLinker = globals.fs.path.join(
216-
realNdkToolchainBin,
217-
'arm-linux-androideabi-ld');
218-
final String realNdkSysroot =
219-
globals.fs.path.join(realNdkDir, 'platforms', 'android-9', 'arch-arm');
220-
221-
final AndroidSdk sdk = AndroidSdk.locateAndroidSdk();
222-
expect(sdk.directory, realSdkDir);
223-
expect(sdk.ndk, isNotNull);
224-
expect(sdk.ndk.directory, realNdkDir);
225-
expect(sdk.ndk.compiler, realNdkCompiler);
226-
expect(sdk.ndk.compilerArgs, <String>['--sysroot', realNdkSysroot, '-fuse-ld=$realNdkLinker']);
227-
}, overrides: <Type, Generator>{
228-
FileSystem: () => fs,
229-
ProcessManager: () => FakeProcessManager.any(),
230-
Platform: () => FakePlatform(operatingSystem: os),
231-
});
232-
});
233-
234-
for (final String os in <String>['linux', 'macos']) {
235-
testUsingContext('detection on $os (no ndk available)', () {
236-
sdkDir = MockAndroidSdk.createSdkDirectory(withAndroidN: true);
237-
globals.config.setValue('android-sdk', sdkDir.path);
238-
239-
final String realSdkDir = sdkDir.path;
240-
final AndroidSdk sdk = AndroidSdk.locateAndroidSdk();
241-
expect(sdk.directory, realSdkDir);
242-
expect(sdk.ndk, isNull);
243-
final String explanation = AndroidNdk.explainMissingNdk(sdk.directory);
244-
expect(explanation, contains('Can not locate ndk-bundle'));
245-
}, overrides: <Type, Generator>{
246-
FileSystem: () => fs,
247-
ProcessManager: () => FakeProcessManager.any(),
248-
Platform: () => FakePlatform(operatingSystem: os),
249-
});
250-
}
251-
});
252161
});
253162
}
254163

255164
/// A broken SDK installation.
256165
class MockBrokenAndroidSdk extends Mock implements AndroidSdk {
257166
static Directory createSdkDirectory({
258167
bool withAndroidN = false,
259-
String withNdkDir,
260-
bool withNdkSysroot = false,
261168
bool withSdkManager = true,
262169
}) {
263170
final Directory dir = globals.fs.systemTempDirectory.createTempSync('flutter_mock_android_sdk.');

packages/flutter_tools/test/src/mocks.dart

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,6 @@ class MockApplicationPackageFactory extends Mock implements ApplicationPackageFa
6363
class MockAndroidSdk extends Mock implements AndroidSdk {
6464
static Directory createSdkDirectory({
6565
bool withAndroidN = false,
66-
String withNdkDir,
67-
int ndkVersion = 16,
68-
bool withNdkSysroot = false,
6966
bool withSdkManager = true,
7067
bool withPlatformTools = true,
7168
bool withBuildTools = true,
@@ -100,41 +97,6 @@ class MockAndroidSdk extends Mock implements AndroidSdk {
10097
_createSdkFile(dir, 'tools/bin/sdkmanager$bat');
10198
}
10299

103-
if (withNdkDir != null) {
104-
final String ndkToolchainBin = globals.fs.path.join(
105-
'ndk-bundle',
106-
'toolchains',
107-
'arm-linux-androideabi-4.9',
108-
'prebuilt',
109-
withNdkDir,
110-
'bin',
111-
);
112-
final String ndkCompiler = globals.fs.path.join(
113-
ndkToolchainBin,
114-
'arm-linux-androideabi-gcc',
115-
);
116-
final String ndkLinker = globals.fs.path.join(
117-
ndkToolchainBin,
118-
'arm-linux-androideabi-ld',
119-
);
120-
_createSdkFile(dir, ndkCompiler);
121-
_createSdkFile(dir, ndkLinker);
122-
_createSdkFile(dir, globals.fs.path.join('ndk-bundle', 'source.properties'), contents: '''
123-
Pkg.Desc = Android NDK[]
124-
Pkg.Revision = $ndkVersion.1.5063045
125-
126-
''');
127-
}
128-
if (withNdkSysroot) {
129-
final String armPlatform = globals.fs.path.join(
130-
'ndk-bundle',
131-
'platforms',
132-
'android-9',
133-
'arch-arm',
134-
);
135-
_createDir(dir, armPlatform);
136-
}
137-
138100
return dir;
139101
}
140102

0 commit comments

Comments
 (0)