realm / realm-java

Realm is a mobile database: a replacement for SQLite & ORMs
http://realm.io
Apache License 2.0
11.45k stars 1.75k forks source link

RealmTransformer not executing on with AGP 7.2.0/1 #7685

Closed pedronveloso closed 1 year ago

pedronveloso commented 2 years ago

How frequently does the bug occur?

All the time

Description

Android Studio just updated their stable IDE version. I'm using the recommended Gradle tooling version that came with the IDE update. The app compiles normally, but crashes at runtime - and according to the error it seems fairly clear that the Realm plugin didn't work as expected. Let me know if you need further information.

Stacktrace & log output

java.lang.ExceptionInInitializerError: RealmTransformer doesn't seem to be applied. Please update the project configuration to use the Realm Gradle plugin. See https://docs.mongodb.com/realm/sdk/android/install/#customize-dependecies-defined-by-the-realm-gradle-plugin
        at io.realm.RealmConfiguration.<clinit>(RealmConfiguration.java:80)
        at io.realm.RealmConfiguration.access$000(RealmConfiguration.java:68)
        at io.realm.RealmConfiguration$Builder.initializeBuilder(RealmConfiguration.java:552)
        at io.realm.RealmConfiguration$Builder.<init>(RealmConfiguration.java:538)
        at io.realm.Realm.initializeRealm(Realm.java:320)
        at io.realm.Realm.init(Realm.java:261)
        at com.patreon.android.data.manager.RealmManager.initialize(RealmManager.java:55)


### Can you reproduce the bug?

Yes, always

### Reproduction Steps

_No response_

### Version

10.10.1

### What SDK flavour are you using?

Local Database only

### Are you using encryption?

No, not using encryption

### Platform OS and version(s)

all

### Build environment

Android Studio version: Android Studio Chipmunk | 2021.2.1
Build #AI-212.5712.43.2112.8512546, built on April 28, 2022

Gradle : gradle-7.3.3-all

Realm: 10.10.1
ewnu commented 2 years ago

I get same results when I update AGP from 7.1.3 to 7.2.0. I'm sticking with 7.1.3 until this gets fixed

cmelchior commented 2 years ago

I tried to reproduce this with our IntroExample, Chipmunk and AGP 7.2.0 and it seemed to work fine.

Can you provide more information about your project setup? If you can reproduce in a sample project it would be even better.

Also, have you tried a clean build to see if that fixes the issue?

VicMikhailau commented 2 years ago

@cmelchior The same result. After upadtaing to the Chipmunk and AGP 7.2.0 - all works as expected.

cmelchior commented 2 years ago

@VicMikhailau Not 100% sure if you mean it worked after upgrading or it failed with the same error that @pedronveloso is reporting?

VicMikhailau commented 2 years ago

@cmelchior It worked after upgrading without any issues.

pedronveloso commented 2 years ago

Clean build also has the same result. It compiles just fine, but crashes at runtime. I'll try Realm's sample for comparison.

patricknanganocappello commented 2 years ago

Got the same problem, I've also tried a clean build without any success

anshulmak commented 2 years ago

Got the same issue. I've tried clean build and Invalidate Caches too but the apk is crashing.

VadimWelldone commented 2 years ago

Also, got crashes at runtime. Maybe it's related to this breaking change in AGP 7.2: https://developer.android.com/studio/releases/gradle-plugin-api-updates#instrumentatin-apis-deprecated because AGP produces such a warning message and points out to Realm plugin:

AGPBI: {"kind":"warning","text":"API 'android.registerTransform' is obsolete.\nIt will be removed in version 8.0 of the Android Gradle plugin.\nThe Transform API is removed to improve build performance. Projects that use the\nTransform API force the Android Gradle plugin to use a less optimized flow for the\nbuild that can result in large regressions in build times. It’s also difficult to\nuse the Transform API and combine it with other Gradle features; the replacement\nAPIs aim to make it easier to extend the build without introducing performance or\ncorrectness issues.\n\nThere is no single replacement for the Transform API—there are new, targeted\nAPIs for each use case. All the replacement APIs are in the\nandroidComponents {}block.\nFor more information, see https://developer.android.com/studio/releases/gradle-plugin-api-updates#transform-api.\nTo determine what is calling android.registerTransform, use -Pandroid.debug.obsoleteApi=true on the command line to display more information.","sources":[{}]}

cmelchior commented 2 years ago

