1

In which SDK, jar this class can be found? I need to check at runtime if an exception is an instance of it.

I know this class can be found in AOSP sources. But this doesn't help much in runtime.

Eugen Martynov
  • 19,888
  • 10
  • 61
  • 114

3 Answers3

2

That is a hidden class in the Android SDK (see the Android 8.1 edition).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Mark, how to compile against hidden class in Android SDK? – Eugen Martynov Apr 09 '18 at 15:58
  • If I workaround to build it like https://stackoverflow.com/questions/28287570/exposing-hidden-apis-android-l-sdk-21-in-android-studio. Is it guarnteed that app will not crash in production? – Eugen Martynov Apr 09 '18 at 16:01
  • 1
    @EugenMartynov: "how to compile against hidden class in Android SDK?" -- you can't. "Is it guarnteed that app will not crash in production?" -- I would not do that. Call `Class.forName()` and look it up at runtime, bearing in mind that this might break on Android P. Or, call `getClass().getName()` on the exception and compare it to `android.security.KeyStoreException` (though this wouldn't handle subclasses). – CommonsWare Apr 09 '18 at 16:04
  • Thank you! I will actually compare class names since it is the only one available strategy for now. – Eugen Martynov Apr 09 '18 at 16:19
  • 1
    Or just ignore cause and relay on the `ProviderException` in this case – Eugen Martynov Apr 09 '18 at 16:36
1

You can download the android sdk source, see this link:

Android SDK source code

With regards to checking the exception type at runtime, you can do the following (in Kotlin - transcribe to Java if required):

fun doSomething() {
    try {
        // Do something that may cause an exception
    } catch (ex: KeyStoreException) {

    }
}

or:

fun checkType() {
    try {
        // Something that could throw an exception
    } catch(ex: Exception) {
        when (ex) {
            is KeyStoreException -> {
                // Handle
            }
        }
    }
}
Thomas Cook
  • 4,371
  • 2
  • 25
  • 42
  • 1
    I can not compile this code. Did you try? The thingy this class is not visible in Android SDK. – Eugen Martynov Apr 09 '18 at 15:57
  • Compiles fine for me. It is Kotlin however, like I said in my answer you may need to transcribe to Java if you're writing in Java. – Thomas Cook Apr 09 '18 at 16:00
  • Is it `android.security.KeyStoreException` or `java.security.KeyStoreException`? – Eugen Martynov Apr 09 '18 at 16:02
  • java.security.KeyStoreException, for which android.security.KeyStoreException is simply an alias (as far as I know). From the official docs (note the package name): https://developer.android.com/reference/java/security/KeyStoreException.html – Thomas Cook Apr 09 '18 at 16:03
  • No, it is not the alias. Please don't get wrong with it :) – Eugen Martynov Apr 09 '18 at 16:04
  • Tom, sorry that my question looks vague. The problem that this class is not visible in Android SDK and it is completely different class than you pointed out. So the question was not how to write code against of class that I can not find in my compile path – Eugen Martynov Apr 09 '18 at 16:17
1

This is a hidden class in Android . This can only be accessed by the framework or below layers and system apps , cannot be used by third party applications . You can check out the defination in the below link .

http://androidxref.com/8.0.0_r4/xref/frameworks/base/keystore/java/android/security/KeyStoreException.java.

There are 3 such classes in Android . All the 3 are under different packages and used in different context .

http://androidxref.com/8.0.0_r4/search?q=&defs=&refs=&path=%22KeyStoreException.java%22&hist=&project=art&project=bionic&project=bootable&project=build&project=cts&project=dalvik&project=developers&project=development&project=device&project=docs&project=external&project=frameworks&project=hardware&project=kernel&project=libcore&project=libnativehelper&project=packages&project=pdk&project=platform_testing&project=prebuilts&project=sdk&project=system&project=test&project=toolchain&project=tools

khetanrajesh
  • 300
  • 3
  • 13