Forum Discussion

vatbub's avatar
vatbub
Contributor
8 years ago
Solved

Testcomplete and the new permission system in Android

Hey guys, as you might know as an Android developer, the new permission system introduced with Android M is awsome from the users point of view but a real pain in the bum for devs and automated qa. ...
  • vatbub's avatar
    vatbub
    8 years ago

    Heyho,

    I just wanted to post the solution we found here so that anybody can look it up.

    The problem was that our app already requests the GPS permission  at startup so there is no way we can grant the permission after launching the app. On the other hand it makes no sense to grant the permission before installing the app. The solution is to split up the process of installing and launching the app on the phone:

    Instead of using

    AndroidTestedApp.Run()

    which installs and launches the app,

    we now use the following:

    // Install the app
    var packObj = Mobile.Device.PackageManager.GetPackageFromAPK(AndroidTestedApp.APKFileName);
    
    Mobile.Device.PackageManager.RemovePackage(packObj);
    
    if (!Mobile.Device.PackageManager.InstallPackage(packObj)){
      Log.Error("Something went wrong while installing the application.");
    }
    
    // Allow all permissions if necessary
    
    var oShell = new ActiveXObject("WScript.Shell"); 
    var grantFineGPSCommand =  "adb shell pm grant " + packageName + " android.permission.ACCESS_FINE_LOCATION";
    
    try {
            Log.Message("Granting fine GPS access for app " + packageName, "Using command: " + grantFineGPSCommand);
            oShell.Run(grantFineGPSCommand);
    } catch (e) {
            Log.Warning(e);
    }
          
        
    // Launch the application      
    if (!Mobile.Device.PackageManager.LaunchPackage(packObj)){
        Log.Error("Something went wrong while launching the application.");
        return;
    }

    As you can see, this allows us to put the adb commands in between installing and launching the app and no popups appear.

     

    Greetings,

    vatbub