Hi @VadimWelldone The API's should only be removed in AGP 8.0, not 7.2. So something else must be causing it.

For anyone else seeing this. Some more details on exactly you project structure looks like would be appreciated. I have tested with AGP 7.2.0 and Gradle 7.3.3 where it did work on a simple project. So maybe it is a specific project structure triggering this, or certain grade properties being set.

pedronveloso commented 2 years ago

@cmelchior I wasn't able to get studio to properly initialize the demos from the repo. Looking at the Realm docs, the main different I see in my current setup is that I'm using the "Gradle Legacy Syntax" as opposed to the newer "Gradle Plugin Syntax". Not sure if this could be the root cause, but its the first thing that stood out to me.

patricknanganocappello commented 2 years ago

@cmelchior I wasn't able to get studio to properly initialize the demos from the repo. Looking at the Realm docs, the main different I see in my current setup is that I'm using the "Gradle Legacy Syntax" as opposed to the newer "Gradle Plugin Syntax". Not sure if this could be the root cause, but its the first thing that stood out to me.

I just tried to change my project gradle setup to the newer gradle plugin sintax and I get the same issue. Still crashes at runtime

andersfredlund commented 2 years ago

Same problem here, my debug build works though not using proguard/minify so could it be something in the code optimisation step which brakes the build? Using Dagger only...

zhaosunny1 commented 2 years ago

Butterknife And Realm both got errors

monday8am commented 2 years ago

Same problem here. In our case, it works in a branch without Hilt and the one with Dagger Hilt crashes. Going to 7.1.3 works.

PriyaSindkar commented 2 years ago

I get same results when I update AGP from 7.1.3 to 7.2.0. I'm sticking with 7.1.3 until this gets fixed

going back to 7.1.3 worked for me as well.

PriyaSindkar commented 2 years ago

Same problem here. In our case, it works in a branch without Hilt and the one with Dagger Hilt crash

@clementetb - if it helps, I am using hilt in my project..

Jeff11 commented 2 years ago

I got the same error stack trace with gradle plugin 7.2.0. Also using Hilt.

CsabaMiomni commented 2 years ago

The same error here. Back to 7.1.3 works fine.

cmelchior commented 2 years ago

Hi everyone. We still have issues reproducing this. We have a suspicion it might be related to Hilt, but so far we haven't been able to reproduce it. So if anyone have a sample project where this can be reproduced it would be highly appreciated.

vernazza commented 2 years ago

I have the same problem (not working in 7.2, working in 7.1.3 I'm not using HILT and, looking at the dependency tree, no library is using it either

cmelchior commented 2 years ago

Hi @vernazza Thank you for the information 👍

ewnu commented 2 years ago

@cmelchior I'm not using hilt either.

bobekos commented 2 years ago

@cmelchior

I too get crashes at runtime on Gradle version 7.2.0. There are no problems on 7.1.3. The errors are also strange. It almost seems that the proxy objects are null when access. Although the objects are filled when looking at them with the debugger. Writing to the DB doesn't seem to be a problem. My project also doesn't use hilt but dagger. I could not reproduce it on the fly with a fresh project. I don't know where the differences are. In the new project I used the same Realm and Dagger version. The only difference, the new project was in Kotlin and the other one in Java.

ghost commented 2 years ago

I can confirm I started having this issue after installing hilt and adding @HiltAndroidApp to my application class. Unfortunately I cannot share my project as the code is not open sourced.

nhachicha commented 2 years ago

I created a sample project using the latest Realm-Java with Dagger and AGP 7.2 https://github.com/nhachicha/realm_java_7685 (and it's working) could you please use it as a baseline to reproduce the issue so we can investigate cc @GriffinJBC @bobekos @ewnu @vernazza @pedronveloso

ahmedsiddique-cb commented 2 years ago

I am using Hilt+Realm in my android project and i am facing this issue as well after upgrading to 7.2.0 after downgrading to 7.1.3 as suggested by the users worked, but it's not a permanent solution.

bobekos commented 2 years ago

@nhachicha No chance i can't reproduce it. That's super weird. In my project I can't really understand it either. I know why it crashes. It is simply a null pointer to a RealmObject which is 100% filled. The funny thing is, if I copy the exact same code (to fetch a realm object) somewhere else (into an existing class) then it works. If I create a new class that is identical, it doesn't work. The realm objects and the proxies are identical in both. But in one I can read the attributes in the other there are null. I can't even tell you what the reason is. Already deleted all cashes and cleaned up the project.

bobekos commented 2 years ago

@nhachicha Ok I could at least tell where the difference is. I use a multi module project. I had to move the class from the app module to my "core" module. In the "core" module the realm configuration is provided via dagger. And the class that i moved is injecting this realm over the constructor. I tried to do the same setup in the test project. Until now without success.

PVGH19 commented 2 years ago

So I'm also getting this error but I know exactly why it's happening: For me, it's because I introduced DaggerHilt which also uses an android gradle plugin. So I think that the Dagger Hilt plugin and Realm plugin are incompatible. So there may be some incompatible plugins within some applications...

Note: I created a new project with Realm + Dagger Hilt and confirmed this is the case. These are my configurations:

build.gradle

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath "io.realm:realm-gradle-plugin:10.11.0"
        classpath 'com.google.dagger:hilt-android-gradle-plugin:2.42'
    }
}

