AOSP: Creating a System Application
Overview
- Download Android Open Source Project
- Build Android Open Source Project
- Build an Android Application inside AOSP
- Build an Android Application with Native Code inside AOSP
- Build an Android Application with
.so
and.jar
libraries. - Build a System Application
Prerequisite
If you don’t have enough powerful PC, most probably your build will take hours and it’s not a fact that you will succeed in the end.
On my PC which has the following configuration, building Android 10 takes ~ 25 minutes.
Download Android Open Source Project
Below are the steps describing the download and build process of AOSP (steps are described for Linux machine)
Initialize Repo Client
Create a directory where source code will be downloaded
$ mkdir aosp
$ cd aosp
Configure Git (you can skip this step if this was ever done before)
$ git config --global user.name "Your Name"
$ git config --global user.email "you@example.com"
Get the latest version of Repo. You must specify a URL for the manifest, which specifies where the various repositories included in the Android source are placed within your working directory.
$ repo init -u https://android.googlesource.com/platform/manifest -b android-10.0.0_r41
It’s better not to use master
branch, you can specify a different branch after -b
. A list of available branches can be found here.
Download Source Code Tree
To download the source tree call the command below
$ repo sync -jx -c
where x
is the number of threads, In my case command will look lile
repo sync -j32 -c
Building the Android Open Source Project
After successfully syncing the source code you can start building Android Open Source Project
Setup The Environment
Type the command below in your terminal
$ source build/envsetup.sh
this will setup build environment for you.
Choosing a Target
You can build the Android Open Source Project for different targets: Phone, Auto, TV, etc. To select a target type
$ lunch
This command will show you a list of available build targets,
select a target by typing the number and pressing Enter or by typing the name of the target and pressing the Enter.
Let’s say you want to build for Android Auto then type for example 8
and press Enter (for phone emulator select one of 25
or 26
). After the target is selected you can start building my typing in the terminal
$ m
this will start a build which depending on the power of your machine will take from 20 minutes to 9 hours. On my machine, the initial build takes
After a successful build, it’s time to run on an emulator, to do that just type
$ emulator
Building a Simple Application
Now we want to add an application and build it inside of AOSP. Here we have two options we can place our application in the
aosp/packages/apps
aosp/vendor/awsome/packages/app
the second directory does not exist and you should create it, instead of awsome
you can specify any name the rest should be unchanged.
$ mkdir vendor/awsome/packages/app
I will go with the second approach. Create a folder with your application name, let’s say in our case it’s HelloAosp
with the following structure
- Android.mk
- AndroidManifest.xml
+ res
+ values
- strings.xml
+ src
+ com
+ example
+ helloaosp
- HelloAosp.java
Now let’s see what should be content of each file
Android.mk
This file will be used to build your source code. You can read more about Android.mk
here.
AndroidManifest.xml
Nothing special in the manifest file a standard AndroidManifest.xml.
strings.xml
HelloAosp.java
Again nothing special, an activity which will print “Hello AOSP” as soon as the application will be launched.
Important: Now we need to tell our build system that we want to include this application into AOSP, so we can see it after we run the system image in the emulator or real device. To do so open a file
/AOSP/packages/services/Car/car_product/build/car.mk
there you should see something like
PRODUCT_PACKAGES += \
Bluetooth \
CarDeveloperOptions \
OneTimeInitializer \
Provision \
SystemUpdater \
HelloAosp
add HelloAosp
at the end of the line.
Now it’s the right time to build the android open source project with your application and deploy it on the emulator, for that just do
$ m
and after the successful build
$ emulator -no-snapshot &
wait a bit and after the emulator launched you can see that your HelloAosp application is there and if you click on the icon it will show on the screen “Hello AOSP!”
Congratulations!
Building an application with Native Code
Let’s now add a bit of spice to our application. Let’s say we want to do some native calls from our HelloAosp
application. Our C developers kindly provided us a hello-aosp.c
file that has a method that is returning.
Hello AOSP from JNI
So we need to modify the current application structure as following
- Android.mk
- AndroidManifest.xml
+ res
+ values
- strings.xml
+ src
+ com
+ example
+ helloaosp
- HelloAosp.java
+ jni
- Android.mk
- Application.mk
- hello-aosp.c
As you can see we added a new folder called jni
where we will add C code and needed .mk
files for building that code.
jni/Andrid.mk
Here we specified the C file name and the name of the library libhello-aosp
which we will get as a result of the build.
jni/Application.mk
you can read more about Application.mk
here.
jni/hello-aosp.c
Also, we need to modify Android.mk
of the main project and HelloAosp.java
files.
Android.mk
The difference between old and new Android.mk
files are only one line
LOCAL_JNI_SHARED_LIBRARIES := libhello-aosp
with this line, we are telling our application that we have a jni
library which we would like to use.
HelloAosp.java
In the java file, we need to load the jni
library, please note that here you need to specify the name of the library without lib
prefix, so instead of libhello-aosp
we just write hello-aosp
. Also, we need to define the native methods with keyword native, this means the methods are implemented in the library, and when you call stringFromJNI
a method which is in the hello-aosp.c
file will be called.
Now last two steps
$ m
and after the successful build
$ emulator -no-snapshot &
Run your application on the emulator and you will see the message Hello AOSP from JNI
on your screen.
Building an application with .so and .jar libraries.
Now we need to build our HelloAosp application but this time our developers provided us a .jar
library which is a jni
library which is depending on several .so
files.
Now project structure will look like
- Android.mk
- AndroidManifest.xml
+ res
+ values
- strings.xml
+ src
+ com
+ example
+ helloaosp
- HelloAosp.java
+ libs
- hello-aosp.jar
- libStringUtils.so
- libStringConverters.so
- libUtils.so
Android.mk
So several lines were added in the Android.mk
file:
LOCAL_STATIC_JAVA_LIBRARIES := \
lib-hello-aosp
this line tells the build system that we want to use hello-aosp.jar
library which is a static java library.
include $(CLEAR_VARS)LOCAL_MODULE_TAGS := optionalLOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := \
lib-hello-aosp:libs/hello-aosp.jarinclude $(BUILD_MULTI_PREBUILT)include $(call all-makefiles-under,$(LOCAL_PATH))
this block of code tells our build system that we have a static java library that is already built and should be used. you can give any name to your library
lib-any-name-you-wish:libs/hello-aosp.jar
but also you need to provide the same name for
LOCAL_STATIC_JAVA_LIBRARIES := \
lib-any-name-you-wish
and with the last change, we are telling the build system that we have prebuild jni libraries
JNI_LIBS :=
$(foreach FILE,$(shell find $(LOCAL_PATH)/libs/ -name *.so), $(eval JNI_LIBS += $(FILE)))
LOCAL_PREBUILT_JNI_LIBS := $(subst $(LOCAL_PATH),,$(JNI_LIBS))
this is a shortened way of writing, if you have more then 3 libraries I think it is better to use a shortened way, instead of writing each library one by one, in this case, you don’t care if you add or remove one .so file
LOCAL_PREBUILT_JNI_LIBS := \
libStringUtils.so \
libStringConverters.so \
libUtils.so
That’s it, now you can build the application and run it on the emulator.
Building a System application
To run our application with system privileges you need to modify few lines in the AndroidManifest.xml
and Android.mk
files.
- Android.mk
- AndroidManifest.xml
+ res
+ values
- strings.xml
+ src
+ com
+ example
+ helloaosp
- HelloAosp.java
AndroidManifest.xml
add following two lines to AndroidManifest.xml
coreApp="true"
android:sharedUserId="android.uid.system"
Android.mk
LOCAL_CERTIFICATE := platformLOCAL_PRIVILEGED_MODULE := true
and lines above to the Android.mk
file