Android Support
Android Runtime
Fory Java supports Android 8.0+ (API level 26+) through the regular fory-core artifact. No separate
Android artifact is required for core object serialization.
Use core object serialization on Android:
Fory#serialize(Object)andFory#deserialize(byte[]).BaseFory#deserialize(ByteBuffer)for heap, direct, and read-onlyByteBufferinputs.- Stream, channel, and out-of-band buffer APIs through byte-array, heap-buffer, or
ByteBuffercopy paths. - Java collections/maps and xlang collections/maps.
java/fory-format row-format APIs are JVM-only and are not supported on Android.
Runtime Codegen
Runtime serializer code generation is disabled on Android. If withCodegen(true) is set, Fory keeps
Android serialization on the non-codegen path and logs a warning.
Android apps that need generated serializers should use build-time static generated serializers instead.
Static Generated Serializers
Use @ForyStruct static generated serializers for Android application classes. They are generated by
javac during the app build and work without runtime bytecode generation.
Install The Annotation Processor
Add fory-annotation-processor to the annotation processor path of the module that compiles your
Android model classes:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.apache.fory</groupId>
<artifactId>fory-annotation-processor</artifactId>
<version>${fory.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
Then annotate Android model classes with @ForyStruct.
Static generated serializers are required on Android when a serialized class uses Fory type-use annotations, for example:
import java.util.List;
import org.apache.fory.annotation.ForyStruct;
import org.apache.fory.annotation.UInt8Type;
@ForyStruct
public class ImageBlock {
public List<@UInt8Type Integer> pixels;
}
Without the generated static descriptors, Android reflection may not expose the nested type-use
metadata needed for annotations such as @Ref, @Int8Type, @UInt8Type, @Float16Type, or
@BFloat16Type. Serialization for those classes will not have the schema information Fory needs.
See Static Generated Serializers for setup instructions.
Object Model Requirements
Android serializers use public Android runtime capabilities. For application classes, prefer:
- accessible no-argument constructors, or records with supported constructors.
- public, protected, or package-private serialized fields.
- non-private getters and setters for private serialized fields.
@ForyStructstatic generated serializers for Android model classes.
Final fields in ordinary classes are not suitable for generated read/copy methods. Use records for constructor-based immutable values.
Unsupported Features
The following JVM features are not supported on Android:
- Runtime serializer code generation and async compilation.
- Lambda and
SerializedLambdaserialization. - Native-address serialization APIs and native-address
MemoryBufferwrapping. - Raw unsafe memory copy APIs.
java/fory-formatrow-format APIs.
ByteBuffer
BaseFory#deserialize(ByteBuffer) supports heap, direct, and read-only buffers on Android by copying
the remaining bytes into a Fory-owned heap buffer. The caller buffer position and limit are not
changed.
Raw direct-buffer address wrapping is a JVM-only fast path and is not used on Android.
Collections, Maps, And Proxies
Common JDK collection and map implementations are supported on Android. In xlang mode, collection and map serialization uses the xlang protocol and does not encode Java wrapper/view internals.
java.lang.reflect.Proxy serialization is supported for normal proxy usage. Do not invoke, log, or
use a proxy as a map/set key while it is still being deserialized; the invocation handler may not be
ready yet.