plugins {
    id 'com.android.application' version '7.2.1' apply false
    id 'com.android.library' version '7.2.1' apply false
    id 'org.jetbrains.kotlin.android' version '1.6.21' apply false
    id "org.jetbrains.kotlin.kapt" version "1.6.21" apply false
}

build.gradle(app)

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'org.jetbrains.kotlin.kapt'
}

apply plugin: 'dagger.hilt.android.plugin'
apply plugin: "realm-android"

Gradle: Gradle : gradle-7.3.3-all

Edit: Setting id 'com.android.application' version '7.2.1' apply false in the plugins to 7.1.3 worked as described by @ahmedsiddique-cb

Apologies I didn't see your comment when I initially created the post.

cmelchior commented 2 years ago

@PVGH19 Yes, that is possible. You can see how here:

https://www.mongodb.com/docs/realm-legacy/docs/java/latest/index.html#how-do-i-customize-dependecies-defined-by-the-realm-gradle-plugin

ewnu commented 2 years ago

This is weird. I think its noting to do with dagger / hilt I'm not using any of them in my project.

As @bobekos described it seems to me multi module setup is the issue here. We also extensively use modules and some of them uses Realm.

PVGH19 commented 2 years ago

@ewnu I can assure you this is true. I created a very small project with just Dagger Hilt + Realm and it did not work with android application 7.2.1. It did work with 7.1.3. Now there may be other reasons that it is happening to you specifically.

Jeff11 commented 2 years ago

@PVGH19 could you upload your test project for @cmelchior and others to debug please?

PVGH19 commented 2 years ago

@Jeff11 @cmelchior I've added the sample project here: https://github.com/PVGH19/RealmAndDagger

Initially, it should run correctly. As soon as we change android.application version to 7.2.1 you should see the RealmTransformer crash.

SuperTango commented 2 years ago

More data: In our project, we have HILT defined, but we're not really using it. It works correctly with Gradle 7.2.0, but not 7.3.1. 🤷‍♂️

TemMax commented 2 years ago

After updating to AGP 7.2.0 faced the same issue on a Production build in runtime. Please, fix it.

vpmalley commented 2 years ago

I'm running into the same issue, it is likely linked to the version 7.2.x of the gradle plugin (with version 7.3.3 of the wrapper). In my case, I only realised it when compiling a release version of the app, it works fine in debug mode. I wonder what differences in these could cause that. I'm showing the build variant differences if it inspires anyone:

debug {
    versionNameSuffix "-debug"
    manifestPlaceholders = [enableCrashReporting: "false"]
    minifyEnabled false
    proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    FirebasePerformance {
        instrumentationEnabled false
    }
}
release {
    manifestPlaceholders = [enableCrashReporting: "true"]
    minifyEnabled true
    shrinkResources true
    proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    signingConfig signingConfigs.release
}

And the proguard rules for Realm (I didn't change anything in it) (not minifying didn't stop the app from crashing though).

## Realm

-keep class io.realm.annotations.RealmModule
-keep @io.realm.annotations.RealmModule class *
-keep class io.realm.internal.Keep
-keep @io.realm.internal.Keep class *
-dontwarn javax.**
-dontwarn io.realm.**
nhachicha commented 2 years ago

Thanks @PVGH19 I'm able to reproduce the issue. I'll have a look.

nhachicha commented 2 years ago

Some updates regarding the issue:

Using hilt gradle plugin beyond version 2.38 can cause the RealmTransformer to not trigger, this might be caused by the fact that hilt strated using the new ASM Transformer API (which will be the only Transformer supported from AGP 8.0 onwoard).

