Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add system api classes to your android project for non AOSP builds

I have an android system app that is currently being built with the android build system. The SDK in use is system_current as it uses some system apis.

For external dependency reasons, ease of development, debugging etc, it would be nice to move this app to Android Studio and use Gradle to build.

Converting the sources to build with Gradle is straight forward enough. However, at build time, the system APIs are not found as those are not available in the normal SDK. I thought that by generating the SDK from the AOSP sources I'd get an SDK I could use, but that target output seems to also not have the System APIs available.

How would I change my gradle build to be able to use the System SDK to compile against?

like image 980
Dennis Avatar asked Oct 25 '25 05:10

Dennis


2 Answers

There are two ways depending if you are only trying to use current non-public APIs, or of you've added custom ones yourselves and are trying to access those.

  • If you're only trying to use current system-level APIs, you can use android.jar from this repository https://github.com/anggrayudi/android-hidden-api

  • If you have added some new method that isn't part of standard AOSP, then it's a but more work.
    The reason that all methods don't show as part of Android SDK is that in AOSP code they are annotated with @hide. This means that when SDK is generated, they are exlcuded.
    You'll need to remove that annotation for the methods that you want to use. Then you'l need

    • Run make update-api to update the public API of the project.
    • Make the Android SDK from code by following: https://android.googlesource.com/platform/sdk/+/master/docs/howto_build_SDK.txt
    • Change the Android SDK path in Android Studio to use the custom one you build.

There's also the possibility of accessing methods via reflection on runtime without SDK generation. It's slower and messier to code though.

like image 52
Rick Sanchez Avatar answered Oct 26 '25 22:10

Rick Sanchez


Not sure I understand exactly what you mean by normal SDK etc.

From https://developer.android.com/studio/build

The key part is to have a

apply plugin: 'com.android.library'

...

android {
   ...
}

If on the other hand you wanted to build with a desktop JDK but compile against java APIs you could add a dependency on robolectric and you can get a jar you can import into another Android project.

implementation "org.robolectric:android-all:11-robolectric-6757853"
like image 44
Yuri Schimke Avatar answered Oct 27 '25 00:10

Yuri Schimke



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!