Create an application in Android Studio

更新时间:
复制 MD 格式

Create an Android application that displays a Toast message on button click and generate a signed APK installation package.

The procedure 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, skip this tutorial and directly create an application in the mPaaS console.

Create a project

  1. Open Android Studio and click File > New > New Project.

  2. In the new project window, select Empty Activity and click Next.

  3. Enter a Name, Package name, and Save location. You can use the default package name. This tutorial uses Scan Application as the name. Set Minimum SDK to API 18: Android 4.3 (Jelly Bean).

    Note

    API 18: Android 4.3 (Jelly Bean) is the minimum version supported by mPaaS. Select a different version for production as needed.

  4. Click Finish to create the project.

Write the code

  1. Open the activity_main.xml file and add a button with the following code.

     <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" />
  2. Open the MainActivity class and add the click event 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();
             }
         });
  3. After the code compiles successfully, the coding step is complete.

Create a signature file and sign the project

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

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

  3. Click Create new.

  4. Enter the required information and click OK to create the signature. The generated signature file is available in the specified Key store path.

  5. After the fields are automatically populated, click Next to sign the project.

  6. Select a build variant as required. Note the build variant because you must select the same type when you use the encrypted file. For signature versions, you must select V1 (Jar Signature). V2 (Full APK Signature) is optional.

  7. Click Finish. After packaging completes, the signed APK is available in the debug folder of your project directory (~\MyHApplication\app\debug). In this tutorial, the installation package 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. Run the project.

  3. Click BUTTON. A Toast message appears, confirming the application is installed and working correctly.