Previous, I am building my app using
def room_version = '2.5.0'
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
Today, when I remove
annotationProcessor "androidx.room:room-compiler:$room_version"
The app is still fully build-able. My app does use @Dao, @Entity, ...
May I know, why is it so? I thought the reason we are using annotationProcessor, because we want to support @Dao, @Entity, ...?
The Room compiler annotation processor generates java code from the respective annotations (descried below). The generated code is only then invoked at run time. Hence the lack of the code does not result in a compile time issue (but a run time issue/exception).
Primarily it generates code for each
@Databaseannotated class and for each@Daoannotated interface or abstract class.The generated code is placed in the
java (generated)folder (visible from theAndroidview of the project explorer).for each
@Databaseabstract class there will be a class that is the same name as the@Databaseannotated class but suffixed with_Impl.entitiesparameter of the@Databaseannotation that defines the@Entityannotated classes that are then deemed to be the tables that Room will use.for each
@Daoannotated abstract class or interface (typically the latter) there will be a a class that is the same name as the@Daoannotated class/interface but suffixed with_ImplWithout the annotation processer for the compiler (aka removing the line), then there will be no complaint to compile time as effectively you are saying that you are not using Room.
However, if you then try to run the App (if it attempts to use Room), you will then get a run time exception stating that the
@Databaseclass suffixed with_Impldoes not exist, even though there is no compile time error.Demonstration
A project consists of an abstract class
TheDatabaseannotated with@Databaseas per:-@Entityannotated classes.Within the
TheDatabaseis also the line:-@Daoannotated interface/abstract class (the latter in this demo)) it including:-
If the build gradle (module) excludes the room compiler as per:-
Activity Code for MainActivity is
:-
Then there is no issue compiling the project:-
The Android View shows:-
However if an attempt is made to run the App then:-
TheDatabase_Impldoes not exist.Now with the Room compiler annotation processor
:-
The build works as per :-
Android View :-
If an attempt is made to run the App then:-
The app runs without an exception. The Log includes some expected output:-
App Inspection also confirms that the database exists and contains data e.g. :-
If the Room compiler annotation processor is removed, then again the code will compile but as the code is not generated (the old code being removed):-