@@ -104,169 +104,8 @@ String getAvdManagerPath([ AndroidSdk existingSdk ]) {
104
104
AndroidSdk .locateAndroidSdk ()? .avdManagerPath;
105
105
}
106
106
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
-
268
107
class AndroidSdk {
269
- AndroidSdk (this .directory, [ this .ndk] ) {
108
+ AndroidSdk (this .directory) {
270
109
reinitialize ();
271
110
}
272
111
@@ -276,9 +115,6 @@ class AndroidSdk {
276
115
/// The path to the Android SDK.
277
116
final String directory;
278
117
279
- /// Android NDK (can be `null` ).
280
- final AndroidNdk ndk;
281
-
282
118
List <AndroidSdkVersion > _sdkVersions;
283
119
AndroidSdkVersion _latestVersion;
284
120
@@ -380,16 +216,7 @@ class AndroidSdk {
380
216
return null ;
381
217
}
382
218
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);
393
220
}
394
221
395
222
static bool validSdkDirectory (String dir) {
0 commit comments