BaseDexClassLoader

public class BaseDexClassLoader
extends ClassLoader

java.lang.Object
   ↳ java.lang.ClassLoader
     ↳ dalvik.system.BaseDexClassLoader


Base class for common functionality between various dex-based ClassLoader implementations.

Summary

Public constructors

BaseDexClassLoader(String dexPath, File optimizedDirectory, String librarySearchPath, ClassLoader parent)

Constructs an instance.

Public methods

String findLibrary(String name)

Returns the absolute path name of a native library.

String toString()

Returns a string representation of the object.

Protected methods

Class<?> findClass(String name)

Finds the class with the specified binary name.

URL findResource(String name)

Finds the resource with the given name.

Enumeration<URL> findResources(String name)

Returns an enumeration of URL objects representing all the resources with the given name.

Package getPackage(String name)

This method is deprecated. See ClassLoader#getPackage(String)

Inherited methods

Public constructors

BaseDexClassLoader

Added in API level 14
public BaseDexClassLoader (String dexPath, 
                File optimizedDirectory, 
                String librarySearchPath, 
                ClassLoader parent)

Constructs an instance. Note that all the *.jar and *.apk files from dexPath might be first extracted in-memory before the code is loaded. This can be avoided by passing raw dex files (*.dex) in the dexPath.

Parameters
dexPath String: the list of jar/apk files containing classes and resources, delimited by File.pathSeparator, which defaults to ":" on Android.

optimizedDirectory File: this parameter is deprecated and has no effect since API level 26.

librarySearchPath String: the list of directories containing native libraries, delimited by File.pathSeparator; may be null

parent ClassLoader: the parent class loader

Public methods

findLibrary

Added in API level 14
public String findLibrary (String name)

Returns the absolute path name of a native library. The VM invokes this method to locate the native libraries that belong to classes loaded with this class loader. If this method returns null, the VM searches the library along the path specified as the "java.library.path" property.

Parameters
name String: The library name

Returns
String The absolute path of the native library

toString

Added in API level 14
public String toString ()

Returns a string representation of the object.

Returns
String a string representation of the object.

Protected methods

findClass

Added in API level 14
protected Class<?> findClass (String name)

Finds the class with the specified binary name. This method should be overridden by class loader implementations that follow the delegation model for loading classes, and will be invoked by the loadClass method after checking the parent class loader for the requested class.

Parameters
name String: The binary name of the class

Returns
Class<?> The resulting Class object

Throws
ClassNotFoundException

findResource

Added in API level 14
protected URL findResource (String name)

Finds the resource with the given name. Class loader implementations should override this method.

Parameters
name String: The resource name

Returns
URL URL object for reading the resource; null if the resource could not be found, a URL could not be constructed to locate the resource, the resource is in a package that is not opened unconditionally, or access to the resource is denied by the security manager.

findResources

Added in API level 14
protected Enumeration<URL> findResources (String name)

Returns an enumeration of URL objects representing all the resources with the given name. Class loader implementations should override this method.

Parameters
name String: The resource name

Returns
Enumeration<URL> An enumeration of URL objects for the resource. If no resources could be found, the enumeration will be empty. Resources for which a URL cannot be constructed, are in a package that is not opened unconditionally, or access to the resource is denied by the security manager, are not returned in the enumeration.

getPackage

Added in API level 14
protected Package getPackage (String name)

This method is deprecated.
See ClassLoader#getPackage(String)

Returns package information for the given package. Unfortunately, instances of this class don't really have this information, and as a non-secure ClassLoader, it isn't even required to, according to the spec. Yet, we want to provide it, in order to make all those hopeful callers of myClass.getPackage().getName() happy. Thus we construct a Package object the first time it is being requested and fill most of the fields with fake values. The Package object is then put into the ClassLoader's package cache, so we see the same one next time. We don't create Package objects for null arguments or for the default package.

There is a limited chance that we end up with multiple Package objects representing the same package: It can happen when when a package is scattered across different JAR files which were loaded by different ClassLoader instances. This is rather unlikely, and given that this whole thing is more or less a workaround, probably not worth the effort to address.

Parameters
name String: the name of the class

Returns
Package the package information for the class, or null if there is no package information available for it