|
1 | 1 | ## Android Compatibility Guide |
2 | 2 |
|
3 | | -This page summarizes how to use FastProto on Android and current limitations. |
| 3 | +This page summarizes how to use FastProto on Android. |
4 | 4 |
|
5 | 5 | ## What Works |
6 | 6 |
|
7 | 7 | - Annotation-based mapping for primitive types, arrays, strings, enums, bit/byte order, and checksum/CRC |
8 | 8 | - Decode/encode via `FastProto.decode(...)` and `FastProto.encode(...)` |
9 | 9 | - Builder/Utils APIs (no annotations) |
| 10 | +- **Lambda formulas with annotation processor** (since 4.0.0) |
| 11 | + |
| 12 | +## Lambda Formulas on Android |
| 13 | + |
| 14 | +Since version 4.0.0, FastProto provides a separate annotation processor module (`fastproto-processor`) that generates formula classes at compile time. This makes `@DecodingFormula(lambda = "...")` and `@EncodingFormula(lambda = "...")` fully compatible with Android. |
| 15 | + |
| 16 | +### Setup |
| 17 | + |
| 18 | +**Gradle (Kotlin DSL)**: |
| 19 | +```kotlin |
| 20 | +dependencies { |
| 21 | + implementation("org.indunet:fastproto:4.0.0") |
| 22 | + annotationProcessor("org.indunet:fastproto-processor:4.0.0") |
| 23 | +} |
| 24 | + |
| 25 | +// For Kotlin projects, use kapt instead: |
| 26 | +// kapt("org.indunet:fastproto-processor:4.0.0") |
| 27 | +``` |
| 28 | + |
| 29 | +**Gradle (Groovy DSL)**: |
| 30 | +```groovy |
| 31 | +dependencies { |
| 32 | + implementation 'org.indunet:fastproto:4.0.0' |
| 33 | + annotationProcessor 'org.indunet:fastproto-processor:4.0.0' |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +**Maven**: |
| 38 | +```xml |
| 39 | +<dependencies> |
| 40 | + <dependency> |
| 41 | + <groupId>org.indunet</groupId> |
| 42 | + <artifactId>fastproto</artifactId> |
| 43 | + <version>4.0.0</version> |
| 44 | + </dependency> |
| 45 | + <dependency> |
| 46 | + <groupId>org.indunet</groupId> |
| 47 | + <artifactId>fastproto-processor</artifactId> |
| 48 | + <version>4.0.0</version> |
| 49 | + <scope>provided</scope> |
| 50 | + </dependency> |
| 51 | +</dependencies> |
| 52 | +``` |
| 53 | + |
| 54 | +### How It Works |
| 55 | + |
| 56 | +The annotation processor scans your code at compile time and generates `Function` implementation classes for each lambda expression. These generated classes are included in your APK and used at runtime instead of dynamic compilation. |
| 57 | + |
| 58 | +For example, this annotation: |
| 59 | +```java |
| 60 | +@Int16Type(offset = 0) |
| 61 | +@DecodingFormula(lambda = "x -> x * 0.1") |
| 62 | +private double temperature; |
| 63 | +``` |
| 64 | + |
| 65 | +Generates a class like: |
| 66 | +```java |
| 67 | +public class YourClass_temperature_DecodingFormula implements Function<Integer, Object> { |
| 68 | + @Override |
| 69 | + public Object apply(Integer x) { |
| 70 | + return x * 0.1; |
| 71 | + } |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +### Alternative: Custom Function Classes |
| 76 | + |
| 77 | +You can also use custom function classes directly without relying on the annotation processor: |
| 78 | + |
| 79 | +```java |
| 80 | +public class TemperatureFormula implements Function<Integer, Double> { |
| 81 | + @Override |
| 82 | + public Double apply(Integer value) { |
| 83 | + return value * 0.1; |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +public class SensorData { |
| 88 | + @Int16Type(offset = 0) |
| 89 | + @DecodingFormula(TemperatureFormula.class) |
| 90 | + private double temperature; |
| 91 | +} |
| 92 | +``` |
10 | 93 |
|
11 | 94 | ## Current Limitations |
12 | 95 |
|
13 | | -- Dynamic formula compilation is unavailable on Android |
14 | | - - Reason: Android runtime does not ship `javax.tools.JavaCompiler`, nor support loading JVM `.class` via `URLClassLoader`/`defineClass`. Only DEX is supported at runtime. |
15 | | - - Impact: Using string-based lambda in `@DecodingFormula(lambda = "...")` / `@EncodingFormula(lambda = "...")` will fail on Android. |
16 | 96 | - `java.sql.Timestamp` is not part of Android standard APIs |
17 | 97 | - Impact: Types/codec relying on `java.sql.*` will not work. Prefer `long` epoch millis or `java.time.*` (with desugaring) instead. |
18 | 98 |
|
19 | | -## How to Use on Android |
| 99 | +## Other Android Setup |
20 | 100 |
|
21 | | -- Formulas |
22 | | - - Do NOT use string-based lambdas in annotations. |
23 | | - - Instead, implement `java.util.function.Function` (or a typed interface) as a normal class and reference the class in annotations. |
24 | | -- Time Types |
| 101 | +- **Time Types** |
25 | 102 | - Prefer `long` (epoch millis) or `java.time.Instant/LocalDateTime`. |
26 | 103 | - Enable core library desugaring to use `java.time.*` on older Android. |
27 | | -- Gradle Setup (library/app module) |
| 104 | +- **Gradle Setup (library/app module)** |
28 | 105 | - compileOptions.sourceCompatibility/targetCompatibility = 1.8 |
29 | 106 | - coreLibraryDesugaringEnabled = true |
30 | 107 | - Add dependency: `com.android.tools:desugar_jdk_libs` |
31 | | -- R8/ProGuard |
| 108 | +- **R8/ProGuard** |
32 | 109 | - Keep runtime annotations and members used by reflection |
33 | 110 | - Example rules: |
34 | 111 |
|
35 | 112 | ```pro |
36 | 113 | -keepattributes RuntimeVisibleAnnotations, RuntimeVisibleParameterAnnotations, Signature, InnerClasses, EnclosingMethod |
37 | 114 | -keep class org.indunet.fastproto.annotation.** { *; } |
38 | | -# If you include the core jar and want to silence unused tools warnings: |
39 | | --dontwarn javax.tools.** |
40 | | --dontwarn java.net.URLClassLoader |
| 115 | +-keep class org.indunet.fastproto.formula.generated.** { *; } |
41 | 116 | ``` |
42 | 117 |
|
43 | | -## Known Issues and Roadmap |
44 | | - |
45 | | -- Dynamic compiler will be made optional/disabled on Android to avoid `javax.tools` and custom classloader usage |
46 | | -- Provide time codecs that do not depend on `java.sql.Timestamp` (prefer `Instant/long`) |
47 | | -- Consider publishing an Android-friendly artifact variant |
48 | | - |
49 | | -These improvements are planned for future releases. |
50 | | - |
51 | 118 | ## FAQ |
52 | 119 |
|
53 | | -- Can I still transform values? Yes. Implement a class-based function and refer to it in annotations, or transform in your own code before/after FastProto. |
54 | | -- Do I need ProGuard rules? If you rely on reflection/annotations, keep them as shown above. If you do not use dynamic compilation, shrinkers will strip those classes automatically. |
| 120 | +- **Can I use lambda formulas on Android?** Yes! Since 4.0.0, the `fastproto-processor` module generates formula classes at compile time, making lambda formulas fully compatible with Android. |
| 121 | +- **Do I need the annotation processor?** If you use `@DecodingFormula(lambda = "...")` or `@EncodingFormula(lambda = "...")`, yes. If you only use class-based formulas like `@DecodingFormula(MyFormula.class)`, the annotation processor is optional. |
| 122 | +- **Do I need ProGuard rules?** If you rely on reflection/annotations, keep them as shown above. The generated formula classes should also be kept. |
0 commit comments