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 4.1.1 with buildToolsVersion=30.0.2 , Compile SDK Version = 30.0.2 and targetSdkVersion 30

At any time, you can access the complete Android Project built with this tutorial at our Github repositories

Goal

Learn how to install Parse-SDK on your Android project.

Prerequisites

To complete this section, you will need:

  • An app created at Back4App.
    • Note: If you don’t have an app now please follow the New Parse App tutorial to learn how to create a Parse app 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 official website.

Note: Parse Android SDK works with Compile SDK Version = 27 and targetSdkVersion 27 or higher.

Step 1 - Install SDK

We need to implement Parse SDK to our Android project for this we will use Gradle, an open-source build automation tool that is designed to be flexible enough to build almost any type of software. Android Studio uses Gradle for build process and import external libraries like Parse SDK.

1 - In your Android Studio project, open your build.gradle (Module:app) file.

Now we will add Jitpack to our Project. Jitpack is a package repository for JVM and Android projects.

There are two build.gradle files build.gradle (Project: YourProjectName) and build.gradle (Module: app).

If we add it to our build.gradle (Module: app) file, it will affect all modules of the project. If we add it to our build.gradle (Project: YourProjectName) file it will only affect the current app module. In this guide, we will add Jitpack to the app level gradle file.

We want to add SDK on project level that why we are using build.gradle (Module:app) in this tutorial

2 - Now we need to add repositories{} tag after the dependencies{} tag in the build.gradle (Project) file and copy the following code snippet:

1
2
3
4
5
   repositories {
     mavenCentral()
     jcenter()
     maven { url 'https://jitpack.io' }
   }

3 - It’s also necessary to look in the android{} tag if your compileSdkVersion is 27 or higher and also if your targetSdkVersion is 27 or higher. If they aren’t, you must change these versions to 27 or higher, otherwise your Parse SDK for Android may not work properly. After checking this, your build.gradle (Module:app) should look like the one in the image below.

4 - If all previous steps are set now we are able to add Parse Android SDK to our project.

In dependencies tag change the Parse-SDK-Android value with version of your choice.


1
   implementation "com.github.parse-community.Parse-SDK-Android:parse:latest-version-here"

You can see current version of SDK in here SDK Versions.

5 - Now we need to sync our build.gradle to last changes to effect our project.

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

Step 2 - Connect to Back4App

Now it is time to use Android Parse SDK in action. We need internet access and server credentials to connect to back4app.

To use the Parse SDK, our application need to have access to the internet network. To allow our app to have it, we need to grant permissions in the AndroidManifest.xml file. Also, we have to set up the app’s credentials to connect our app to Back4App. For achiving this we need to folow the steps below.

1 - Go to app > manifests > AndroidManifest.xml in your Android Studio Project.

2 - Now, go right after the application tag in the AndroidManifest.xml file and copy the following code snippet:

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

3 - Inside the application section of the AndroidManifest.xml file, add the following code:

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" />

4 - Go to app > res > values > strings.xml file.

5 - In the strings.xml file add the following code:

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>

6 - Leave the string.xml opened and go to Back4App Website, log in and click on My Apps. Find your app and then click on SERVER SETTINGS.

7 - Find the “Core Settings” block and click on Settings. The “Core Settings” block looks like this:

8 - After that, you will see the details of your Parse app, such as your App Id and your Client Key. Copy these credentials and paste them in the appropriate place at the strings.xml file of your Android Studio Project.

Step 3 - Initialize Parse SDK in Our App

In this step we will complete the Parse Initialization using App Id and Client key which we have obtained at the end of Step 2.

1 - We recommend you to write the installation codes in the App.java or App.kt files which you will create. The reason for this, to ensure that our Parse SDK initialize codes will works before any other Activity or Context, Application Context is the firs that creates and last destroy. To create your App class follow the image below.

2 - Import parse library to your App file:

1
   import com.parse.Parse;

3 - Inside App file call the following code:

app > java > com > back4app > java > example > App.java

1
2
3
4
5
6
7
8
9
10
11
   public class App extends Application {
      @Override
      public void onCreate() {
         super.onCreate();
         Parse.initialize(new Parse.Configuration.Builder(this)
                  .applicationId(getString(R.string.back4app_app_id))
                  .clientKey(getString(R.string.back4app_client_key))
                  .server(getString(R.string.back4app_server_url))
                  .build());
         }
      }

app > java > com > back4app > java > back4appexample > App.kt

1
2
3
4
5
6
7
8
9
10
11
   class App : Application() {
      override fun onCreate() {
         super.onCreate()
         Parse.initialize(
               Parse.Configuration.Builder(this)
                     .applicationId(getString(R.string.back4app_app_id))
                     .clientKey(getString(R.string.back4app_client_key))
                     .server(getString(R.string.back4app_server_url))
                     .build());
      }
   }

We put our code onCreate() method because we want to connect to our server first before taking any other action

Don’t forget to define this file in the AndroidManifest.xml. To do so, go to the AndroidManifest.xml file and add the following line of code inside the application tag:

1
android:name=".App"

If the name of the java file that extends Application that you created on the previous step isn’t “App”, don’t forget that the code above should have the correct name of the file (android:name=".name_of_the_file").

Step 4 - Test your connection

To test your connection with Parse SDK, let’s save an object in the main activity of your Android Studio Project.

  1. Go to your Android Studio Project and add the following code to your onCreate() method in order to save your first Parse Object of the application into your Dashboard.

    app > java > com > back4app > java > example > App.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    
    public class MainActivity extends AppCompatActivity {
       @Override
       protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          ParseObject firstObject = new  ParseObject("FirstClass");
          firstObject.put("message","Hey ! First message from android. Parse is now connected");
          firstObject.saveInBackground(e -> {
             if (e != null){
                Log.e("MainActivity", e.getLocalizedMessage());
                }else{
                   Log.d("MainActivity","Object saved.");
                }
             });
          }
       }
    

    app > java > com > back4app > java > back4appexample > App.kt

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    
    class MainActivity : AppCompatActivity() {
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
         setContentView(R.layout.activity_main)
         val firstObject = ParseObject("FirstClass")
         firstObject.put("message","Hey ! First message from android. Parse is now connected")
         firstObject.saveInBackground {
             if (it != null){
                 it.localizedMessage?.let { message -> Log.e("MainActivity", message) }
             }else{
                 Log.d("MainActivity","Object saved.")
             }
         }
     }
    }
    
  2. Launch your app and go to Back4App Website and click on My Apps. Find your app and then click on DASHBOARD.
  3. Now click on Core > Browser > First Class. You should see the First Class with an object, as shown in the image below.

It’s done!

We completed the section! Now you have learned how to install the Parse SDK in your application.

Learn more by walking around our Android Tutorials.