Vishal Pawar
Vishal Pawar

Reputation: 4340

refer aosp functions in Android Core App using gradle

How to refer aosp hidden methods in core android app using gradle build system. I am referring framework and other jars from out folder but unable to access hidden API. Is there any way to access hidden methods.

Upvotes: 0

Views: 182

Answers (1)

TheWanderer
TheWanderer

Reputation: 17854

There are a couple ways.

  1. Android Hidden API

    As long as you're fine targeting API 27 (there's no API 28 release as of writing this) this way works great. You can directly call the hidden methods with proper code completion and everything.

    Note: As of writing this, you'll need to set your Gradle classpath to 3.1.4. 3.2.0 adds some sort of integrity check that breaks builds when using a modified framework JAR.

  2. Use reflection

    It's not ideal, but it'll work if you want to target API 28, or you want to use Gradle 3.2.0.

    Example (Kotlin):

    val IWindowManager = Class.forName("android.view.IWindowManager")
    val IWindowManagerStub = Class.forName("android.view.IWindowManager\$Stub")
    val ServiceManager = Class.forName("android.os.ServiceManager")
    
    val binder = ServiceManager.getMethod("checkService", String::class.java).invoke(null, Context.WINDOW_SERVICE)
    val iWindowManagerInstance = IWindowManagerStub.getMethod("asInterface", Binder::class.java).invoke(null, binder)
    
    val hasNavBar = IWindowManager.getMethod("hasNavigationBar").invoke(iWindowManagerInstance) as Boolean
    

Upvotes: 2

Related Questions