@@ -141,14 +141,46 @@ public NativeClient(string gameObjectName, BacktraceConfiguration configuration,
141141 private string GetNativeDirectoryPath ( )
142142 {
143143 using ( var unityPlayer = new AndroidJavaClass ( "com.unity3d.player.UnityPlayer" ) )
144- using ( var activity = unityPlayer . GetStatic < AndroidJavaObject > ( "currentActivity" ) )
145- using ( var context = activity . Call < AndroidJavaObject > ( "getApplicationContext" ) )
146- using ( var applicationInfo = context . Call < AndroidJavaObject > ( "getApplicationInfo" ) )
147- {
148- return applicationInfo . Get < string > ( "nativeLibraryDir" ) ;
144+ using ( var activity = unityPlayer . GetStatic < AndroidJavaObject > ( "currentActivity" ) )
145+ {
146+ // handle specific case when current activity is not available
147+ // this case might happen for example in flutter.
148+ if ( activity == null )
149+ {
150+ return string . Empty ;
151+ }
152+ using ( var context = activity . Call < AndroidJavaObject > ( "getApplicationContext" ) )
153+ using ( var applicationInfo = context . Call < AndroidJavaObject > ( "getApplicationInfo" ) )
154+ {
155+ return applicationInfo . Get < string > ( "nativeLibraryDir" ) ;
156+ }
149157 }
150158 }
151159
160+ /// <summary>
161+ /// Guess native directory path based on the data path directory.
162+ /// GetNativeDirectoryPath method might return empty value when activity is not available
163+ /// this might happen in flutter apps.
164+ /// </summary>
165+ /// <returns>Guessed path to lib directory</returns>
166+ private string GuessNativeDirectoryPath ( )
167+ {
168+ var sourceDirectory = Path . Combine ( Path . GetDirectoryName ( Application . dataPath ) , "lib" ) ;
169+ if ( ! Directory . Exists ( sourceDirectory ) )
170+ {
171+ return string . Empty ;
172+ }
173+ var libDirectory = Directory . GetDirectories ( sourceDirectory ) ;
174+ if ( libDirectory . Length == 0 )
175+ {
176+ return string . Empty ;
177+ }
178+ else
179+ {
180+ return libDirectory [ 0 ] ;
181+ }
182+ }
183+
152184 /// <summary>
153185 /// Start crashpad process to handle native Android crashes
154186 /// </summary>
@@ -193,6 +225,10 @@ private void HandleNativeCrashes(IDictionary<string, string> backtraceAttributes
193225 }
194226
195227 var libDirectory = GetNativeDirectoryPath ( ) ;
228+ if ( string . IsNullOrEmpty ( libDirectory ) || ! Directory . Exists ( libDirectory ) )
229+ {
230+ libDirectory = GuessNativeDirectoryPath ( ) ;
231+ }
196232 if ( ! Directory . Exists ( libDirectory ) )
197233 {
198234 return ;
0 commit comments