Library for working with VK API, authorization through VK app, using VK functions. Minimal version of Android is 2.3
To use VK SDK primarily you need to create a new VK application here by choosing the Standalone application type. Choose a title and confirm the action via SMS and you will be redirected to the application settings page. You will require your Application ID (referenced as API_ID in the documentation). Fill in the "Batch name for Android", "Main Activity for Android" and "Certificate fingerprint for Android".
To receive your certificate's fingerprint you can use one of the following methods.
- You need to find the keystore location for your app. The ''debug'' store is usually located in these directories:
- ~/.android/ for OS X and Linux,
- C:\Documents and Settings<user>.android\ for Windows XP,
- C:\Users<user>.android\ for Windows Vista, Windows 7 and Windows 8.
The keystore for the release version is usually created by a programmer, so you should create it or recall its location.
- After the keystore's location has been found, use keytool utilite (it is supplied with the Java SDK). You can get keys list with the following command:
keytool -exportcert -alias androiddebugkey -keystore path-to-debug-or-production-keystore -list -vYou will observe a similar result:
Certificate fingerprint: SHA1: DA:39:A3:EE:5E:6B:4B:0D:32:55:BF:EF:95:60:18:90:AF:D8:07:09By deleting all the colons you'll get your key's fingerprint.
If you've already added SDK to your project, you can use the following function in each Activity of your app.
String[] fingerprints = VKUtil.getCertificateFingerprint(this, this.getPackageName());
As a rule, fingerprints contains a single line. It's a fingerprint of your certificate (depends on the certificate used for your app's signing)
You can add more than one fingerprint in your app settings, e.g., debug and release fingerprints.
You can add next maven dependency in your project:
com.vk:androidsdk:[MAVEN_CENTRAL_VERSION]
For example, your gradle script will contains such dependencies:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:20.0.0'
compile 'com.vk:androidsdk:1.6.5'
}
We give preference to Android Studio and our platform is targeted firstly to this enviroment.
You can add the library to your project using Gradle.
-
Copy the vksdk_library directory to your project's directory.
-
Find the settings.gradle file.
Most likely, it contains something like that:
include ':app'
Edit the line this way:
include ':vksdk_library',':app'
- Now your project includes vksdk_library module. You need to add it like a dependency to your app. Find the build.gradle file in the subdirectory of your app's module (e.g., YOUR_PROJECT/app/build.gradle).
Add this line to the dependencies.
compile project(':vksdk_library')Your file can be like that:
If your project doesn't support Gradle, you can add SDK by the following way:
-
Open Project Settings and select Modules.
-
Click the Add (+) button and select Import module
-
Find the directory with VK SDK and select vksdk_library, click Add.
-
Select Create module from existing sources, then click Next two times. Rename the module from "main" to "vksdk", then click Next.
-
Add the new vksdk module as a dependency to your app's module.
-
In Package explorer click right mouse button, then click Import.
-
Select Android/Existing android code into workspace.
-
Find a folder with SDK, select vksdk_library.
-
Open Properties of vksdk_library, then Java build path, then Add folder and pick "java" folder
-
In Properties of your app go to Android, add vksdk_library in the library section.
Your need to add to your manifest the following elements:
-
in the root of you need to add permission
-
in the element shoud be added
<activity android:name="com.vk.sdk.VKServiceActivity" android:label="ServiceActivity" android:theme="@style/VK.Transparent" />
to avoid possible problems with running authorizing activity.
- Add this to the resource file (example strings.xml):
<integer name="com_vk_sdk_AppId">your_app_id</integer>
- Initialize the SDK on startup using the following method. The best way is to call it in the app's onCreate method.
VKSdk.initialize(Context applicationContext);
There are several authorization methods:
VKSdk.login(Activity runningActivity, String... scope);
VkSdk.login(Fragment runningFragment, String... scope);
When succeeded call the onActivityResultMethod:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (!VKSdk.onActivityResult(requestCode, resultCode, data, new VKCallback<VKAccessToken>() {
@Override
public void onResult(VKAccessToken res) {
startTestActivity();
// User passed Authorization
}
@Override
public void onError(VKError error) {
// User didn't pass Authorization
}
})) {
super.onActivityResult(requestCode, resultCode, data);
}
}
Use AccessTokenTracker the following way:
public class Application extends android.app.Application {
VKAccessTokenTracker vkAccessTokenTracker = new VKAccessTokenTracker() {
@Override
public void onVKAccessTokenChanged(VKAccessToken oldToken, VKAccessToken newToken) {
if (newToken == null) {
// VKAccessToken is invalid
}
}
};
@Override
public void onCreate() {
super.onCreate();
vkAccessTokenTracker.startTracking();
VKSdk.initialize(this);
}
}
-
Plain request.
VKRequest request = VKApi.users().get();
-
Request with parameters.
VKRequest request = VKApi.users().get(VKParameters.from(VKApiConst.USER_IDS, "1,2"));
-
Http (not https) request (only if scope VK_PER_NOHTTPS has been passed).
VKRequest request = VKApi.users().get(VKParameters.from(VKApiConst.USER_IDS, "1,2"));
request.secure = NO;
- Request with predetermined maximum number of attempts.
VKRequest request = VKApi.wall().post(VKParameters.from(VKApiConst.OWNER_ID, "-60479154", VKApiConst.MESSAGE, "Hello, world!"));
request.attempts = 10;
//or infinite
//postReq.attempts = 0;
It will take 10 attempts until succeeds or an API error occurs.
-
Request that calls a method of VK API.
VKRequest request = new VKRequest("friends.get", VKParameters.from(VKApiConst.FIELDS, "sex,bdate,city"));
-
Request for uploading photos on user wall.
final Bitmap photo = getPhoto();
VKRequest request = VKApi.uploadWallPhotoRequest(new VKUploadImage(photo, VKImageParameters.jpgImage(0.9f)), 0, 60479154);
request.executeWithListener(new VKRequestListener() {
@Override
public void onComplete(VKResponse response) {
//Do complete stuff
}
@Override
public void onError(VKError error) {
//Do error stuff
}
@Override
public void onProgress(VKRequest.VKProgressType progressType,
long bytesLoaded,
long bytesTotal)
{
//I don't really believe in progress
}
@Override
public void attemptFailed(VKRequest request, int attemptNumber, int totalAttempts) {
//More luck next time
}
});
SDK gives a feature to execute several unrelated requests at the one call.
- Prepare requests.
VKRequest request1 = VKApi.uploadWallPhotoRequest(new VKUploadImage(photo1, VKImageParameters.jpgImage(0.9f)), 0, 60479154);
VKRequest request2 = VKApi.uploadWallPhotoRequest(new VKUploadImage(photo2, VKImageParameters.jpgImage(0.5f)), 0, 60479154);
VKRequest request3 = VKApi.uploadWallPhotoRequest(new VKUploadImage(photo3, VKImageParameters.jpgImage(0.1f)), 0, 60479154);
VKRequest request4 = VKApi.uploadWallPhotoRequest(new VKUploadImage(photo4, VKImageParameters.pngImage()), 0, 60479154);
-
Combine created requests into one.
VKBatchRequest batch = new VKBatchRequest(request1, request2, request3, request4);
-
Load the obtained request.
batch.executeWithListener(new VKBatchRequestListener() {
@Override
public void onComplete(VKResponse[] responses) {
super.onComplete(responses);
String[] photos = new String[responses.length];
for (int i = 0; i < responses.length; i++) {
VKPhoto photoModel = ((VKPhotoArray) responses[i].parsedModel).get(0);
photos[i] = String.format("photo%s_%s", photoModel.owner_id, photoModel.id);
}
makePost(VKStringJoiner.join(photos, ","));
}
@Override
public void onError(VKError error) {
showError(error);
}
});
- The result of each method returns to a corresponding requestListener. The batch VKResponse for each request in order they have been passed.
-
For use sdk purchases you must use com.vk.sdk.payments.VKIInAppBillingService instead com.android.vending.billing.IInAppBillingService like this mService = new VKIInAppBillingService(IInAppBillingService.Stub.asInterface(service));
-
when initialize app use VKSdk.initialize(this).withPayments();
-
add below code for your manifest
<receiver
android:name="com.vk.sdk.payments.VKPaymentsReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
<intent-filter>
<action android:name="com.android.vending.INSTALL_REFERRER"/>
</intent-filter>
</receiver>```
Class Reference
=========
[See the full classes reference at GitHub pages](http://vkcom.github.io/vk-android-sdk/)