public abstract class AndroidClassLoadingStrategy extends Object implements ClassLoadingStrategy<ClassLoader>
A class loading strategy that allows to load a dynamically created class at the runtime of an Android
application. For this, a DexClassLoader is used under the covers.
This class loader requires to write files to the file system which are then processed by the Android VM. It is
not permitted by Android's security checks to store these files in a shared folder where they could be
manipulated by a third application what would break Android's sandbox model. An example for a forbidden storage
would therefore be the external storage. Instead, the class loading application must either supply a designated
directory, such as by creating a directory using android.content.Context#getDir(String, int) with specifying
android.content.Context#MODE_PRIVATE visibility for the created folder or by using the
getCodeCacheDir directory which is exposed for Android API versions 21 or higher.
By default, this Android ClassLoadingStrategy uses the Android SDK's dex compiler in
version 1.7 which requires the Java class files in version ClassFileVersion.JAVA_V6 as
its input. This version is slightly outdated but newer versions are not available in Maven Central which is why this
outdated version is included with this class loading strategy. Newer version can however be easily adapted by
implementing the methods of a AndroidClassLoadingStrategy.DexProcessor to
appropriately delegate to the newer dex compiler. In case that the dex compiler's API was not altered, it would
even be sufficient to include the newer dex compiler to the Android application's build path while also excluding
the version that ships with this class loading strategy. While most parts of the Android SDK's components are
licensed under the Apache 2.0 license, please also note
their terms and conditions.
| Modifier and Type | Class and Description |
|---|---|
static interface |
AndroidClassLoadingStrategy.DexProcessor
A dex processor is responsible for converting a collection of Java class files into a Android dex file.
|
protected static interface |
AndroidClassLoadingStrategy.FileProcessor
A processor for files that are added to a dex file.
|
static class |
AndroidClassLoadingStrategy.Injecting
An Android class loading strategy that injects types into the target class loader.
|
static class |
AndroidClassLoadingStrategy.Wrapping
An Android class loading strategy that creates a wrapper class loader that loads any type.
|
ClassLoadingStrategy.Configurable<S extends ClassLoader>, ClassLoadingStrategy.Default, ClassLoadingStrategy.ForBootstrapInjection, ClassLoadingStrategy.ForJnaInjection, ClassLoadingStrategy.ForUnsafeInjection, ClassLoadingStrategy.UsingLookup| Modifier and Type | Field and Description |
|---|---|
protected File |
privateDirectory
A directory that is not shared with other applications to be used for storing generated classes and
their processed forms.
|
protected RandomString |
randomString
A generator for random string values.
|
BOOTSTRAP_LOADER, NO_PROTECTION_DOMAIN| Modifier | Constructor and Description |
|---|---|
protected |
AndroidClassLoadingStrategy(File privateDirectory,
AndroidClassLoadingStrategy.DexProcessor dexProcessor)
Creates a new Android class loading strategy that uses the given folder for storing classes.
|
| Modifier and Type | Method and Description |
|---|---|
protected abstract Map<TypeDescription,Class<?>> |
doLoad(ClassLoader classLoader,
Set<TypeDescription> typeDescriptions,
File jar)
Applies the actual class loading.
|
Map<TypeDescription,Class<?>> |
load(ClassLoader classLoader,
Map<TypeDescription,byte[]> types) |
protected final File privateDirectory
protected final RandomString randomString
protected AndroidClassLoadingStrategy(File privateDirectory, AndroidClassLoadingStrategy.DexProcessor dexProcessor)
privateDirectory - A directory that is not shared with other applications to be used for storing
generated classes and their processed forms.dexProcessor - The dex processor to be used for creating a dex file out of Java files.public Map<TypeDescription,Class<?>> load(@MaybeNull ClassLoader classLoader, Map<TypeDescription,byte[]> types)
load in interface ClassLoadingStrategy<ClassLoader>protected abstract Map<TypeDescription,Class<?>> doLoad(@MaybeNull ClassLoader classLoader, Set<TypeDescription> typeDescriptions, File jar) throws IOException
classLoader - The target class loader.typeDescriptions - Descriptions of the loaded types.jar - A jar file containing the supplied types as dex files.IOException - If an I/O exception occurs.Copyright © 2014–2024. All rights reserved.