Skip to main content

VPNGN In-App SDK for Android

Installation​

  1. Download the VPNGN SDK for Android from the Downloads page

  2. Unarchive the file and place vpnsdk-release.aar in the libs folder of your project

  3. Add the following dependencies to build.gradle:

    implementation fileTree(include: ['vpnsdk-release.aar'], dir: 'libs')
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.9.1'
implementation 'com.wireguard.android:tunnel:1.0.20211029'
implementation 'androidx.webkit:webkit:1.4.0'
  1. Create notification icon asset

  2. Declare manifest parameters in AndroidManifest.xml under application section:

        <meta-data
tools:replace="android:value"
android:name="VPN_NOTIFICATION_ICON"
android:value="ic_launcher" />

android:value should be set to the name of icon asset created before

  1. Add permissions to the AndroidManifest
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
  1. Add Wireguard service reference to AndroidManifest:
    <service
android:name="com.wireguard.android.backend.GoBackend$VpnService"
android:exported="false"
android:permission="android.permission.BIND_VPN_SERVICE">
<intent-filter>
<action
android:name="android.net.VpnService" />
</intent-filter>
</service>
  1. Add the following line to gradle.properties:
    android.bundle.enableUncompressedNativeLibs=false
  1. Add the following property to application tag in AndroidManifest:
    android:extractNativeLibs="true"

Usage​

Use the VPNGN.start method in order to start VPNGN activity:

    public static void start(@NotNull android.content.Context context,
@Nullable String subdomain,
String shortToken,
@Nullable VPNGN.OnError errorHandler)

Where:

  1. context - Current activity context

  2. subdomain - project subdomain

  3. shortToken - short token for the end-user

  4. errorHandler - callback to be invoked in case of error

    VPNGN.OnError interface has a following definition:
public interface OnError {
void onError();
}

Invocation of start method will lead to showing new activity. The previous activity will be shown when the end-user closes the activity.

Example:

    VPNGN.start(context, subdomain, token, new VPNGN.OnError() {
@Override
public void onError() {
Toast.makeText(MainActivity.this, "Error happened", Toast.LENGTH_LONG).show();
}
});

Traffic Counter (optional)​

Call the VPNGN.setIOCounter method before calling VPNGN.start:

    VPNGN.setIOCounter(OnIOCount ioCounterHandler)

Example:

    VPNGN.setIOCounter(new VPNGN.OnIOCount() {
@Override
public void OnCount(long bytesIn, long bytesOut) {
// your code here
}
});