Skip to content

Commit 4c7d50d

Browse files
authored
Merge pull request #7 from NexusDynamic/feature/fix-shlib-search
[Fix #5] Update path resolution and handling of missing/invalid ndk. …
2 parents 8ad3821 + 8b7de26 commit 4c7d50d

8 files changed

Lines changed: 1094 additions & 108 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@
22

33
## 0.2.1
44

5-
* Fixed NDK path resolution on Windows by using `USERPROFILE` and normalizing path separators.
5+
* Fixed NDK path resolution on Windows by using `USERPROFILE` and normalizing path separators. Thanks @kekland and @AttalliAyoub for the PRs and Issue (#2).
6+
* Fixed `runProcess` failing with a `FormatException` on non-English Windows by decoding process output with the system encoding. Thanks @wyq0918dev for the PR / Issue (#4).
7+
* The build hook now verifies that `libc++_shared.so` exists and is an ELF shared object before emitting it: thanks to @chillbrodev for the Issue (#5).
8+
* An NDK that does not contain the library for the target architecture is now skipped in favour of the next installation, instead of failing the build.
9+
* Added the legacy `sources/cxx-stl/llvm-libc++/libs/<abi>/` location used by NDK r22 and older as a fallback.
10+
* The NDK the Flutter tool is building with is now used first, derived from the compiler in the build config.
11+
* Added `sdk.dir` / `ndk.dir` from the project's `local.properties`, `flutter config --android-sdk`, `ndk-bundle` directories and more well known SDK locations to NDK discovery.
12+
* Fixed NDK installations in `PATH` never being detected, because the search directory for `ndk-build` was incorrect.
13+
* Added the `libcpp_shared_path` user define and the `ANDROID_LIBCPP_SHARED_PATH` environment variable to override where the library comes from.
14+
* Failures report each NDK and file checked, and it is looged by the build hook.
15+
* Added `ia32` mapped to `x86`.
616

717
## 0.2.0
818

‎README.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,31 @@ Dart / flutter package for Android to add the libc++_shared.so STL C++ shared ru
1010

1111
You obviously need dart/flutter installed, but in addition you must have the Android NDK installed. This package does its best to find the NDK install location during the build hook step.
1212

13+
### How the library is found
14+
15+
During the build hook, `libc++_shared.so` is looked for in this order:
16+
17+
1. An explicit override, if you have set one (see [Overriding the location](#overriding-the-location)).
18+
2. The NDK that the Flutter tool itself is building with, derived from the compiler in the build config.
19+
3. Every other NDK installation that can be found, highest version first. These come from `ndk-build` on your `PATH`, the `ANDROID_NDK`, `ANDROID_NDK_HOME`, `ANDROID_NDK_LATEST_HOME` and `ANDROID_NDK_ROOT` environment variables, `sdk.dir` / `ndk.dir` in your project's `local.properties`, the `ANDROID_HOME`, `ANDROID_SDK_ROOT` and `ANDROID_SDK_HOME` environment variables, `flutter config --android-sdk`, and the usual installation directories for your platform.
20+
21+
A candidate is only used once it has been verified to exist and to be an ELF shared object, so an incomplete NDK installation is skipped in favour of the next one rather than producing a build that fails later. If nothing usable is found the build hook fails with the full list of NDKs considered and files checked - please include that output when reporting an issue.
22+
23+
### Overriding the location
24+
25+
If the library lives somewhere this package does not look, point it at the file directly in your application's `pubspec.yaml`:
26+
27+
```yaml
28+
hooks:
29+
user_defines:
30+
android_libcpp_shared:
31+
libcpp_shared_path: /path/to/libc++_shared.so
32+
```
33+
34+
The `ANDROID_LIBCPP_SHARED_PATH` environment variable does the same thing. Note that Gradle reuses a long lived daemon, so a variable exported after the daemon started will not reach the build hook; the user define is not affected by that.
35+
36+
Both settings also accept the root of an NDK installation, in which case that NDK is used in preference to any other.
37+
1338
## Adding the dependency
1439

1540
Add the package to your pubspec.yaml dependencies:

‎example/pubspec.lock‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -253,18 +253,18 @@ packages:
253253
dependency: transitive
254254
description:
255255
name: meta
256-
sha256: "23f08335362185a5ea2ad3a4e597f1375e78bce8a040df5c600c8d3552ef2394"
256+
sha256: "1741988757a65eb6b36abe716829688cf01910bbf91c34354ff7ec1c3de2b349"
257257
url: "https://pub.dev"
258258
source: hosted
259-
version: "1.17.0"
259+
version: "1.18.0"
260260
native_toolchain_c:
261261
dependency: transitive
262262
description:
263263
name: native_toolchain_c
264-
sha256: f59351d28f49520cd3a74eb1f41c5f19ae15e53c65a3231d14af672e46510a96
264+
sha256: f9c168717100ae6d9fee9ffb0be379bf1f8b26b0f6bcbd4fdddcd931993a6a72
265265
url: "https://pub.dev"
266266
source: hosted
267-
version: "0.19.1"
267+
version: "0.19.2"
268268
path:
269269
dependency: transitive
270270
description:
@@ -293,10 +293,10 @@ packages:
293293
dependency: transitive
294294
description:
295295
name: posix
296-
sha256: "185ef7606574f789b40f289c233efa52e96dead518aed988e040a10737febb07"
296+
sha256: bc1bad54ad2b735816e31f8d4600cfde6c7839975085ddfbca48b6c9f7c4044e
297297
url: "https://pub.dev"
298298
source: hosted
299-
version: "6.5.0"
299+
version: "6.5.2"
300300
process:
301301
dependency: transitive
302302
description:
@@ -378,10 +378,10 @@ packages:
378378
dependency: transitive
379379
description:
380380
name: test_api
381-
sha256: "8161c84903fd860b26bfdefb7963b3f0b68fee7adea0f59ef805ecca346f0c7a"
381+
sha256: "949a932224383300f01be9221c39180316445ecb8e7547f70a41a35bf421fb9e"
382382
url: "https://pub.dev"
383383
source: hosted
384-
version: "0.7.10"
384+
version: "0.7.11"
385385
typed_data:
386386
dependency: transitive
387387
description:

‎hook/build.dart‎

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import 'package:android_libcpp_shared/src/locate_ndk.dart';
1+
import 'package:android_libcpp_shared/src/resolve_libcpp.dart';
22
import 'package:code_assets/code_assets.dart';
33
import 'package:hooks/hooks.dart';
44
import 'package:logging/logging.dart';
@@ -24,22 +24,25 @@ void main(List<String> args) async {
2424
final Architecture targetArchitecture =
2525
input.config.code.targetArchitecture;
2626

27-
logger.info('Searching for android NDK...');
28-
final ndkPaths = await NDKLocator.locate();
29-
final ndk = ndkPaths.forBuildConfig(input.config);
30-
if (ndk == null) {
27+
logger.info('Searching for libc++_shared.so for $targetArchitecture...');
28+
final resolution = await resolveLibcppShared(input, logger: logger);
29+
final libcppSharedPath = resolution.libcppShared;
30+
if (libcppSharedPath == null) {
3131
throw StateError(
32-
'No suitable NDK found for target architecture $targetArchitecture.',
32+
resolution.describeFailure(
33+
targetArchitecture,
34+
input.config.code.android.targetNdkApi,
35+
),
3336
);
3437
}
35-
logger.info('Found NDK at ${ndk.path}, version ${ndk.version}.');
36-
final libcppSharedPath = ndk
37-
.hostArchitectures
38-
.first
39-
.targetArchitectures
40-
.first
41-
.sysrootLibPath
42-
.resolve('libc++_shared.so');
38+
final ndk = resolution.ndk;
39+
if (ndk != null) {
40+
logger.info('Using NDK ${ndk.version} at ${ndk.path.toFilePath()}.');
41+
}
42+
logger.info('Using ${libcppSharedPath.toFilePath()}.');
43+
44+
// Re-run the hook if the library path changes
45+
output.dependencies.add(libcppSharedPath);
4346

4447
output.assets.code.add(
4548
CodeAsset(

0 commit comments

Comments
 (0)