public class AndroidClassLoadingStrategy extends Object implements net.bytebuddy.dynamic.ClassLoadingStrategy
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 Context.getDir(String, int) with specifying
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.
|
| Constructor and Description |
|---|
AndroidClassLoadingStrategy(File privateDirectory)
Creates a new Android class loading strategy that uses the given folder for storing classes.
|
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 |
|---|---|
Map<net.bytebuddy.instrumentation.type.TypeDescription,Class<?>> |
load(ClassLoader classLoader,
Map<net.bytebuddy.instrumentation.type.TypeDescription,byte[]> types) |
String |
toString() |
public AndroidClassLoadingStrategy(File privateDirectory)
AndroidClassLoadingStrategy.DexProcessor.ForSdkCompiler which uses the
Android SDK's dex compiler in version 1.7 which is automatically included with this strategy. Doing so,
the dex compiler is applied with default options.privateDirectory - A directory that is not shared with other applications to be used for storing
generated classes and their processed forms.public 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<net.bytebuddy.instrumentation.type.TypeDescription,Class<?>> load(ClassLoader classLoader, Map<net.bytebuddy.instrumentation.type.TypeDescription,byte[]> types)
load in interface net.bytebuddy.dynamic.ClassLoadingStrategyCopyright © 2014–2015. All rights reserved.