Create a native project in Android Studio

更新时间:
复制 MD 格式

Create an application that displays a Toast message when a button is clicked, and generate a signed APK.

The process consists of four steps:

  1. Create a project

  2. Write the code

  3. Create a signature file and sign the project

  4. Install the application on a mobile phone

If you already have a signed native Android project, proceed to Create an application in the mPaaS console.

Prerequisites

Before creating a project, ensure that you have installed the mPaaS plugininstalled the mPaaS plugin.

Create a project

  1. Open Android Studio. Click File > New > New Project.

  2. In the New Project window, select Empty Activity and click Next.image.png

  3. Set the Name, Package name, and Save location for the project. This tutorial uses mPaaS CDP as the project name and API 18: Android 4.3 (Jelly Bean) as the minimum SDK version.image.png

  4. Click Finish to create the project.

Write the code

  1. Open the activity_main.xml file and add a button to the layout.

     <Button
          android:id="@+id/button"
          android:layout_width="101dp"
          android:layout_height="50dp"
          android:layout_marginStart="142dp"
          android:layout_marginTop="153dp"
          android:layout_marginBottom="151dp"
          android:text="Button"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent" />

    image.png

  2. Open the MainActivity class and add a click event handler for the button.

         findViewById(R.id.button).setOnClickListener(new View.OnClickListener(){
             @Override
             public void onClick(View v) {
                 Toast.makeText(MainActivity.this, "Hello mPaaS!", Toast.LENGTH_SHORT).show();
             }
         });

    image.png

  3. After the project compiles successfully, the code is ready.

Create a signature file and sign the project

A signed APK file is required to configure components in the mPaaS console.

To sign the project and generate the APK file:

  1. In Android Studio, click Build > Generate Signed Bundle / APK....image.png

  2. In the window that appears, select APK and click Next.image.png

  3. Click Create new.image.png

  4. Enter the required information and click OK. The signature file is generated in the path that you specified for Key store path.image.png

  5. After the fields auto-fill, click Next to sign the project.image.png

  6. Select the desired Build Variant. You must select the V1 (Jar Signature) checkbox. You can also select the V2 (Full APK Signature) checkbox if needed.image.png

  7. Click Finish. After the build completes, the signed APK file is located in the debug folder (`~\mPaaSCDP\app\debug`). In this tutorial, the file is named app-debug.apk.

Install the application on a mobile phone

  1. Connect your mobile phone to your computer and enable USB debugging mode on the phone.

  2. In Android Studio, run the project to install the application on your phone.

  3. Open the application on your phone. Click Button. A Toast message that says "Hello mPaaS!" appears at the bottom of the screen, confirming that the application works as expected.