I have a question about the new `@ConstructedBy` a...
# room
d
I have a question about the new
@ConstructedBy
and
RoomDatabaseConstructor
APIs in Room 2.7.0-alpha06. Would appreciate any insights.
🙌 1
d
I'm trying this as well. According to documentation I only need to create an expect object, but I keep getting compilation error
Expected object 'DatabaseCtor' has no actual declaration in module *
for every module that I have. I I add the implementation myself I'm getting SOF on IOS.
plus1 1
k
The documentation is a bit confusing, but I managed to get it working on version
2.7.0-alpha06
. All you need to do is create an
expect object
for the constructor (your
MusicDatabaseConstructor
) and specify it in the
@ConstructedBy
annotation for the database. KSP will override and generate the
initialize()
method for the platform on which you're building. In my case, I didn't specify the factory when creating the database in the Koin module because, thanks to
@ConstructedBy
, the appropriate
initialize()
implementation will be found. Despite Android Studio showing "Expected object...has no actual declaration" (even if you've generated it for all the platforms you need), this doesn't prevent the project from running successfully.
m
I'm getting this error every time I run the project: KSP does generate the actual impl for RoomDatabaseConstructor but the compiler says actual and expect impl are in the same module.
e: file:///Users/saif-ullah/Developer/ps/Expensify/composeApp/build/generated/ksp/metadata/commonMain/kotlin/data/local/database/ExpenseDatabaseConstructor.kt:5:22 'actual object ExpenseDatabaseConstructor : RoomDatabaseConstructor<ExpenseDatabase>' has no corresponding expected declaration
e: file:///Users/saif-ullah/Developer/ps/Expensify/composeApp/build/generated/ksp/metadata/commonMain/kotlin/data/local/database/ExpenseDatabaseConstructor.kt:5:22 ExpenseDatabaseConstructor: expect and corresponding actual are declared in the same module
e: file:///Users/saif-ullah/Developer/ps/Expensify/composeApp/build/generated/ksp/metadata/commonMain/kotlin/data/local/database/ExpenseDatabaseConstructor.kt:5:22 'actual object ExpenseDatabaseConstructor : RoomDatabaseConstructor<ExpenseDatabase>' has no corresponding expected declaration
e: file:///Users/saif-ullah/Developer/ps/Expensify/composeApp/build/generated/ksp/metadata/commonMain/kotlin/data/local/database/ExpenseDatabaseConstructor.kt:5:22 Redeclaration:
actual object ExpenseDatabaseConstructor : RoomDatabaseConstructor<ExpenseDatabase>
e: file:///Users/saif-ullah/Developer/ps/Expensify/composeApp/build/generated/ksp/metadata/commonMain/kotlin/data/local/database/ExpenseDatabaseConstructor.kt:5:22 'actual object ExpenseDatabaseConstructor : RoomDatabaseConstructor<ExpenseDatabase>' has no corresponding expected declaration
e: file:///Users/saif-ullah/Developer/ps/Expensify/composeApp/build/generated/ksp/metadata/commonMain/kotlin/data/local/database/ExpenseDatabaseConstructor.kt:5:22 Redeclaration:
actual object ExpenseDatabaseConstructor : RoomDatabaseConstructor<ExpenseDatabase>
e: file:///Users/saif-ullah/Developer/ps/Expensify/composeApp/build/generated/ksp/metadata/commonMain/kotlin/data/local/database/ExpenseDatabaseConstructor.kt:5:22 'actual object ExpenseDatabaseConstructor : RoomDatabaseConstructor<ExpenseDatabase>' has no corresponding expected declaration
e: file:///Users/saif-ullah/Developer/ps/Expensify/composeApp/build/generated/ksp/metadata/commonMain/kotlin/data/local/database/ExpenseDatabaseConstructor.kt:5:22 Redeclaration:
actual object ExpenseDatabaseConstructor : RoomDatabaseConstructor<ExpenseDatabase>
e: file:///Users/saif-ullah/Developer/ps/Expensify/composeApp/build/generated/ksp/metadata/commonMain/kotlin/data/local/database/ExpenseDatabaseConstructor.kt:5:22 'actual object ExpenseDatabaseConstructor : RoomDatabaseConstructor<ExpenseDatabase>' has no corresponding expected declaration
e: file:///Users/saif-ullah/Developer/ps/Expensify/composeApp/build/generated/ksp/metadata/commonMain/kotlin/data/local/database/ExpenseDatabaseConstructor.kt:5:22 Redeclaration:
actual object ExpenseDatabaseConstructor : RoomDatabaseConstructor<ExpenseDatabase>
e: file:///Users/saif-ullah/Developer/ps/Expensify/composeApp/build/generated/ksp/metadata/commonMain/kotlin/data/local/database/ExpenseDatabaseConstructor.kt:5:22 'actual object ExpenseDatabaseConstructor : RoomDatabaseConstructor<ExpenseDatabase>' has no corresponding expected declaration
e: file:///Users/saif-ullah/Developer/ps/Expensify/composeApp/build/generated/ksp/metadata/commonMain/kotlin/data/local/database/ExpenseDatabaseConstructor.kt:5:22 Redeclaration:
actual object ExpenseDatabaseConstructor : RoomDatabaseConstructor<ExpenseDatabase>
d
I managed to get it to work, but only for the Android target. I’ve been getting the same error as @Dmitrii Napolov. After adding this to my dependencies:
Copy code
dependencies {
    add("kspAndroid", libs.androidx.room.compiler)
    // ...
}
Nothing changed—still the same error. However, after closing the project, deleting the
.idea
folder from the root of the project, and reopening it, I finally got it to work on Android. That said, it’s still not generating anything for desktop or iOS, and I haven’t figured out why yet.
k
@Dean Djermanović desktop and iOS will be generated as soon as you launch the build for the corresponding platform
d
When I try to build desktop or iOS, I’m still getting the
Expected DatabaseConstructor has no actual declaration in module
error. It seems like some KSP config might be missing for those targets. Could you share your
build.gradle
file?
e
i added:
Copy code
dependencies {
    ksp(libs.room.compiler)
    add("kspAndroid",libs.room.compiler)
    add("kspIosSimulatorArm64",libs.room.compiler)
    add("kspIosX64",libs.room.compiler)
    add("kspIosArm64",libs.room.compiler)
}
and its building for ios now
a
Cannot change attributes of configuration 'composeAppdebugFrameworkIosX64' after it has been locked for mutation 🥲
But the dependencies step always throw me an error
d
Adding this line to the dependencies block made it work for desktop:
Copy code
add("kspDesktop", libs.androidx.room.compiler)
However, when I try to do similar for iOS:
Copy code
add("kspIosX64", libs.androidx.room.compiler)
add("kspIosArm64", libs.androidx.room.compiler)
add("kspIosSimulatorArm64", libs.androidx.room.compiler)
I get this error:
Copy code
Cannot change attributes of configuration ':composeApp:debugFrameworkIosX64' after it has been locked for mutation.
d
The issue regarding
Cannot change attributes of configuration
is due to the combination of the Compose and Room plugin being used together, a workaround is described in https://issuetracker.google.com/343408758#comment4
thank you color 1
d
I tried removing the Room Gradle Plugin, but I’m still getting the same error. These are the versions I’m using: • Compose plugin:
1.7.0-alpha01
• KSP plugin:
2.0.10-1.0.24
• Kotlin:
2.0.10
d
Hmm, in my small project I was able to resolve the
Cannot change attributes of configuration
by removing the Room Gradle but I am using Compose Plugin version
1.6.11
. But with
1.7.0-alpha01
the problem is back, then I think the issue must be KSP + Compose Plugin. We'll need to file a bug to one of those two plugin owners.
👍 1
p
If you get the expect/actual same module error, it's because you're running KSP on the common module, you don't need it, just on the target platforms, e.g.
Copy code
dependencies {
    add("kspAndroid", libs.androidx.room.compiler)
    add("kspIosX64", libs.androidx.room.compiler)
    add("kspIosArm64", libs.androidx.room.compiler)
    add("kspIosSimulatorArm64", libs.androidx.room.compiler)
}
🙌 2
m
@Dean Djermanović i have issue similar issue
Copy code
ksp = "2.0.20-1.0.24"
androidx-room = "2.7.0-alpha07"
d
There is a big comment on https://issuetracker.google.com/342905180#comment21 that go over a few points to get Room working in a project, please check it out.
u
Having this issue now. Kotlin - 2.0.20 room - 2.7.0-alpha07 compose - 1.7.0-beta02 ksp - 2.0.20-1.0.25
m
i possible try it not work kspDesktop or ksjvm missing error show it
535 Views