Starting Your First App

If you are starting with a fresh Java project or an existing app, follow these steps to add the Kinvey Java library to your app and set up the Kinvey Client.

Prerequisites

Manual Project Set Up

Eclipse

  1. In Eclipse, create a new Java project: File → New → Java Project. Name it appropriately (eg, "yourapp"). Set Java execution environment to 1.6. Click Finish.
  2. Download the latest Kinvey library (zip) and extract the downloaded zip file.
  3. Right click on your project → Build Path → Configure Build Path and on the Libraries tab click Add External Jars. Navigate to where you downloaded the Kinvey Jars and add them all.
  4. In Eclipse, right click the project → Close Project → right click project (again) → Open Project.

Add an App Backend

Start with signing up if you don't have an account with Kinvey yet.

In the Kinvey console, click Create an App and enter the name of your app when prompted.

Add an app in Console

You can find your key and secret in the dropdown menu in the environment sidebar.

Key and secret

Copy the key and secret when performing the next steps.

Initialize a Client

The Client.Builder is used to build and initialize the Client before making any calls to the Kinvey API.

The Client.Builder requires the App Key and App Secret obtained from the Kinvey console. Initializing a Client is usually done when your application first starts.

import com.kinvey.nativejava.Client;
...
final Client mKinveyClient = new Client.Builder(your_app_key, your_app_secret).build();

For Dedicated Kinvey customers, you'll need to set the base URL for the library to your dedicated Kinvey host URL. You can find this host URL on the dashboard of the console, next to your App Key and App Secret:

final Client mKinveyClient = new Client.Builder(your_app_key, your_app_secret).setBaseUrl("<your dedicated host URL>").build();

Verify Set Up

You can use myClient.ping() to verify that the app has valid credentials. The following code example:

  • builds a new client
  • pings the backend with the app credentials
import import com.kinvey.nativejava.Client;

...

Client myJavaClient = new Client.Builder(appKey, appSecret).build();
try{
    Boolean pingResult = myJavaClient.ping();
    System.out.println("Client ping result -> " + pingResult);
}catch(Exception e){
    System.out.println("something went wrong!");
    e.printStackTrace();
}

Authenticating with Master Secret

While your client-side apps are used by real-life human beings, your server-side code is going to need more control without managing an active user's lifecycle. This can be accomplished by logging in with your app key and master secret, which will be used to authenticate all requests made against your backend.

After building your Client, you can call:

try{
    myJavaClient.user().loginBlocking(MY_APP_KEY, MY_MASTER_SECRET).execute();
    System.out.println("Client logged in -> " + myJavaClient.user().isUserLoggedIn());
}catch (IOException e){
    System.out.println("Couldn't login -> " + e);
    e.printStackTrace();
}

What's next

You are now ready to start building your awesome apps! Next we recommend diving into the User guide or Data store guide to learn more about our service, or explore the sample apps to go straight to working projects.