Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 6b56b56

Browse files
authored
Replace System.loadLibrary with ReLinker.loadLibrary() in FlutterJNI.loadLibrary() (#55095)
Could fix? flutter/flutter#83596 The communication on https://issuetracker.google.com/issues/346717090#comment2 recommends using [ReLinker](https://github.com/KeepSafe/ReLinker) to avoid a bug in how library loading interacts with the [Play delivery feature](https://developer.android.com/guide/playcore/feature-delivery) (used in turn by Flutter's deferred components). The ReLinker docs also suggest that if you have `minSdk` less than `23`, then you should be using ReLinker regardless, as `System.loadLibrary` is unreliable for other reasons. I don't have any strong evidence to suggest that either one of these two root causes is the definitive cause of flutter/flutter#83596, but it probably wouldn't hurt � [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
1 parent b4034d9 commit 6b56b56

File tree

7 files changed

+17
-7
lines changed

7 files changed

+17
-7
lines changed

DEPS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ deps = {
780780
'packages': [
781781
{
782782
'package': 'flutter/android/embedding_bundle',
783-
'version': 'last_updated:2024-06-18T12:13:41-0700'
783+
'version': 'last_updated:2024-09-10T16:32:16-0700'
784784
}
785785
],
786786
'condition': 'download_android_deps',

shell/platform/android/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ embedding_dependencies_jars = [
394394
"//flutter/third_party/android_embedding_dependencies/lib/viewpager-1.0.0.jar",
395395
"//flutter/third_party/android_embedding_dependencies/lib/window-1.2.0.jar",
396396
"//flutter/third_party/android_embedding_dependencies/lib/window-java-1.2.0.jar",
397+
"//flutter/third_party/android_embedding_dependencies/lib/relinker-1.4.5.jar",
397398
]
398399

399400
action("check_imports") {

shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import androidx.annotation.Nullable;
2525
import androidx.annotation.UiThread;
2626
import androidx.annotation.VisibleForTesting;
27+
import com.getkeepsafe.relinker.ReLinker;
2728
import io.flutter.Log;
2829
import io.flutter.embedding.engine.FlutterEngine.EngineLifecycleListener;
2930
import io.flutter.embedding.engine.dart.PlatformMessageHandler;
@@ -139,12 +140,11 @@ public FlutterJNI provideFlutterJNI() {
139140
*
140141
* <p>This method should only be called once across all FlutterJNI instances.
141142
*/
142-
public void loadLibrary() {
143+
public void loadLibrary(Context context) {
143144
if (FlutterJNI.loadLibraryCalled) {
144145
Log.w(TAG, "FlutterJNI.loadLibrary called more than once");
145146
}
146-
147-
System.loadLibrary("flutter");
147+
ReLinker.loadLibrary(context, "flutter");
148148
FlutterJNI.loadLibraryCalled = true;
149149
}
150150

shell/platform/android/io/flutter/embedding/engine/loader/FlutterLoader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public InitResult call() {
182182
ResourceExtractor resourceExtractor = initResources(appContext);
183183

184184
try {
185-
flutterJNI.loadLibrary();
185+
flutterJNI.loadLibrary(appContext);
186186
} catch (UnsatisfiedLinkError unsatisfiedLinkError) {
187187
String couldntFindVersion = "couldn't find \"libflutter.so\"";
188188
String notFoundVersion = "dlopen failed: library \"libflutter.so\" not found";

shell/platform/android/test/io/flutter/embedding/engine/loader/FlutterLoaderTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void itReportsInitializedAfterInitializing() {
5858
flutterLoader.ensureInitializationComplete(ctx, null);
5959
shadowOf(getMainLooper()).idle();
6060
assertTrue(flutterLoader.initialized());
61-
verify(mockFlutterJNI, times(1)).loadLibrary();
61+
verify(mockFlutterJNI, times(1)).loadLibrary(ctx);
6262
verify(mockFlutterJNI, times(1)).updateRefreshRate();
6363
}
6464

@@ -70,7 +70,7 @@ public void unsatisfiedLinkErrorPathDoesNotExist() {
7070

7171
Mockito.doThrow(new UnsatisfiedLinkError("couldn't find \"libflutter.so\""))
7272
.when(mockFlutterJNI)
73-
.loadLibrary();
73+
.loadLibrary(ctx);
7474
try {
7575
flutterLoader.startInitialization(ctx);
7676
} catch (UnsupportedOperationException e) {

tools/androidx/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Defines the additional Android build time dependencies downloaded by `engine/tools/cipd/android_embedding_bundle`, which then get uploaded to CIPD and pulled by `gclient sync` into `third_party/android_embedding_dependencies/lib/`.
2+
3+
Despite the directory name, `files.json` actually includes one non-androidx dependency: [ReLinker](https://github.com/KeepSafe/ReLinker).

tools/androidx/files.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,11 @@
8686
"out_file_name": "core-1.10.3.aar",
8787
"maven_dependency": "com.google.android.play:core:1.10.3",
8888
"provides": []
89+
},
90+
{
91+
"url": "https://repo1.maven.org/maven2/com/getkeepsafe/relinker/relinker/1.4.5/relinker-1.4.5.aar",
92+
"out_file_name": "relinker-1.4.5.aar",
93+
"maven_dependency": "com.getkeepsafe.relinker:relinker:1.4.5",
94+
"provides": ["com.getkeepsafe.relinker.ReLinker"]
8995
}
9096
]

0 commit comments

Comments
 (0)