Import Android Framework Jar

Android

Import AOSP framework jar file

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.

Build out the framework jar.

When you run the following commands,

1
2
3
source ./build/envsetup.sh 
lunch aosp_arm64-eng
make -j24

you can build the AOSP images out and get the class_header.jar file from this path:

path/to/your/AOSP/out/target/common/obj/JAVA_LIBRARIES/framework_intermediates

Here you can find the classes-header.jar file, which is the file we need for the Android Studio.

Setup Android Studio

Open your Android Studio and your system project. Here we need to do some extra settings and import the jar file.

Create an SDK folder

You can create an SDK folder in your application root path and put your framework.jar file in this SDK folder.

Update project build.gradle.

We need to update the project level Gradle file and add the following code,

before Android 4.2

1
2
3
4
5
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs.add('-Xbootclasspath/p:app/sdk/framework.jar')
}
}

after Android 4.2

1
2
3
4
5
6
7
8
9
gradle.projectsEvaluated {  
tasks.withType(JavaCompile) {
Set<File> fileSet = options.bootstrapClasspath.getFiles()
List<File> newFileList = new ArrayList<>();
newFileList.add(new File("./app/sdk/framework.jar"))
newFileList.addAll(fileSet)
options.bootstrapClasspath = files(newFileList.toArray())
}
}

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.

And the framework.jar to the Application module

After the changes described above, you will need to add this jar file to the Application module.

1
compileOnly files('sdk/framework.jar')

We use 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.

Import Android Framework Jar

Android

Import AOSP framework jar file

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.

Build out the framework jar.

When you run the following commands,

1
2
3
source ./build/envsetup.sh 
lunch aosp_arm64-eng
make -j24

you can build the AOSP images out and get the class_header.jar file from this path:

path/to/your/AOSP/out/target/common/obj/JAVA_LIBRARIES/framework_intermediates

Here you can find the classes-header.jar file, which is the file we need for the Android Studio.

Setup Android Studio

Open your Android Studio and your system project. Here we need to do some extra settings and import the jar file.

Create an SDK folder

You can create an SDK folder in your application root path and put your framework.jar file in this SDK folder.

Update project build.gradle.

We need to update the project level Gradle file and add the following code,

before Android 4.2

1
2
3
4
5
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs.add('-Xbootclasspath/p:app/sdk/framework.jar')
}
}

after Android 4.2

1
2
3
4
5
6
7
8
9
gradle.projectsEvaluated {  
tasks.withType(JavaCompile) {
Set<File> fileSet = options.bootstrapClasspath.getFiles()
List<File> newFileList = new ArrayList<>();
newFileList.add(new File("./app/sdk/framework.jar"))
newFileList.addAll(fileSet)
options.bootstrapClasspath = files(newFileList.toArray())
}
}

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.

And the framework.jar to the Application module

After the changes described above, you will need to add this jar file to the Application module.

1
compileOnly files('sdk/framework.jar')

We use 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.