| --- |
| title: Android Support |
| sidebar_position: 15 |
| id: android_support |
| license: | |
| Licensed to the Apache Software Foundation (ASF) under one or more |
| contributor license agreements. See the NOTICE file distributed with |
| this work for additional information regarding copyright ownership. |
| The ASF licenses this file to You under the Apache License, Version 2.0 |
| (the "License"); you may not use this file except in compliance with |
| the License. You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
| --- |
| |
| ## 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)` and `Fory#deserialize(byte[])`. |
| - `BaseFory#deserialize(ByteBuffer)` for heap, direct, and read-only `ByteBuffer` inputs. |
| - Stream, channel, and out-of-band buffer APIs through byte-array, heap-buffer, or `ByteBuffer` copy |
| 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: |
| |
| ```xml |
| <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: |
| |
| ```java |
| 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](static-generated-serializers.md) 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. |
| - `@ForyStruct` static 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 `SerializedLambda` serialization. |
| - Native-address serialization APIs and native-address `MemoryBuffer` wrapping. |
| - Raw unsafe memory copy APIs. |
| - `java/fory-format` row-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. |