B4J Question jShell and ADB Commands

Juan Marrero

Active Member
Licensed User
Longtime User
Hello.
I'm trying to use jShell to send adb commands to android devices. This is the string I'm using as arguments: backup -f "C:\Users\Bike Stop\Documents\b4j\ADBBackup\Objects\bkp\8_19_231_12_40_51_bkp.ab" -apk -shared -all -nosystem. When I copy this string to a command prompt screen it works, but throug b4j it triggers "adb help" command.
This is the code:
B4X:
   Dim CMD As Shell
   Dim args As String
   
   args = "backup"
   
   If chkSaveFolder.Checked Then
     If txtSaveFolder.Text <> "" Then
       Dim date As String = DateTime.GetMonth(DateTime.Now) & "_" & DateTime.GetDayOfMonth(DateTime.Now) & "_" & DateTime.GetDayOfYear(DateTime.Now)
       Dim time As String = DateTime.GetHour(DateTime.Now) & "_" & DateTime.GetMinute(DateTime.Now) & "_" & DateTime.GetSecond(DateTime.Now)
       Dim fullPath As String = txtSaveFolder.Text
       If fullPath.EndsWith("\") Then
         fullPath = fullPath & date & "_" & time & "_bkp.ab"
       Else
         fullPath = fullPath & "\" & date & "_" & time & "_bkp.ab"
       End If
       
       args = args & " -f " & $""PathToReplace""$
       args = args.Replace("PathToReplace", fullPath)
     End If
     
     If chkSaveApks.Checked Then
       args = args & " -apk"
     Else
       args = args & " -noapk"
     End If
     
     If chkSaveShared.Checked Then
       args = args & " -shared"
     Else
       args = args & " -noshared"
     End If
     
     args = args & " -all"
     
     If chkSaveSystem.Checked Then
       args = args & " -system"
     Else
       args = args & " -nosystem"
     End If     
   Else
     args = args & " -all"
   End If
   
   CMD.Initialize("CMD", "adb", Array As String(args))
   CMD.WorkingDirectory = ModMain.APP_PATH
   CMD.Run(100000)

This code works OK:
B4X:
   Dim CMD As Shell
   
   CMD.Initialize("CMD", "adb", Array As String("devices") )
   CMD.WorkingDirectory = ModMain.APP_PATH
   CMD.Run(100000)

For sure there is a mistake of mine somewhere. I also tried the List approach with same results.
 

Juan Marrero

Active Member
Licensed User
Longtime User
Got it working. My mistake was that i put -f <PATH> in the same argument. After I separated them into two arguments it started working.
Posting the code in case someone need it.

B4X:
   Dim CMD As Shell
   Dim args As List
   
   args.Initialize
   
   args.Add("backup")
   
   If chkSaveFolder.Checked Then
     If txtSaveFolder.Text <> "" Then
       Dim date As String = DateTime.GetMonth(DateTime.Now) & "_" & DateTime.GetDayOfMonth(DateTime.Now) & "_" & DateTime.GetYear(DateTime.Now)
       Dim time As String = DateTime.GetHour(DateTime.Now) & "_" & DateTime.GetMinute(DateTime.Now) & "_" & DateTime.GetSecond(DateTime.Now)
       Dim fullPath As String = txtSaveFolder.Text
       If fullPath.EndsWith("\") Then
         fullPath = fullPath & date & "_" & time & "_bkp.ab"
       Else
         fullPath = fullPath & "\" & date & "_" & time & "_bkp.ab"
       End If
       
       Dim arg As String = $""PathToReplace""$
       
       args.Add("-f")
       args.Add(arg.Replace("PathToReplace", fullPath))
     End If
     
     If chkSaveApks.Checked Then
       args.Add("-apk")
     Else
       args.Add("-noapk")
     End If
     
     If chkSaveShared.Checked Then
       args.Add("-shared")
     Else
       args.Add("-noshared")
     End If
     
     args.Add("-all")
     
     If chkSaveSystem.Checked Then
       args.Add("-system")
     Else
       args.Add("-nosystem")
     End If     
   Else
     args.Add("-all")
   End If
   
   CMD.Initialize("CMD", "adb", args)
   CMD.WorkingDirectory = ModMain.APP_PATH
   CMD.Run(-1)

Hope it helps someone.
 
Upvote 0
Top