Android

Install Parse SDK on your Android Studio Project

Introduction

In this section you learn how to install Parse Android SDK into your Android Studio project.

This tutorial uses a basic app created in Android Studio 2.3.3 with Compile SDK Version = 26 and Min SDK Version = 16

At any time, you can access the complete Android Project built with this tutorial at our GitHub repository.

Prerequisites

To complete this tutorial, you need:

  • An app created at Back4App.
  • Android Studio.
  • Basic android app.
    • Note: If you don’t have a basic app created you can follow the Create a Project tutorial from Android Studio Website. After you create your basic app you are ready to follow this guide.

Note: Parse Android SDK works with Android 2.3 or higher.

Step 1 - Install SDK

Follow this step if you haven’t yet installed Parse Android SDK.

Android Studio uses Gradle (an advanced build toolkit) to automate and manage the build process while allowing you to define flexible custom build configurations.
You can refer to Android Studio’s Build Guide for additional details.

To install de Parse SDK for Android in your Android Studio project, open your app/build.gradle file:

build.gradle file from module app

Be careful, there are two files named build.gradle.

The Google HTTP Client Library for Java is in the central Maven repository. In summary Maven is designed to apply patterns to a project’s build infrastructure. The objective is to promote comprehension and productivity by clearing indicating best practices.

For additional details see the Maven Getting Started guide and Android Studio Build Dependencies guide.

To add a Maven repository, go to your build.gradle file, copy the following code snippet and paste it right after dependencies{}:

build.gradle

1
2
3
repositories {
    mavenCentral()
}

Adding Maven repository

To install the SDK, you need to add the following lines inside dependencies{} block. Remember to update the version of Parse SDK for Android to the latest one. You can find it at Parse Community GitHub repository.

build.gradle

1
compile 'com.parse:parse-android:1.16.3' //update version to the latest one

Sync your build.gradle and we are ready to go.

Synchronizing build.gradle file

Troubleshooting:

If you got an error message related to support library (as in the image below):

Error with support repository

You can solve it by adding the repositories maven {url "http://maven.google.com"} and jcenter(). At the end your repositories{} section should be as follows:

build.gradle

1
2
3
4
5
6
7
  repositories {
      mavenCentral()
      jcenter()
      maven {
          url "http://maven.google.com"
      }
  }

Click on Try Again to sync your build.gradle and we are ready to go.

Synchronizing build.gradle file again

To learn more about adding support libraries, see the Android Studio’s Support Library Setup page.

Step 2 - Set up App Permissions and Libs

Granting Permissions

You need to grant permission for your Android app to access the internet network.

To do this, add the following code snippet to your AndroidManifest.xml file:

AndroidManifest.xml

1
2
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>

Your code should be similar to the following one:

AndroidManifest.xml file

Adding Libs

To use Parse Android SDK functions you need to import libs to your code.

Choose the MainActivity.java file and import the following libs:

MainActivity.java

1
2
3
4
5
import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseUser;
import com.parse.ParseInstallation;

Your code now should be similar to the following one:

MainActivity file

Step 3 - Connect your Parse App

Update your meta-data and strings to set up the app’s credentials. Use Parse.Initialize method to call those credentials and connect with Back4App servers.

Setting up your project

Go to your Manifest file and inside application tag add the following code:

AndroidManifest.xml

1
2
3
4
5
6
7
8
9
  <meta-data
      android:name="com.parse.SERVER_URL"
      android:value="@string/back4app_server_url" />
  <meta-data
      android:name="com.parse.APPLICATION_ID"
      android:value="@string/back4app_app_id" />
  <meta-data
      android:name="com.parse.CLIENT_KEY"
      android:value="@string/back4app_client_key" />

Go to your Strings file and add the following code:

strings.xml

1
2
3
4
5
    <string name="back4app_server_url">https://parseapi.back4app.com/</string>

    <!-- Change the following strings as required -->
    <string name="back4app_app_id">PASTE_YOUR_APPLICATION_ID_HERE</string>
    <string name="back4app_client_key">PASTE_YOUR_CLIENT_KEY_HERE</string>

Find your Application ID and your Client Key

  1. Go to your App Dashboard at Back4App website.
  2. Navigate to app’s settings: Click on Server Settings > Core Settings block > Settings.
  3. Return to your Strings file and paste your applicationId and clientKey.

See more at our New Parse App guide.

Initialize your Parse app

Inside your onCreate method, right after super.onCreate() call, add the following code:

MainActivity.java

1
  Parse.initialize(this);

And right after Parse.initialize method add the following code to test your app in the future:
MainActivity.java

1
    ParseInstallation.getCurrentInstallation().saveInBackground();


At the end your code should appear as follows:

Final MainActivity.java file

Step 4 - Test your connection

  1. Build your app in a device or virtual device (Shift+F10).
  2. Wait until the main screen appears.
  3. Login at Back4App Website.
  4. Find your app and click on Dashboard.
  5. Click on Core.
  6. Go to Browser.
  7. If everything works properly, you should find a class named Installation.

It’s done!

At this point, you have learned how to get started with Android apps.

Learn more by walking around our Android Tutorials or check Parse open source documentation for Android SDK.