Building Custom SDK with all Hidden APIs to work with Android System Applications with Android Studio.
This can be done either by customizing the sdk itself or importing the library into Android Studio. But the convenient way is that to build a custom sdk provided as the first step.
-
Download the aosp repo
-
$source build/envsetup.sh
-
$lunch [your-build-variant]
-
[your-build-variant] could be sdk_phone_x86_64, sdk_phone_x86, sdk-eng if you're building for phone/generic build and it could be aosp_car_x86_64, aosp_car_x86 if you're building for generic car. Always prefer to use x86_64 bit variant.
-
$make sdk
-
After $make sdk, the sdk wil be generated at /root_aosp/out/host/linux-x86/sdk/ on Linux and /root_aosp/out/host/darwin-x86/sdk/ on Mac OS
-
If you want build SDK for Windows OS, then run $make win_sdk instead of $make sdk. The generated sdk will be at /root_aosp/out/host/windows-x86/sdk/
-
Go to /aosp_root/out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/ and copy classes-header.jar file
-
Copy your SDK's android.jar file under /Sdk/platforms/android-[xx]/ and copy android.jar file
-
Create a new folder and extract android.jar contents.
-
Extract classes-header.jar on the same new folder where we extracted android.jar file. Replace all the files if the file name already exists.
-
Make a new android.jar file by "/new_folder$jar cfM ../android.jar ." inside the new folder.
-
The above command will generate a new android.jar file outside the new folder.
-
Copy the generated android.jar file and replace it in your SDK directory /Sdk/platforms/android-[xx]/
-
Copy "classes.jar" from /aosp_root/out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/
-
Paste the library under /root_project_dir/app/system_libs/ as "framework.jar"
-
Add it as a dependency in your app module [build.gradle] file - implementation files('system_libs/framework.jar')
-
Configure your project's [build.gradle] file with the below lines of code for java compilation.
- Configure your module's [build.gradle] file with the below code that brings the framework.jar file as a top library and moves SDK to the last. So that it points to framework.jar APIs instead of SDK.
- Prebuilt SDK in AOSP can be found at /aosp_root/prebuilts/sdk/current/system/android.jar. But access to Hidden APIs and Framework AIDL services were limited.
-
Download the aosp repo
-
$source build/envsetup.sh
-
$lunch [your-build-variant]
-
[your-build-variant] could be sdk_phone_x86_64, sdk_phone_x86 if you're building for phone/generic build and it could be aosp_car_x86_64, aosp_car_x86 if you're building for generic car. Always prefer to use x86_64 bit variant.
-
$make -j16
-
$emulator
-
Your emulator will be launched automatically from /aosp_root/prebuilts/android-emulator/linux-x86_64/ if you are building on Linux and /aosp_root/prebuilts/android-emulator/darwin-x86_64/ if you are building on Mac OS based on the system images generated on /aosp_root/out/target/product/[your-build-variant]
-
Download the aosp repo
-
$source build/envsetup.sh
-
$lunch [your-build-variant]
-
[your-build-variant] could be sdk_phone_x86_64, sdk_phone_x86 if you're building for phone/generic build and it could be aosp_car_x86_64, aosp_car_x86 if you're building for generic car. Always prefer to use x86_64 bit variant.
-
$make -j16 sdk sdk_repo
-
This will generate two files under /aosp-root/out/host/linux-x86/sdk/[your-build-variant]/ or in /aosp-root/out/host/darwin-x86/sdk/[your-build-variant]/ if you are building on Mac OS
sdk-repo-linux-system-images-eng.[username].zip
repo-sys-img.xml
-
sdk-repo-linux-system-images-eng.[username].zip is the actual emulator system image that you normally see in /Sdk/system-images/ in an unzipped format.
-
If you want to share this emulator among many developers, then host the both files repo-sys-img.xml and sdk-repo-linux-system-images-eng.[username].zip in an online environment.
-
Make sure that you change the URL in repo-sys-img.xml of sdk-repo-linux-system-images-eng.[username].zip hosted file.
-
After hosting the file, Go to Android Studio > Tools > SDK Manager > SDK Update Sites > Tap Add Button and provide name and URL of repo-sys-img.xml hosted file and then, tap ok.
-
This adds your custom emulator system image to the System Images page of the SDK (/Sdk/system-images/).
-
Then go to Android Studio > Tools > AVD Manager > and create a new virtual device with your custom emulator system image.
-
If you want to use the custom emulator locally, then just unzip the file sdk-repo-linux-system-images-eng.[username].zip in /Sdk/system-images/ directory and should be in the below folder structure.
-
The folder structure for the system images in /Sdk/system-images/ should be,
android-[xx]/[TagId]/x86_64/ - this is for x86_64 version
android-[xx]/[tagId]/x86/ - this is for x86 version
[xx] is the android API level (ex. 29)
[TagId] is the emulator variant. It could be android-automotive if you are building for Car and could be default if you are building for others.
-
And then, edit source.properties and package.xml:
Abi - should be x86_64 or x86
TagId - is the emulator variant. It could be android-automotive if you are building for Car and could be default if you are building for others.
TagDisplay or display - Give the name as you like. ex) "Android Automotive System Image" for Car Variant.
display-name - could be "Intel x86 Atom_64 System Image" for "x86_64" or "Intel x86 Atom System Image" for "x86"
-
If you can't find package.xml and was not auto-generating then just copy it from other emulator system images from your SDK.
-
Then go to Android Studio > Tools > AVD Manager > and create a new virtual device with your custom emulator system image.
-
The Product Variants sdk_phone_x86_64, aosp_x86_64 is in /aosp_root/build/target/product/ directory.
-
The Product Variant aosp_car_x86_64 is in /aosp_root/device/generic/car/ directory.
-
Building sdk_phone_x86_64 for phone variant will not produce any error and will produce SDK and Emulator System Image without any problem.
-
But for building aosp_car_x86_64 for Car will produce some development tools missing errors.
-
The cause of the error for the Car variant aosp_car_x86_64 is that it does not include host tools and libs unlike sdk_phone_x86_64 has host and tools defined.
Copy the host tools and libs from sdk_phone_x86_64 to aosp_car_x86_64
# Define the host tools and libs that are parts of the SDK
-include sdk/build/product_sdk.mk
-include development/build/product_sdk.mk -
Step 5 was not even tried before and it is still a suggestion only.
-
The errors that I experienced are:
deployagent.jar was not found in /aosp_root/out/target/product/[your-build-variant]/system/framework/ directory.
deployagent was not found in /aosp_root/out/target/product/[your-build-variant]/system/bin/ directory.
These errors are produced by /aosp_root/development/build/sdk.atree.
-
To solve this problem, first build for aosp_car_x86_64-userdebug, then build aosp_car_x86 variant and then build aosp_car_x86_64.
-
After all, copy deployagent.jar from /aosp_root/out/target/product/aosp_car_x86/system/framework/ and deployagent from /aosp_root/out/target/product/aosp_car_x86/system/bin/ to /aosp_root/out/target/product/aosp_car_x86_64/system/framework/ and /aosp_root/out/target/product/aosp_car_x86_64/system/bin/
-
Then try $make sdk sdk_repo again and sdk and emulator system images will be produced.
-
The whole idea is to solve the error is, copy all the missing packaging development tools from other product variants from aosp_root/out/target/product/[other-build-variant]/ to your aosp_root/out/target/product/[your-build-variant]/
-
After building custom emulator from above steps, create a new Android Virtual Device (AVD) from AVD Manager, Android Studio.
-
Try remounting the emulator by:
$ adb root $ adb disable-verity $ adb reboot $ adb root $ adb remount
-
If the remount was unsuccessful, then try launching emulator as a writable system.
i) List the available AVDs:
Sdk/emulator$ ./emulator -list-avds [avd-name-1] [avd-name-2]
ii) Launch the writable emulator system:
Sdk/emulator$ ./emulator -writable-system -avd [any-avd-name]
iii) Try remounting the emulator:
$ adb root $ adb remount
-
With the remounted (or) writable emulator system, you can directly push your system applications into system.
$ adb push [app-name].apk /system/product/priv-app/[app-name]/ $ adb reboot
-
Signing the keystore (.jks) with the android platform certificates requires the certificate (platform.pem) and the key (platform.x509.pk8) that can be found in the below path of AOSP
aosp_root/build/target/product/security/
-
Generate a new or use the existing keystore file from Android Studio.
-
Download the shell script
keytool-importkeypair
from https://github.com/getfatday/keytool-importkeypair -
Run the script. It will import the key/certificate pair with the keystore file. Then you can directly use keystore file to sign the apk with platform certificate.
$ keytool-importkeypair -k platform.keystore -p platform -pk8 platform.pk8 -cert platform.x509.pem -alias platform
-
Permissions for all the system privileged apps can be found in:
aosp_root/frameworks/base/etc/permissions
-
Push your permission file [package_name].xml to the below directory:
$ adb push [package_name].xml /system/product/etc/permissions/
-
On Android 10 and above, the device won't boot without these permissions.
-
Building Custom SDK by kwagjj > https://kwagjj.wordpress.com/2017/10/27/building-custom-android-sdk-from-aosp-and-adding-it-to-android-studio/
-
Standalone SDK > https://grapheneos.org/build#standalone-sdk
-
Importing Framework Library into Android Studio by kwagjj > https://kwagjj.wordpress.com/2017/08/10/using-framework-jar-in-android-studio/
-
Building JAR File > https://www.codejava.net/java-core/tools/using-jar-command-examples
-
Restrictions on Non-SDK interfaces > https://developer.android.com/distribute/best-practices/develop/restrictions-non-sdk-interfaces
-
Embedded Android by Karim Yaghmour > https://www.oreilly.com/library/view/embedded-android/9781449327958/ch04.html
-
Building and Sharing AVD > https://source.android.com/setup/create/avd
-
Building Android Car Emulator > https://www.embien.com/blog/building-android-car-emulator/
-
Creating Acloud Instance > https://source.android.com/setup/start
-
Cuttlefish Virtual Android Devices > https://source.android.com/setup/create/cuttlefish
-
Run apps on the Android Emulator > https://developer.android.com/studio/run/emulator
-
Signing build for release > https://source.android.com/devices/tech/ota/sign_builds
-
Signing your app > https://developer.android.com/studio/publish/app-signing
-
Allowing Priv-app Permissions > https://source.android.com/devices/tech/config/perms-whitelist
-
Carbon - Creates images for your source code > https://carbon.now.sh/
Copyright 2021 LiteKite Startup
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.