Why does ClassLoader.loadClass ALWAYS throw ClassNotFoundException in android?

StackOverflow https://stackoverflow.com/questions/22271963

  •  11-06-2023
  •  | 
  •  

Pergunta

Take a look at:

    Context contxt  = createPackageContext("com.exam.provider", CONTEXT_IGNORE_SECURITY); // I know, the package must be (and IS) already installed ;-)

    ClassLoader loader = contxt.getClassLoader();
    loader.loadClass("com.exam.provider.MyGestureOverlayView"); // I'm sure this class DOES exist in "com.exam.provider"

But this code always throws java.lang.ClassNotFoundException. Also note that both android applications share the same user id via:

    <manifest
        ...
        android:sharedUserId="whatever"
        ...
    >

I am completely frustrated. Any idea?

Foi útil?

Solução

Your code works fine, for me, if I just add the CONTEXT_INCLUDE_CODE flag in the call to createPackageContext.

Outras dicas

Is the error at compile-time or run-time

'ClassNotFoundException' here may be because the package is not found at runtime. {after successful compilation}

Uninstalling the app from the device or emulator first solved this problem for me.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top