In this section, you will create an application that displays a Toast message when a button is clicked. You will also generate an APK installation package.
The process has four main steps:
If you already have a signed, native Android project, you can skip this tutorial and go directly to Create an application in the mPaaS console.
Create a project
Open Android Studio and click File > New > New Project.
In the window that appears, select Empty Activity and click Next.
Enter a Name, Package name, and Save location. For this tutorial, set the Name to H5 Application. You can use the default package name. Set Minimum SDK to API 18: Android 4.3 (Jelly Bean).
NoteAPI 18: Android 4.3 (Jelly Bean) is the minimum version that mPaaS supports. Select the version required for your production environment.
Click Finish to create the project.
Write the code
Open the
activity_main.xmlfile and add a button using 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" />Open the
MainActivityclass and add a 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(); } });A successful compilation means you have finished writing the code.
Create a signature file and sign the project
In Android Studio, click Build > Generate Signed Bundle / APK.
In the window that appears, select APK and click Next.
Select Create new.
Enter the required information and click OK. The signature file is generated in the specified Key store path.
After the content is automatically populated, click Next to add a signature to the project.
Select a Build Variant based on your needs. Then, check V1 (Jar Signature). This option is required, while V2 (Full APK Signature) is optional.
Click Finish. After the packaging process is complete, the signed APK installation package is located in the
debugfolder (~\MyHApplication\app\debug) of your project. In this tutorial, the package is namedapp-debug.apk.
Install the application on a mobile phone
Connect your phone to the computer and enable USB debugging mode.
Run the project.
Click BUTTON. A Toast message appears. This confirms that the application is installed and works as expected.