Why do we import the AOSP framework jar file to the Android Studio? It’s usually unnecessary to import this jar file to develop a standard application for your project. But when you need to build an application that will need to interact with your framework code or do system integration with your hardware, those are two good use-cases to put the framework jar into the Android Studio.
Here we will show you how to import this framework jar file and use it in your project.
When you run the following commands,
you can build the AOSP images out and get the class_header.jar file from this path:
Here you can find the classes-header.jar file, which is the file we need for the Android Studio.
Open your Android Studio and your system project. Here we need to do some extra settings and import the jar file.
You can create an SDK folder in your application root path and put your framework.jar file in this SDK folder.
We need to update the project level Gradle file and add the following code,
before Android 4.2
after Android 4.2
The concept here is to put the framework.jar to the classpath where the bootstrap class loader can load and find the class. Android Studio will know it needs to put this framework.jar into the classpath to avoid the import error with this setting. After setting up the project build.gradle, you will need to change your application-level build.gradle.
After the changes described above, you will need to add this jar file to the Application module.
compileOnly because the standard Android SDK contains the corresponding APIs, but we can’t access them due to the
@SystemApi annotation. So letting the compiler know this framework jar and leveraging the APIs during the compiling process can bypass the
@SystemApi access problem.
After doing those changes, you should access the framework API in your Android Studio projects. Enjoy, happy coding.