Development Environment Requirement
Android Studio 2.0+
Android 7.0 (SDK API 24) and above
SDK Reference and Dependency Configuration
1. Maven support for importing.
//Multi-network acceleration SDK
implementation 'com.tencent.linkboost:mpacc:2.9.4'
2. Add via AAR package
Note:
If you add the SDK via Maven, you can skip this step.
Copy the AAR file to the project directory (for example: app/libs).
Add the following dependency configuration to the Gradle file:
dependencies {
//add SDK dependency
implementation fileTree(dir: 'libs', include: ['*.aar'])
//add gson dependency
implementation 'com.google.code.gson:gson:2.8.9'
//add encryption library dependency
implementation 'androidx.security:security-crypto:1.0.0'
}
International Site Configuration
The `setEnv` API of the `MpAccRegister` class defaults to the Mainland China site.
// `Context` refers to the `ApplicationContext`. `env` is an integer value: 0 for the Mainland China site, 1 for the International site.
MpAccRegister.setEnv(context, env);
Sample Code
VPN Mode
Initializing MpAccClient
Note:
SDK authentication also supports application-based integration. You can call MpAccClient.setSign("appid","*****").
You only need to configure either device-based integration or application-based integration.
private void initMpAcc() {
// The datakey applied from Tencent Cloud needs to be imported here (use device connectivity).
MpAccClient.setDataKey("test-123456", "*");
mpAccClient = MpAccClient.getInstance(this);
}
Start acceleration
try {
mpAccClient.registerAccCallback(accCallback);
//vpn acceleration requires user authorization
Intent vpnIntent = MpAccClient.prepare(this);
if (vpnIntent != null) {
startActivityForResult(vpnIntent,
VPN_REQUEST_CODE);
} else {
onActivityResult(VPN_REQUEST_CODE, RESULT_OK,
null);
}
} catch (MpAccSDKException e) {
e.printStackTrace();
}
}
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intentdata) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == VPN_REQUEST_CODE && resultCode == RESULT_OK) {
try {
AccConfig accConfig = new AccConfig();
ArrayList<String> list = new ArrayList<>();
accConfig.setAccMode(AccConfig.ACC_MOD_BONDING); //set acceleration mode
accConfig.setRoute("0.0.0.0", 0); //ip list for acceleration
//list.add("air.tv.douyu.android"); //input the package name to accelerate, default if not passed accelerates ALL applications
accConfig.setWhiteList(list, 0);
mpAccClient.startAcc(accConfig);
} catch (MpAccSDKException e) {
e.printStackTrace();
}
}
}
Releasing Resources
//Free up resources when exiting, such as calling in the Activity's onDestroy method
private void stopAcc() {
try {
mpAccClient.unregisterAccCallback(accCallback);
mpAccClient.stopAcc();
} catch (MpAccSDKException e) {
e.printStackTrace();
}
}
SOCKS Mode
Note:
SOCKS mode acceleration does not require user authorization. The initialization, authentication, and status callback logic of MpAccClient is the same as in VPN mode.
private void initMpAcc() {
MpAccClient.setDataKey("test-123456", "*");
mpAccClient = MpAccClient.getInstance(this);
}
//Trigger acceleration (socks mode)
private void startSocksAcc() {
AccConfig accConfig = new AccConfig();
accConfig.setAccMode(AccConfig.ACC_MOD_BONDING)
.setPingInterval(3)
.setEnableSocks(true) //Socks mode needs to be set to true
.setSocksPort(1080);//Set proxy port
try {
//Listen to acceleration status callback
mpAccClient.registerAccCallback(accCallback);
mpAccClient.startAcc(accConfig);
} catch (MpAccSDKException e) {
e.printStackTrace();
}
}
private void stopSocksAcc() {
try {
mpAccClient.unregisterAccCallback(accCallback);
mpAccClient.stopAcc();
} catch (MpAccSDKException e) {
e.printStackTrace();
}
}
Note:
After acceleration is successfully started in SOCKS mode (that is, after receiving the onAccSuccess callback), your service must send network requests through the SOCKS protocol.
The proxy IP is the local IP, and the proxy port must be the same as the port set through setSocksPort when starting acceleration. Only then can the traffic enter the acceleration tunnel.
The following are common examples of switching network libraries to SOCKS5:
//Example of sending packets with socks5 over UDP
private void testUdpSocks() {
try {
Log.i(TAG, "start testUdp");
//1. The port should be consistent with the setSocksPort used to trigger acceleration.
SocksProxy socksProxy = new Socks5(new
InetSocketAddress("localhost", 1080));
//2. Set socksProxy to DatagramSocket
DatagramSocket clientSocket = new
Socks5Datagram(socksProxy);
//3. Perform normal packet sending business
String message = "Hi, I am UDP client";
byte[] sendBuffer = message.getBytes();
DatagramPacket packet =
new DatagramPacket(sendBuffer, sendBuffer.length,
new InetSocketAddress("106.55.119.181", 8888));
clientSocket.send(packet);
//Received response message from UDP server.
} catch (IOException e) {
e.printStackTrace();
}
}
//HttpURLConnection uses socks Proxy
private void testHttpSocks() {
BufferedReader buff = null;
HttpURLConnection urlConnection = null;
try {
Create a proxy agent
Proxy socksProxy = new Proxy(Proxy.Type.SOCKS, new
InetSocketAddress("127.0.0.1", 1080));
URL url = new URL("url");
//Add Proxy to HttpURLConnection
urlConnection = (HttpURLConnection)
url.openConnection(socksProxy);
//Perform normal packet sending business
} catch (Exception e) {
e.printStackTrace();
}
}
//OKHttp uses socks Proxy
private void testOkHttpSocks(){
Create a proxy agent
Proxy socksProxy =new Proxy(Proxy.Type.SOCKS,
new InetSocketAddress("127.0.0.1", 1080));
//Add Proxy to OkHttpClient
OkHttpClient client = new
OkHttpClient.Builder().proxy(socksProxy).build();
//Perform normal packet sending business
}
//TCP Socket uses socks Proxy
private void testSocketSocks() {
Proxy socksProxy =new Proxy(Proxy.Type.SOCKS,
new InetSocketAddress("127.0.0.1", 1080));
Socket socket = new Socket(socksProxy);
//...
}
C++ SOCKS Mode
For TCP and UDP access to SOCKS, see:
FAQs
1. If I use the SDK in VPN mode for acceleration, will acceleration still take effect if another third-party app also enables VPN?
Answer:No. By default, Android allows only one VPN service at a time. If another application also enables VPN, the current acceleration VPN service will be released, the onAccFail callback will be triggered with error code -5, and acceleration will stop.
2. How do I use SOCKS mode for acceleration?
Answer:To use SOCKS mode for acceleration, in addition to calling startAcc to configure the acceleration mode and SOCKS port, you must also encapsulate your service-related network interfaces to send traffic through a SOCKS5 proxy. For examples, see the SOCKS mode sample code.
3. What is the difference between SOCKS mode and VPN mode acceleration?
Answer:Both modes are designed to obtain service data traffic for accelerated forwarding.VPN mode requires the user to approve an authorization prompt, and a small key icon appears in the system UI. In VPN mode, any third-party application on the phone can be accelerated.SOCKS mode does not require user authorization, but you must modify the service network sending interface so that traffic is sent through a SOCKS proxy.
4. Can dual Wi-Fi or a custom USB network adapter be used for multi-network acceleration?
Currently, the Android SDK supports acceleration for three types of network adapters: Wi-Fi, MOBILE, and ETH. You can call setAccLinks to add the network adapters you want to use for acceleration.
If you want to add a network adapter type that is not supported by the SDK, you can dynamically add a custom network adapter through addNetworkPlugin. For plugin code, see:
5. What is the difference between the onStopMpAcc and onAccException callbacks in MeasureCallback?
Answer:The SDK callback onStopMpAcc notifies the user to stop acceleration. It is mainly triggered when the device is in a single-network-adapter environment, when the default route network has returned to normal, or when there has been no obvious acceleration benefit for a long time. Whether to finally stop acceleration (stopAcc) is up to the user.
The SDK callback onAccException indicates that a major exception has occurred during acceleration, such as network disconnection after acceleration starts, excessively high latency, Negative optimization, or acceleration results that are far below expectations. In this case, the SDK proactively stops acceleration and speed testing so that service traffic returns to the origin path, preventing the acceleration exception from affecting normal service usage.
6. How can I obtain the total traffic used in a single acceleration session and the bandwidth rate of each link during acceleration?
Answer:AccCallback provides the onAccDataUpdate callback. Its parameters include the total accelerated uplink and downlink traffic, as well as detailed information about each link (PathDetail).
7. The minSdkVersion of the SDK is 24, which is higher than the minSdkVersion configured in my app. How do I resolve the packaging/compilation error?
Answer:Add the following configuration to the manifest file, and check the system version dynamically in code. If the current system version is lower than 24, do not call the SDK APIs.
<uses-sdk tools:overrideLibrary="com.android.linkboost.multi,androidx.security"/>
8. After acceleration is initiated, even without actual service requests, the onAccDataUpdate callback still reports accelerated traffic. What is the reason for this?
Answer: Processes such as internal SDK handshake negotiation and protocol probing generate traffic. This traffic is minimal and negligible, and it is not included in acceleration billing.