Using @PVGH19 sample one can use com.google.dagger:hilt-android-gradle-plugin:2.38 with AGP 7.2.1 and it still works. Another work around is to keep using AGP 7.1.3 until we finish the process of rewriting the transformer using ASM (instead of Javassist currently).

We will post here once we completed the re-write of the trasnformer.

pedronveloso commented 1 year ago

Some updates regarding the issue:

Using hilt gradle plugin beyond version 2.38 can cause the RealmTransformer to not trigger, this might be caused by the fact that hilt strated using the new ASM Transformer API (which will be the only Transformer supported from AGP 8.0 onwoard).

Using @PVGH19 sample one can use com.google.dagger:hilt-android-gradle-plugin:2.38 with AGP 7.2.1 and it still works. Another work around is to keep using AGP 7.1.3 until we finish the process of rewriting the transformer using ASM (instead of Javassist currently).

We will post here once we completed the re-write of the trasnformer.

Sadly I can't do this because Kotlin requires a certain minimum version of Hilt, and 2.38 is more than a year old, I believe it supports version of Kotlin around 1.5 which is also pretty old at this point.

o15a3d4l11s2 commented 1 year ago

I noticed something strange:

Gradle: 7.2.1
Realm: tested with 10.11.1 and and older app with 6.1.0

All works fine until Firebase Performance Monitoring (com.google.firebase:firebase-perf) is added. Then the error shows.

fanwgwg commented 1 year ago

Friendly ping, this issue has been open since May, is there a timeline when we can expect it to be resolved? This is currently blocking our migration of other libraries, which requires AGP minimum version of 7.2

Jeff11 commented 1 year ago

Today I updated all libs and tools and this crash isn't happening anymore for me.

Gradle plugin 7.2.2 Dagger 2.43.2 Realm 10.11.1

Edit: Confirmed! https://issuetracker.google.com/issues/232438924

cmelchior commented 1 year ago

@fanwgwg We don't have an exact timeline yet as we are still hitting a few issues, but you can follow the progress here https://github.com/realm/realm-java/pull/7694

imknown commented 1 year ago

Android Studio official release notes of AGP 7.2.2 shows it fixed: https://developer.android.google.cn/studio/releases/gradle-plugin#7-2-0

rorbech commented 1 year ago

@Jeff11 Thanks for the notice.

Bumping to AGP 7.2.2 also fixes the issue for the reproduction project supplied in https://github.com/realm/realm-java/issues/7685#issuecomment-1144684084

pedronveloso commented 1 year ago

@Jeff11 Thanks for the notice.

Bumping to AGP 7.2.2 also fixes the issue for the reproduction project supplied in #7685 (comment)

This has worked for us as well. Unclear why that particular version of AGP didn't work. Hopefully if something will break going forward this buys Realm's team more time to work on a proper fix.

PriyaSindkar commented 1 year ago

Also, got crashes at runtime. Maybe it's related to this breaking change in AGP 7.2: https://developer.android.com/studio/releases/gradle-plugin-api-updates#instrumentatin-apis-deprecated because AGP produces such a warning message and points out to Realm plugin:

AGPBI: {"kind":"warning","text":"API 'android.registerTransform' is obsolete.\nIt will be removed in version 8.0 of the Android Gradle plugin.\nThe Transform API is removed to improve build performance. Projects that use the\nTransform API force the Android Gradle plugin to use a less optimized flow for the\nbuild that can result in large regressions in build times. It’s also difficult to\nuse the Transform API and combine it with other Gradle features; the replacement\nAPIs aim to make it easier to extend the build without introducing performance or\ncorrectness issues.\n\nThere is no single replacement for the Transform API—there are new, targeted\nAPIs for each use case. All the replacement APIs are in the\nandroidComponents {}block.\nFor more information, see https://developer.android.com/studio/releases/gradle-plugin-api-updates#transform-api.\nTo determine what is calling android.registerTransform, use -Pandroid.debug.obsoleteApi=true on the command line to display more information.","sources":[{}]}

I am getting this as warning as well when I bumped my AGP from 7.1.3 to 7.2.2 with gradle version 7.3.3. The obsolete APIs used is pointing to the realm plugin.

rorbech commented 1 year ago

Hi @PriyaSindkar. Thanks for the observation. We are in the process of migrating to the new API in #7714.