VPNGN In-App SDK for Android
Installation​
Download the VPNGN SDK for Android from the Downloads page
Unarchive the file and place vpnsdk-release.aar in the libs folder of your project
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'
Create notification icon asset
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
- Add permissions to the AndroidManifest
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
- 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>
- Add the following line to gradle.properties:
android.bundle.enableUncompressedNativeLibs=false
- 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:
context - Current activity context
subdomain - project subdomain
shortToken - short token for the end-user
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
}
});