Android VpnService.Builder.establish () sporadically null

When I try to create a tunnel interface for my VpnService, I get the following error:

Attempt to invoke virtual method 'java.lang.String android.os.ParcelFileDescriptor.toString()' on a null object reference

      

The only thing I have is to reboot the device when this happens. If I don't, I won't be able to create a tunnel at all.

My code for creating a tunnel:

// This is inside my VpnService implementation 

private ParcelFileDescriptor configureTunnelWithPushOpts(PushOpts popts)
{
    VpnService.Builder builder = this.new Builder();

    builder.setMtu       ( currentServerPrefs.SERVER_MTU );
    builder.addAddress   ( popts.ip, 32                  );
    builder.addDnsServer ( popts.dns1                    );
    builder.addDnsServer ( popts.dns2                    );
    builder.addRoute     ( "0.0.0.0", 0                  );


    // Note: Blocking mode is now enabled in native
    // code under the setFileDescriptor function.
    // builder.setBlocking(true);

    builder.setConfigureIntent(
            PendingIntent.getActivity(
                    this,
                    0,
                    new Intent(
                            this,
                            MainActivity.class
                    ),
            PendingIntent.FLAG_UPDATE_CURRENT)
    );

    final ParcelFileDescriptor vpnInterface;

    synchronized (this) {
        builder.setSession(currentServerPrefs.SERVER_ADDRESS);
        vpnInterface = builder.establish();
    }

    Logger.info("New interface: " + vpnInterface.toString(), this);
    return vpnInterface;
}

      

Edit:

As per the documentation, the function establish

will return null

if the VpnService has not been prepared, however I am using that to prepare it before the above function is run.

// This is inside my MainActivity class

Intent intent = VpnService.prepare(getApplicationContext());
if (intent != null) {
    startActivityForResult(intent, 0);
} else {
    onActivityResult(0, RESULT_OK, null);
}

. . . 

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        Intent intent = new Intent(this, MyVpnService.class);
        startService(intent);
    }
}

      

The phone I'm using for debugging

Google Nexus 5 Running Android 5.0.1

Edit

I figured out how to replicate it so that it doesn't get sporadic. Basically, if I uninstall the app while the VPN is connected and then reinstall it and try to start it back, I get a null response when I start it builder.establish()

. I'm afraid this could create potential problems when the app is updated through the Google Play Store.

Other than that, do not list this question as a duplicate. Every other question on this question has been answered that the service must be provisioned before the builder is created, however I am preparing it in my case and have a different reason for my problem. Any help would be greatly appreciated.

+5


source to share


1 answer


Thanks a lot for the hint, this led me to a solution!

I had the same problem in Android 5.0.2: everything worked fine and I installed a vpn app that I am developing. The next day, an unsolvable one suddenly appeared NullPointerException

, although it prepare

was correctly named.



The problem can be solved in my environment with the following steps:

  • Delete the application. It doesn't matter if the app is still running with VPN functionality or not.
  • Reboot your phone. Make sure it is completely disabled.
  • After rebooting, make sure the app is no longer installed (I had some weird development issues where the app was reinstalled after rebooting my phone).
  • Install the VPN app again. Now it should get the vpn interface instead of null

    .
+2


source







All Articles