Android Question GetSafeDirDefaultExternal Causing Fatal Error

I am now trying to use the <bold>RuntimePermissions.GetSafeDirDefaultExternal</bold> function instead of <bold>File.DirDefaultExternal</bold> to read and write files to a safe external directory. Unfortunately this function is causing a fatal error when used in apps on my phone (running Android 10). The following tiny sample app crashes on my phone when the GetSafeDirDefaultExternal function is encountered . I see that there are some related posts on the forum but they don't show solutions that solve this problem. I've updated the <bold>tools</bold> directory and the other resources in the /Android folder as one post recommends but this function is still causing a fatal error. There must be a simple explanation for this problem because users are obviously using this function successfully.

<code>
Sub Process_Globals

End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)

Public xui As XUI
Dim rp As RuntimePermissions

xui.MsgboxAsync("test 1", "debug")
Wait for Msgbox_Result(Result As Int)

'This statement executes properly
xui.MsgboxAsync("File.DirDefaultExternal: " & File.DirDefaultExternal, "debug")
Wait for Msgbox_Result(Result As Int)

'This statement causes the app to crash!
xui.MsgboxAsync("rp.GetSafeDirDefaultExternal(): " & rp.GetSafeDirDefaultExternal(""), "debug")
Wait for Msgbox_Result(Result As Int)
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
</code>
 
I've reposted my question below with corrected tags. I'm not sure what you mean when you say "You need to post the log." No messages appear in the log section of the B4A IDE when this small app is compiled.

I am now trying to use the RuntimePermissions.GetSafeDirDefaultExternal function instead of File.DirDefaultExternal to read and write files to a safe external directory. Unfortunately this function is causing a fatal error when used in apps on my phone (running Android 10). The following tiny sample app crashes on my phone when the GetSafeDirDefaultExternal function is encountered . I see that there are some related posts on the forum but they don't show solutions that solve this problem. I've updated the tools directory and the other resources in the /Android folder as one post recommends but this function is still causing a fatal error. There must be a simple explanation for this problem because users are obviously using this function successfully.

B4X:
Sub Process_Globals

End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)

Public xui As XUI
Dim rp As RuntimePermissions

xui.MsgboxAsync("test 1", "debug")
Wait for Msgbox_Result(Result As Int)

'This statement executes properly
xui.MsgboxAsync("File.DirDefaultExternal: " & File.DirDefaultExternal, "debug")
Wait for Msgbox_Result(Result As Int)

'This statement causes the app to crash!
xui.MsgboxAsync("rp.GetSafeDirDefaultExternal(): " & rp.GetSafeDirDefaultExternal(""), "debug")
Wait for Msgbox_Result(Result As Int)
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 
Upvote 0
It took me some time to learn how to create a runtime error log. For the benefit of other users who don't happen to know how to do it, it is done by simply adding the following statement to the Project Attributes section at the top of the Main code module:

#BridgeLogger: True

If B4A bridge is connected an execution log is shown in the B4A IDE when the app is run on a device.

In the case of the code above, when run on my phone I get the follow pretty cryptic error log

main$ResumableSub_Activity_Createresume (java line: 398)
java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/core/content/ContextCompat;
at anywheresoftware.b4a.objects.RuntimePermissions.GetSafeDirDefaultExternal(RuntimePermissions.java:120)
at b4a.example.main$ResumableSub_Activity_Create.resume(main.java:398)
at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:267)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:207)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
at anywheresoftware.b4a.keywords.Common$1.onClick(Common.java:490)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:191)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:237)
at android.app.ActivityThread.main(ActivityThread.java:7948)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1075)
Caused by: java.lang.ClassNotFoundException: androidx.core.content.ContextCompat
... 13 more

Hopefully this information will help someone figure out why a fatal error is occurring in my case, but not when this code is compiled and run by other users.
 
Upvote 0
Hi Erel,

Thanks for your reply. I currently have B4A Version 10.50 installed and I've updated my sdk files using the zip files from the link you provided. When I rerun my test app I unfortunately still ultimately get the same error:

Caused by: java.lang.ClassNotFoundException: androidx.core.content.ContextCompat

Is there another way I can get hold of the missing class?
 
Upvote 0
An additional note - when I point the sdk manager to the new sdk files I also get an error message:

Could not find or load main class

com.android.sdklib.toos.sdkmanager.sdkmanagerCli

I imagine that this error may be the cause of the error noted above.
 
Upvote 0
I erased the /android directory that I previously created by extracting the sdk files with the Windows extraction tool and recreated it by extracting the same zip files with 7-Zip as recommended. After that I no longer got an error message about a missing class when I pointed the sdk-manager to the new /android folder. I then installed all the new classes that appeared. Unfortunately I found that I still got the same error message when I ran the test app.

Caused by: java.lang.ClassNotFoundException: androidx.core.content.ContextCompa

I looked through the classes (class libraries?) listed in the sdk manager and there is one listed as:

androidx.core:core (Google Maven)

It is listed as 'installed'. Should the missing class be part of that library? Is there a way to check? I would welcome suggestions on what else to try.
 
Last edited:
Upvote 0
I certainly believe Erel when he says that with a correct installation everything will work. Obviously other users have been using the
RuntimePermissions.GetSafeDirDefaultExternal("") command successfully and have even run my sample app without any errors so the problem is clearly with my installation. Unfortunately I've tried following the installation instructions multiple times and still haven't eliminated the 'missing class' error. I am either still doing something wrong or I did something wrong in the past that is still having consequences. In any case this is fortunately the only command that has been problematic for me so far and I've now come up with a work-around using Erel's SD Card to Internal Directory code example. I will let my current installation stand unless I encounter other similar problems in the future. Trying to solve this problem was definitely informative in any case and I appreciate the assistance I've received.
 
Upvote 0
Thanks for the suggestion. I hadn't thought of doing that and it does make sense that perhaps installing a newer version of B4A might solve my installation problems (if I've performed the other installation steps correctly). I've now installed v10.70 but, unfortunately, I still get the same damn error. More suggestions are welcome!
 
Upvote 0
For the benefit of anyone who is having the same problem I've described above I wanted to mention that I finally found the source of the problem in my case.
The problem was an incorrect path to the android.jar file. I didn't realize that android.jar is included in one the sdk resource directories we are instructed to create when installing the B4A SDK. In the Tools>Configure Paths utility I had the android.jar path set to a version of android.jar that I had installed in a separate Java installation. When I changed the path to C:\Android\platforms\android-30\android.jar as specified in the instructions the missing class issue was resolved. As is usually the case, the solution seems obvious when you finally find it.
 
Upvote 0
Top