All Products
Search
Document Center

Create an Application in Android Studio

Last Updated: May 21, 2021

In this section, you will create an app that causes a Toast to appear when a user clicks a button, and obtain the installation package in APK format.

This process mainly includes the following 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 native Android project and the project has already been signed, skip this tutorial and directly Create an application in the mPaaS console.

Create a project

  1. Start Android Studio and choose File > New > New Project.
    Start
  2. In the Create New Project dialog box that appears, select Empty Activity and click Next.
    Select
  3. Enter a name in Name, enter a package name in Package name, and specify a project location in Save location. Note that the default package name can be used. For example, enter Scan Application in the Name entry box. From the Minimum SDK drop-down list, select API 18: Android 4.3 (Jelly Bean).
    Note: API 18: Android 4.3 (Jelly Bean)” is the earliest version that the mPaaS supports. You can select a version according to the actual production requirements.
    1
  4. Click Finish. The Create a project procedure is complete.

Write the code

  1. Open the activity_main.xml file and refer to the following code to add a button.
    1. <Button
    2. android:id="@+id/button"
    3. android:layout_width="101dp"
    4. android:layout_height="50dp"
    5. android:layout_marginStart="142dp"
    6. android:layout_marginTop="153dp"
    7. android:layout_marginBottom="151dp"
    8. android:text="Button"
    9. app:layout_constraintStart_toStartOf="parent"
    10. app:layout_constraintTop_toTopOf="parent" />
    2
  2. Open the MainActivity class and add a click event to the button.
    1. findViewById(R.id.button).setOnClickListener(new View.OnClickListener(){
    2. @Override
    3. public void onClick(View v) {
    4. Toast.makeText(MainActivity.this, "Hello mPaaS!", Toast.LENGTH_SHORT).show();
    5. }
    6. });
    3
  3. Compile the code. After successful compilation, the Write the code procedure is completed.

Create a signature file and sign the project

  1. In Android Studio, choose Build > Generate Signed Bundle / APK.
    4
  2. In the dialog box that appears, select APK and click Next.
    Select APK
  3. Click Create new.
    Create key
  4. Enter all the required information and click OK. The signature creation is complete. You can obtain the generated signature file in the path specified in Key store path.
    4
  5. After the fields are automatically filled, click Next to start signing the project.
    5
  6. Select a build variant in Build Variants as needed. Remember the selected build variant. You need to select the same variant when you use the encrypted file.
    Then select the encrypted version V1 (Jar Signature). Note that V1 (Jar Signature) is required while V2 (Full APK Signature) is optional.
    6
  7. Click Finish. After the packaging is complete, you can locate the signed APK installation package in the debug folder under the project directory (~\MyHApplication\app\debug). In this tutorial, the name of the installation package is app-debug.apk.
    APK

Install the application on a mobile phone

  1. Connect a cell phone to the computer and enable the USB Debugging mode of the cell phone.
  2. Run the project.
    5
  3. Click BUTTON. A toast notification appears, as shown in the following figure. This notification indicates that you have successfully installed the application with the expected features. Now you complete the installation of the application on a mobile phone.