df7b11afc3
**! ! Warning**: Do not merge before testing every API call and database read involving JSON ! **Gson** is obsolete and has been superseded by **Moshi**. But more importantly, parsing Kotlin objects using Gson is _dangerous_ because Gson uses Java serialization and is **not Kotlin-aware**. This has two main consequences: - Fields of non-null types may end up null at runtime. Parsing will succeed, but the code may crash later with a `NullPointerException` when trying to access a field member; - Default values of constructor parameters are always ignored. When absent, reference types will be null, booleans will be false and integers will be zero. On the other hand, Kotlin-aware parsers like **Moshi** or **Kotlin Serialization** will validate at parsing time that all received fields comply with the Kotlin contract and avoid errors at runtime, making apps more stable and schema mismatches easier to detect (as long as logs are accessible): - Receiving a null value for a non-null type will generate a parsing error; - Optional types are declared explicitly by adding a default value. **A missing value with no default value declaration will generate a parsing error.** Migrating the entity declarations from Gson to Moshi will make the code more robust but is not an easy task because of the semantic differences. With Gson, both nullable and optional fields are represented with a null value. After converting to Moshi, some nullable entities can become non-null with a default value (if they are optional and not nullable), others can stay nullable with no default value (if they are mandatory and nullable), and others can become **nullable with a default value of null** (if they are optional _or_ nullable _or_ both). That third option is the safest bet when it's not clear if a field is optional or not, except for lists which can usually be declared as non-null with a default value of an empty list (I have yet to see a nullable array type in the Mastodon API). Fields that are currently declared as non-null present another challenge. In theory, they should remain as-is and everything will work fine. In practice, **because Gson is not aware of nullable types at all**, it's possible that some non-null fields currently hold a null value in some cases but the app does not report any error because the field is not accessed by Kotlin code in that scenario. After migrating to Moshi however, parsing such a field will now fail early if a null value or no value is received. These fields will have to be identified by heavily testing the app and looking for parsing errors (`JsonDataException`) and/or by going through the Mastodon documentation. A default value needs to be added for missing optional fields, and their type could optionally be changed to nullable, depending on the case. Gson is also currently used to serialize and deserialize objects to and from the local database, which is also challenging because backwards compatibility needs to be preserved. Fortunately, by default Gson omits writing null fields, so a field of type `List<T>?` could be replaced with a field of type `List<T>` with a default value of `emptyList()` and reading back the old data should still work. However, nullable lists that are written directly (not as a field of another object) will still be serialized to JSON as `"null"` so the deserializing code must still be handling null properly. Finally, changing the database schema is out of scope for this pull request, so database entities that also happen to be serialized with Gson will keep their original types even if they could be made non-null as an improvement. In the end this is all for the best, because the app will be more reliable and errors will be easier to detect by showing up earlier with a clear error message. Not to mention the performance benefits of using Moshi compared to Gson. - Replace Gson reflection with Moshi Kotlin codegen to generate all parsers at compile time. - Replace custom `Rfc3339DateJsonAdapter` with the one provided by moshi-adapters. - Replace custom `JsonDeserializer` classes for Enum types with `EnumJsonAdapter.create(T).withUnknownFallback()` from moshi-adapters to support fallback values. - Replace `GuardedBooleanAdapter` with the more generic `GuardedAdapter` which works with any type. Any nullable field may now be annotated with `@Guarded`. - Remove Proguard rules related to Json entities. Each Json entity needs to be annotated with `@JsonClass` with no exception, and adding this annotation will ensure that R8/Proguard will handle the entities properly. - Replace some nullable Boolean fields with non-null Boolean fields with a default value where possible. - Replace some nullable list fields with non-null list fields with a default value of `emptyList()` where possible. - Update `TimelineDao` to perform all Json conversions internally using `Converters` so no Gson or Moshi instance has to be passed to its methods. - ~~Create a custom `DraftAttachmentJsonAdapter` to serialize and deserialize `DraftAttachment` which is a special entity that supports more than one json name per field. A custom adapter is necessary because there is not direct equivalent of `@SerializedName(alternate = [...])` in Moshi.~~ Remove alternate names for some `DraftAttachment` fields which were used as a workaround to deserialize local data in 2-years old builds of Tusky. - Update tests to make them work with Moshi. - Simplify a few `equals()` implementations. - Change a few functions to `val`s - Turn `NetworkModule` into an `object` (since it contains no abstract methods). Please test the app thoroughly before merging. There may be some fields currently declared as mandatory that are actually optional.
207 lines
6.8 KiB
Groovy
207 lines
6.8 KiB
Groovy
plugins {
|
|
alias(libs.plugins.android.application)
|
|
alias(libs.plugins.google.ksp)
|
|
alias(libs.plugins.kotlin.android)
|
|
alias(libs.plugins.kotlin.kapt)
|
|
alias(libs.plugins.kotlin.parcelize)
|
|
}
|
|
|
|
apply from: 'getGitSha.gradle'
|
|
|
|
final def gitSha = ext.getGitSha()
|
|
|
|
// The app name
|
|
final def APP_NAME = "Tusky"
|
|
// The application id. Must be unique, e.g. based on your domain
|
|
final def APP_ID = "com.keylesspalace.tusky"
|
|
// url of a custom app logo. Recommended size at least 600x600. Keep empty to use the Tusky elephant friend.
|
|
final def CUSTOM_LOGO_URL = ""
|
|
// e.g. mastodon.social. Keep empty to not suggest any instance on the signup screen
|
|
final def CUSTOM_INSTANCE = ""
|
|
// link to your support account. Will be linked on the about page when not empty.
|
|
final def SUPPORT_ACCOUNT_URL = "https://mastodon.social/@Tusky"
|
|
|
|
android {
|
|
compileSdk 34
|
|
namespace "com.keylesspalace.tusky"
|
|
defaultConfig {
|
|
applicationId APP_ID
|
|
namespace "com.keylesspalace.tusky"
|
|
minSdk 24
|
|
targetSdk 34
|
|
versionCode 117
|
|
versionName "24.1"
|
|
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
|
vectorDrawables.useSupportLibrary = true
|
|
|
|
resValue "string", "app_name", APP_NAME
|
|
|
|
buildConfigField("String", "CUSTOM_LOGO_URL", "\"$CUSTOM_LOGO_URL\"")
|
|
buildConfigField("String", "CUSTOM_INSTANCE", "\"$CUSTOM_INSTANCE\"")
|
|
buildConfigField("String", "SUPPORT_ACCOUNT_URL", "\"$SUPPORT_ACCOUNT_URL\"")
|
|
}
|
|
buildTypes {
|
|
debug {
|
|
isDefault true
|
|
}
|
|
release {
|
|
minifyEnabled true
|
|
shrinkResources true
|
|
proguardFiles 'proguard-rules.pro'
|
|
|
|
kotlinOptions {
|
|
freeCompilerArgs = [
|
|
"-Xno-param-assertions",
|
|
"-Xno-call-assertions",
|
|
"-Xno-receiver-assertions"
|
|
]
|
|
}
|
|
}
|
|
}
|
|
|
|
flavorDimensions += "color"
|
|
productFlavors {
|
|
blue {}
|
|
green {
|
|
resValue "string", "app_name", APP_NAME + " Test"
|
|
applicationIdSuffix ".test"
|
|
versionNameSuffix "-" + gitSha
|
|
isDefault true
|
|
}
|
|
}
|
|
|
|
lint {
|
|
lintConfig file("lint.xml")
|
|
// Regenerate by running `./gradlew app:newLintBaseline`
|
|
baseline = file("lint-baseline.xml")
|
|
}
|
|
|
|
buildFeatures {
|
|
buildConfig true
|
|
resValues true
|
|
viewBinding true
|
|
}
|
|
testOptions {
|
|
unitTests {
|
|
returnDefaultValues = true
|
|
includeAndroidResources = true
|
|
}
|
|
unitTests.all {
|
|
systemProperty 'robolectric.logging.enabled', 'true'
|
|
systemProperty 'robolectric.lazyload', 'ON'
|
|
}
|
|
}
|
|
sourceSets {
|
|
androidTest.assets.srcDirs += files("$projectDir/schemas".toString())
|
|
}
|
|
|
|
// Exclude unneeded files added by libraries
|
|
packagingOptions.resources.excludes += [
|
|
'LICENSE_OFL',
|
|
'LICENSE_UNICODE',
|
|
]
|
|
|
|
bundle {
|
|
language {
|
|
// bundle all languages in every apk so the dynamic language switching works
|
|
enableSplit = false
|
|
}
|
|
}
|
|
dependenciesInfo {
|
|
includeInApk false
|
|
includeInBundle false
|
|
}
|
|
applicationVariants.configureEach { variant ->
|
|
variant.outputs.configureEach {
|
|
outputFileName = "Tusky_${variant.versionName}_${variant.versionCode}_${gitSha}_" +
|
|
"${variant.flavorName}_${buildType.name}.apk"
|
|
}
|
|
}
|
|
}
|
|
|
|
ksp {
|
|
arg("room.schemaLocation", "$projectDir/schemas")
|
|
arg("room.generateKotlin", "true")
|
|
arg("room.incremental", "true")
|
|
}
|
|
|
|
configurations {
|
|
// JNI-only libraries don't play nicely with Robolectric
|
|
// see https://github.com/tuskyapp/Tusky/pull/3367
|
|
testImplementation.exclude group: "org.conscrypt", module: "conscrypt-android"
|
|
testRuntime.exclude group: "org.conscrypt", module: "conscrypt-android"
|
|
}
|
|
|
|
// library versions are in PROJECT_ROOT/gradle/libs.versions.toml
|
|
dependencies {
|
|
implementation libs.kotlinx.coroutines.android
|
|
|
|
implementation libs.bundles.androidx
|
|
implementation libs.bundles.room
|
|
ksp libs.androidx.room.compiler
|
|
|
|
implementation libs.android.material
|
|
|
|
implementation libs.bundles.moshi
|
|
ksp libs.moshi.kotlin.codegen
|
|
|
|
implementation libs.bundles.retrofit
|
|
implementation libs.networkresult.calladapter
|
|
|
|
implementation libs.bundles.okhttp
|
|
|
|
implementation libs.conscrypt.android
|
|
|
|
implementation libs.bundles.glide
|
|
ksp libs.glide.compiler
|
|
|
|
implementation libs.bundles.dagger
|
|
kapt libs.bundles.dagger.processors
|
|
|
|
implementation libs.sparkbutton
|
|
|
|
implementation libs.touchimageview
|
|
|
|
implementation libs.bundles.material.drawer
|
|
implementation libs.material.typeface
|
|
|
|
implementation libs.image.cropper
|
|
|
|
implementation libs.bundles.filemojicompat
|
|
|
|
implementation libs.bouncycastle
|
|
implementation libs.unified.push
|
|
|
|
implementation libs.bundles.xmldiff
|
|
|
|
testImplementation libs.androidx.test.junit
|
|
testImplementation libs.robolectric
|
|
testImplementation libs.bundles.mockito
|
|
testImplementation libs.mockwebserver
|
|
testImplementation libs.androidx.core.testing
|
|
testImplementation libs.kotlinx.coroutines.test
|
|
testImplementation libs.androidx.work.testing
|
|
testImplementation libs.truth
|
|
testImplementation libs.turbine
|
|
|
|
androidTestImplementation libs.espresso.core
|
|
androidTestImplementation libs.androidx.room.testing
|
|
androidTestImplementation libs.androidx.test.junit
|
|
}
|
|
|
|
// Work around warnings of:
|
|
// WARNING: Illegal reflective access by org.jetbrains.kotlin.kapt3.util.ModuleManipulationUtilsKt (file:/C:/Users/Andi/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-annotation-processing-gradle/1.8.22/28dab7e0ee9ce62c03bf97de3543c911dc653700/kotlin-annotation-processing-gradle-1.8.22.jar) to constructor com.sun.tools.javac.util.Context()
|
|
// See https://youtrack.jetbrains.com/issue/KT-30589/Kapt-An-illegal-reflective-access-operation-has-occurred
|
|
tasks.withType(org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask).configureEach {
|
|
kaptProcessJvmArgs.addAll([
|
|
"--add-opens", "jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
|
|
"--add-opens", "jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
|
|
"--add-opens", "jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED",
|
|
"--add-opens", "jdk.compiler/com.sun.tools.javac.jvm=ALL-UNNAMED",
|
|
"--add-opens", "jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED",
|
|
"--add-opens", "jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED",
|
|
"--add-opens", "jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED",
|
|
"--add-opens", "jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
|
|
"--add-opens", "jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED",
|
|
"--add-opens", "jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED"])
|
|
}
|