Skip to content

Commit

Permalink
fixed issue: migrate to ksp annotation processor (#342)
Browse files Browse the repository at this point in the history
* fixed issue: migrate to ksp annotation processor

* removed useless commas

* fixed issue: lateinit property testDB has not been initialized

* removed useless import in Test
  • Loading branch information
Nassr-Allah authored Sep 26, 2023
1 parent 3ca5ef1 commit 82d1aa7
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 22 deletions.
26 changes: 11 additions & 15 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
plugins {
id("com.android.application")
id("kotlin-android")
id("kotlin-kapt")
id("com.google.devtools.ksp")
id("dagger.hilt.android.plugin")
id("androidx.navigation.safeargs.kotlin")
id("org.jlleitschuh.gradle.ktlint")
Expand All @@ -21,12 +21,11 @@ android {
testInstrumentationRunner = "org.eu.exodus_privacy.exodusprivacy.ExodusTestRunner"
val API_KEY = System.getenv("EXODUS_API_KEY")
buildConfigField("String", "EXODUS_API_KEY", "\"$API_KEY\"")
javaCompileOptions {
annotationProcessorOptions {
compilerArgumentProviders(
RoomSchemaArgProvider(File(projectDir, "schemas"))
)
}

ksp {
arg(
RoomSchemaArgProvider(File(projectDir, "schemas")),
)
}

signingConfigs {
Expand Down Expand Up @@ -87,9 +86,6 @@ android {
generateLocaleConfig = true
}
}
kapt {
correctErrorTypes = true
}

dependencies {
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
Expand Down Expand Up @@ -127,7 +123,7 @@ dependencies {
implementation(libs.okhttp)

// Hilt
kapt(libs.dagger.hilt.compiler)
ksp(libs.dagger.hilt.compiler)
implementation(libs.dagger.hilt.android)

// Lifecycle
Expand All @@ -136,7 +132,7 @@ dependencies {
implementation(libs.androidx.lifecycle.service)

// Room
kapt(libs.androidx.room.compiler)
ksp(libs.androidx.room.compiler)
implementation(libs.androidx.room.ktx)
implementation(libs.androidx.room.runtime)

Expand All @@ -153,10 +149,10 @@ dependencies {
testImplementation(libs.androidx.test.rules)
testImplementation(libs.dagger.hilt.android.testing)
testImplementation(libs.okhttp.mockwebserver)
kaptTest(libs.dagger.hilt.compiler)
kspTest(libs.dagger.hilt.compiler)

// instrumentation tests
kaptAndroidTest(libs.dagger.hilt.compiler)
kspAndroidTest(libs.dagger.hilt.compiler)
androidTestImplementation(libs.dagger.hilt.android.testing)
androidTestImplementation(libs.androidx.test.core)
androidTestImplementation(libs.androidx.test.core.ktx)
Expand All @@ -171,7 +167,7 @@ dependencies {
class RoomSchemaArgProvider(
@get:InputDirectory
@get:PathSensitive(PathSensitivity.RELATIVE)
val schemaDir: File
val schemaDir: File,
) : CommandLineArgumentProvider {
override fun asArguments() = listOf("room.schemaLocation=${schemaDir.path}")
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@
}
],
"primaryKey": {
"autoGenerate": false,
"columnNames": [
"id"
],
"autoGenerate": false
]
},
"indices": [],
"foreignKeys": []
Expand Down Expand Up @@ -169,10 +169,10 @@
}
],
"primaryKey": {
"autoGenerate": false,
"columnNames": [
"packageName"
],
"autoGenerate": false
]
},
"indices": [],
"foreignKeys": []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import dagger.hilt.android.testing.HiltAndroidTest
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.runTest
import org.eu.exodus_privacy.exodusprivacy.manager.database.ExodusDatabase
import org.eu.exodus_privacy.exodusprivacy.manager.database.ExodusDatabaseConverters
import org.eu.exodus_privacy.exodusprivacy.manager.database.ExodusDatabaseRepository
import org.eu.exodus_privacy.exodusprivacy.manager.database.app.ExodusApplication
import org.eu.exodus_privacy.exodusprivacy.manager.database.tracker.TrackerData
Expand Down Expand Up @@ -40,6 +41,7 @@ class ExodusDatabaseRepositoryTest {
private lateinit var image: Bitmap
private lateinit var testDB: ExodusDatabase
private lateinit var exodusDatabaseRepository: ExodusDatabaseRepository
private lateinit var exodusTypeConverter: ExodusDatabaseConverters

private val packageName = "com.test.testapp"
private val packageName2 = "com.test.testapp2"
Expand All @@ -64,11 +66,12 @@ class ExodusDatabaseRepositoryTest {
assets = context.assets
bitmapStream = assets.open("mipmap/big_square_bigfs.png")
image = BitmapFactory.decodeStream(bitmapStream)
exodusTypeConverter = ExodusDatabaseConverters()

testDB = Room.inMemoryDatabaseBuilder(
context,
ExodusDatabase::class.java
).build()
).addTypeConverter(exodusTypeConverter).build()

exodusDatabaseRepository = ExodusDatabaseRepository(
testDB,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import org.eu.exodus_privacy.exodusprivacy.objects.Constants
autoMigrations = [
AutoMigration(
from = Constants.previousDatabaseVersion,
to = Constants.currentDatabaseVersion
to = Constants.currentDatabaseVersion,
)
]
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package org.eu.exodus_privacy.exodusprivacy.manager.database

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import androidx.room.ProvidedTypeConverter
import androidx.room.TypeConverter
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import org.eu.exodus_privacy.exodusprivacy.objects.Permission
import java.io.ByteArrayOutputStream

@ProvidedTypeConverter
class ExodusDatabaseConverters {

@TypeConverter
Expand All @@ -21,6 +23,17 @@ class ExodusDatabaseConverters {
return Gson().toJson(list)
}

@TypeConverter
fun toMutableStringList(string: String): MutableList<String> {
val listType = object : TypeToken<MutableList<String>>() {}.type
return Gson().fromJson(string, listType)
}

@TypeConverter
fun fromMutableStringList(list: MutableList<String>): String {
return Gson().toJson(list)
}

@TypeConverter
fun toIntList(string: String): List<Int> {
val listType = object : TypeToken<List<Int>>() {}.type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ import javax.inject.Singleton
object ExodusDatabaseModule {

private const val DATABASE_NAME = "exodus_database"
private val exodusTypeConverter = ExodusDatabaseConverters()

@Singleton
@Provides
fun provideExodusDatabaseInstance(@ApplicationContext context: Context): ExodusDatabase {
return Room.databaseBuilder(context, ExodusDatabase::class.java, DATABASE_NAME).build()
return Room.databaseBuilder(context, ExodusDatabase::class.java, DATABASE_NAME)
.addTypeConverter(exodusTypeConverter).build()
}
}
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ plugins {
alias(libs.plugins.androidx.navigation.safeargs.kotlin) apply false
alias(libs.plugins.dagger.hilt.android) apply false
alias(libs.plugins.gradle.ktlint) apply false
alias(libs.plugins.kotlin.ksp) apply false
}

tasks.register("clean", Delete::class) {
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ runner = "1.5.2"
shimmer = "0.5.0"
swiperefreshlayout = "1.1.0"
window = "1.1.0"
ksp = "1.8.22-1.0.11"

[libraries]
androidx-activity-ktx = { module = "androidx.activity:activity-ktx", version.ref = "activity-ktx" }
Expand Down Expand Up @@ -95,3 +96,4 @@ kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlinPlu
androidx-navigation-safeargs-kotlin = { id = "androidx.navigation.safeargs.kotlin", version.ref = "navigation-fragment-ktx" }
dagger-hilt-android = { id = "com.google.dagger.hilt.android", version.ref = "dagger-hilt" }
gradle-ktlint = { id = "org.jlleitschuh.gradle.ktlint", version.ref = "gradleKtlint" }
kotlin-ksp = { id = "com.google.devtools.ksp", version.ref = "ksp"}

0 comments on commit 82d1aa7

Please sign in to comment.