The Android Open Source Project (AOSP) is a vast collection of code that serves as the foundation for the Android operating system. When working with AOSP, developers often need to add new applications directly into the Android build system. In this tutorial, we will walk you through how to create a simple HelloWorld application, place it in the packages/apps
directory, and compile it as part of the AOSP source code.
This step-by-step guide is designed for those new to AOSP development or developers looking for a simple example to get started with customizing AOSP. By the end of this guide, you’ll have a basic understanding of how to integrate and build an app with AOSP.
What You’ll Need:
- A working AOSP build environment set up on your machine.
- Basic understanding of Android app development (Java or Kotlin).
- Familiarity with AOSP’s file structure.
Step 1: Set Up Your AOSP Environment
Before we dive into the actual code, make sure you have your AOSP environment properly set up. If you haven’t set it up yet, you can follow the official AOSP build instructions to download and configure the source code on your machine.
Make sure that the following commands work:
# Initialize AOSP repository
repo init -u https://android.googlesource.com/platform/manifest
# Sync the AOSP source code
repo sync
# Set up environment for building
source build/envsetup.sh
# Choose your target (example: aosp_x86_64-eng)
lunch aosp_x86_64-eng
Now that your environment is ready, we can create our HelloWorld app.
Step 2: Create the HelloWorld Application
In AOSP, all Android applications that are part of the build reside in the packages/apps
directory. Let’s create a new directory for our HelloWorld app inside packages/apps
.
Create the HelloWorld Directory:
Navigate to the packages/apps
directory in your AOSP source:
cd packages/apps
mkdir HelloWorld
Inside the HelloWorld
directory, we’ll create the following files:
AndroidManifest.xml
src/com/example/helloworld/MainActivity.java
res/layout/activity_main.xml
res/values/strings.xml
Android.mk
orAndroid.bp
(depending on your AOSP version)
AndroidManifest.xml
The AndroidManifest.xml
file is crucial because it defines the app’s structure. Create the AndroidManifest.xml
file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.helloworld">
<application
android:label="HelloWorld"
android:icon="@mipmap/ic_launcher">
<activity
android:name=".MainActivity"
android:label="HelloWorld">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java
Next, we’ll create the main activity file for the HelloWorld app. Create the following directory structure: src/com/example/helloworld/
and inside that, create MainActivity.java
.
package com.example.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create a TextView to display "Hello, World!"
TextView textView = new TextView(this);
textView.setText("Hello, World!");
setContentView(textView);
}
}
activity_main.xml (Optional)
If you prefer to define the layout in XML, you can create the activity_main.xml
file under res/layout/
.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/helloWorldText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, World!" />
</LinearLayout>
strings.xml
Now, create the res/values/strings.xml
file to define the string resource:
<resources>
<string name="app_name">HelloWorld</string>
<string name="hello_world">Hello, World!</string>
</resources>
Step 3: Set Up Build Configuration
To compile the HelloWorld app as part of the AOSP build system, you’ll need to define either an Android.mk
file or an Android.bp
file.
Option 1: Android.mk
Create an Android.mk
file in the HelloWorld directory:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := HelloWorld
LOCAL_SRC_FILES := src/com/example/helloworld/MainActivity.java
LOCAL_PACKAGE_NAME := HelloWorld
LOCAL_SDK_VERSION := current
include $(BUILD_PACKAGE)
Option 2: Android.bp
For newer AOSP versions using the Android.bp
build system, create an Android.bp
file:
android_app {
name: "HelloWorld",
srcs: ["src/**/*.java"],
sdk_version: "current",
}
Step 4: Compile and Build the Application
Now that you’ve set up the HelloWorld app and its build configuration, you can compile it as part of the AOSP source.
Build the AOSP with the HelloWorld App
Make sure your environment is still set up (e.g., source build/envsetup.sh
), and run the following command to compile your app as part of the AOSP build:
m HelloWorld
This will build the HelloWorld app and include it in the system image. Once the build is complete, you can flash the image to an emulator or device to see the app.
Step 5: Verify and Run the App
After flashing your new system image to the device or emulator, you should be able to find the HelloWorld app in the app drawer. Launch it, and you should see the “Hello, World!” message on the screen.
In this tutorial, we covered how to create a simple HelloWorld application and integrate it into the Android Open Source Project. We walked through setting up the app in packages/apps
, defining the necessary Android and Java files, and compiling the app using the AOSP build system.
With this basic knowledge, you can now start building and adding more complex applications to the AOSP source. Understanding how to add custom apps to AOSP is an important step in Android development, especially for developers looking to customize or build their own Android systems.