Creating and Running Your First HarmonyOS Application with DevEco Studio
After successfully installing DevEco Studio, the next step is to verify your setup by running a simple "Hello World" project. This process not only tests your development environment but also provides a foundational understanding of how to create and deploy applications for HarmonyOS devices. Here's a detailed guide on how to create a new project and run it on a HarmonyOS-powered device.
Step 1: Create a New Project
Launch DevEco Studio: Open DevEco Studio on your computer. The welcome page will greet you with several options. Click on "Create Project" to start the project creation process.
Follow the Project Creation Wizard: The wizard will guide you through the steps to set up your project. First, you need to choose the type of project you want to create. For this example, you can select either "Application" or "Atomic Service." Both options are suitable for creating a basic "Hello World" application. After making your selection, choose the "Empty Ability" template. This template provides a minimal starting point for your project, allowing you to focus on the core functionality without unnecessary complexities. Click "Next" to proceed.
Fill in Project Details: In this step, you will provide essential information about your project. Enter a name for your project, such as "HelloWorldProject." You can also specify the location where you want to save your project files. Additionally, you may need to configure other settings like the package name and the minimum HarmonyOS API level required. The package name should follow the standard Java package naming conventions (e.g.,
com.example.helloworld
). The API level determines the compatibility of your application with different versions of HarmonyOS. Ensure that the selected API level matches the version of HarmonyOS running on your device. Once you have filled in all the necessary details, click "Finish" to create the project.
Step 2: Run Hello World
Connect a HarmonyOS-Powered Device: To test your application, you need a physical device running HarmonyOS. Connect your device to your computer using a USB cable. Ensure that your device is in developer mode and that USB debugging is enabled. This can usually be done in the device's developer settings. If you don't have a physical device, you can also use the HarmonyOS emulator provided by DevEco Studio. However, running on a physical device provides a more realistic testing environment.
Configure Signing Settings: Before you can run your application, you need to configure the signing settings. Click on "File" in the menu bar, then select "Project Structure..." In the Project Structure window, navigate to "Project" > "SigningConfigs." Check the box next to "Support HarmonyOS" and enable "Automatically generate signature." This option allows DevEco Studio to automatically generate and manage the signing certificate for your application. Next, click "Sign In" and log in with your Huawei account. The auto-signing process may take a few moments to complete. Once it is done, click "OK" to save the settings.
Run the Project: With everything configured, you are now ready to run your application. Click the run button in the toolbar (usually represented by a green triangle). DevEco Studio will compile your project and deploy it to the connected HarmonyOS device. If everything is set up correctly, you should see your "Hello World" application launch on the device.
Understanding the Code
While the steps above guide you through the process of creating and running a basic "Hello World" application, it's also important to understand the underlying code. Here's a brief overview of what you might find in your project:
-
MainAbility.java: This is the main entry point of your application. It extends the
Ability
class and overrides theonStart
method. In this method, you can set up the UI components and define the behavior of your application. For a "Hello World" application, the code might look something like this:
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
// Find the text component and set its text to "Hello World"
Text text = (Text) findComponentById(ResourceTable.Id_text);
text.setText("Hello World");
}
-
layout/ability_main.xml: This is the layout file that defines the UI of your application. It contains XML elements that represent the visual components of your application. For example, you might have a
<Text>
element that displays the "Hello World" message:
<Text
ohos:id="$+id:text"
ohos:layout_width="match_parent"
ohos:layout_height="match_parent"
ohos:text="Hello World"
ohos:text_size="50fp"
ohos:text_color="#000000"/>
Top comments (0)