Repository: flutter
Issue: `dart:ffi` conflicts with `System.loadLibrary` on Android API <=23
Labels: platform-android,engine,dependency: dart,P2,team-android,triaged-android

## Summary

I'm not sure if this is a Dart SDK issue or a Flutter issue.

On Android API versions <=23, calling a function in the Android SDK that does `System.loadLibrary`, followed by a `dart:ffi` `DynamicLibrary.open` fails.

Some examples of the former can be:

- `android.media.SoundPool`, because of the static block [here](https://cs.android.com/android/platform/superproject/+/android-6.0.1_r81:frameworks/base/media/java/android/media/SoundPool.java;l=115;drc=5c768f4c8584d93fae3f5e87c8fdcc88ecfba002)
- `android.webkit.CookieManager.getInstance()`, because it loads `WebViewChromiumFactoryProvider` which loads a library [here](https://source.chromium.org/chromium/chromium/src/+/44.0.2403.119:android_webview/glue/java/src/com/android/webview/chromium/WebViewChromiumFactoryProvider.java;l=370;drc=9d51f6856cb4522a94d0484f0592f18ba99d95a0?originalUrl=https:%2F%2Fcs.chromium.org%2F)

More info in a doc in b/159167352

### Why this happens

When calling `System.loadLibrary` on Android <=23, the Java runtime actually overrides the paths searched during a `dlopen` call ([`g_ld_library_paths`](https://cs.android.com/android/platform/superproject/+/android-6.0.1_r81:bionic/linker/linker.cpp;l=92;drc=379003621a1d2c28c18b42236d1d61ce80c1e92a)) to match that of the classloader for the caller, before calling `dlopen` on the library.

When this is called by a function in the Android SDK (i.e. down the call chain of `CookieManager`), the classloader is different than that of the users application code, and this overrides the `g_ld_library_paths` to exclude the `g_ld_library_paths` of the current application / APK.

`DynamicLibrary.load(‘libsomething.so’)` in Dart is just a thin layer around `dlopen`. This means that if it is called after the `g_ld_library_paths` are overridden, the linker will be unable to find the `libsomething.so` if this was provided from the APK.

## Steps to Reproduce

https://github.com/jiahaog/ffi_android_linker_conflict/commit/a8971f405789a1181dc858e33c5d96a7d6f361ff is a minimal repro

The important files are just 
- app/lib/main.dart
- app/android/app/src/main/java/com/example/ffi_android_linker_conflict/MainActivity.java

**Expected results:** <!-- what did you want to see? -->

`DynamicLibrary.open` succeeds.

**Actual results:** <!-- what did you see? -->

<details>
  <summary>Logs</summary>

```
I/flutter ( 5755): ══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════
I/flutter ( 5755): The following ArgumentError was thrown while handling a gesture:
I/flutter ( 5755): Invalid argument(s): Failed to load dynamic library (dlopen failed: library "libnative_add.so" not
I/flutter ( 5755): found)
I/flutter ( 5755): When the exception was thrown, this was the stack:
I/flutter ( 5755): #0      _open (dart:ffi-patch/ffi_dynamic_library_patch.dart:11:55)
I/flutter ( 5755): #1      new DynamicLibrary.open (dart:ffi-patch/ffi_dynamic_library_patch.dart:20:12)
I/flutter ( 5755): #2      _MyHomePageState._incrementCounter (package:ffi_android_linker_conflict/main.dart:59:26)
I/flutter ( 5755): #3      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:992:19)
I/flutter ( 5755): #4      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:1098:38)
I/flutter ( 5755): #5      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:184:24)
I/flutter ( 5755): #6      TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:524:11)
I/flutter ( 5755): #7      BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:284:5)
I/flutter ( 5755): #8      BaseTapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:256:7)
I/flutter ( 5755): #9      GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:158:27)
I/flutter ( 5755): #10     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:224:20)
I/flutter ( 5755): #11     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:200:22)
I/flutter ( 5755): #12     GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:158:7)
I/flutter ( 5755): #13     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:104:7)
I/flutter ( 5755): #14     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:88:7)
I/flutter ( 5755): #18     _invoke1 (dart:ui/hooks.dart:283:10)
I/flutter ( 5755): #19     _dispatchPointerDataPacket (dart:ui/hooks.dart:192:5)
I/flutter ( 5755): (elided 3 frames from dart:async)
I/flutter ( 5755): Handler: "onTap"
I/flutter ( 5755): Recognizer:
I/flutter ( 5755):   TapGestureRecognizer#2c717
I/flutter ( 5755): ════════════════════════════════════════════════════════════════════════════════════════════════════
```

```
[✓] Flutter (Channel master, 1.20.0-1.0.pre.118, on Mac OS X 10.15.3 19D76, locale en-US)
    • Flutter version 1.20.0-1.0.pre.118 at /Users/jiahaog/flutter
    • Framework revision 8665e13801 (5 hours ago), 2020-06-18 18:08:01 -0700
    • Engine revision b5f5e6332c
    • Dart version 2.9.0 (build 2.9.0-17.0.dev 54481776c9)

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.0-rc2)
    • Android SDK at /Users/jiahaog/Library/Android/sdk
    • Platform android-stable, build-tools 30.0.0-rc2
    • Java binary at: /Applications/Android Studio 3.5 Preview.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)
    • All Android licenses accepted.

[!] Xcode - develop for iOS and macOS (Xcode 11.3.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 11.3.1, Build version 11C505
    ✗ CocoaPods installed but not working.
        You appear to have CocoaPods installed but it is not working.
        This can happen if the version of Ruby that CocoaPods was installed with is different from the one being used to invoke it.
        This can usually be fixed by re-installing CocoaPods. For more info, see https://github.com/flutter/flutter/issues/14293.
      To re-install CocoaPods, run:
        sudo gem install cocoapods

[✓] Android Studio
    • Android Studio at /Applications/Android Studio 3.5 Preview.app/Contents
    • Flutter plugin version 33.4.2
    • Dart plugin version 183.5901
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1343-b01)

[✓] VS Code (version 1.43.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.9.1

[✓] Connected device (1 available)
    • Android SDK built for x86 64 • emulator-5554 • android-x64 • Android 6.0 (API 23) (emulator)

! Doctor found issues in 1 category.
```

</details>