Shadow
Shadow

Reputation: 4782

How to Run Preview for compose functions without building the entire APK in :app module

I have a multi module project, and any previews in the :compose module run fast, but when I try the exact same thing in the :app module then it will always build the entire APK even if the compose is a very basic text function:

:compose module

@Composable
fun MyComposable() {
    Text("this runs fast in the compose module")
}

@Preview
@Composable
fun MyComposablePreview() {
    MyComposable()
}

:app module

@Composable
fun MyComposableScreen() {
    Text("this should run fast in the app module as well, but instead the entire app APK is being generated every time Run Preview is  pressed")
}

@Preview
@Composable
fun MyComposableScreenPreview() {
    MyComposableScreen()
}

If I hit the "Run Preview" button in the Android Studio Preview window

enter image description here

for the MyComposablePreview then it only runs that preview on its own without any other dependency just fine, meaning nothing else other than that preview will be executed.

However, if I do the same thing in my :app module with MyComposableScreenPreview then it builds the entire app APK along with the preview screen itself.

Every time this is run, it always takes the same time it takes to build the entire app APK, which can take multiple minutes per each "Run Preview", or longer if the app is bigger. Running them in the :compose module only takes 2 seconds at most.

This is a big problem because the app has also a lot of intricate logic prior to entering it (screen routing, login stage, server configuration stage, etc.) rendering the practicality of the compose preview mentioned above mostly useless given that if the right setup/configuration does not exist then the preview does not get displayed or is re-routed to a different screen.

I tried to find a way to NOT build the entire APK when running compose previews in the :app module, but I could not find anything so far.

Is there any way that I configure it to not build the entire APK when running compose previews in the :app module?

NOTE: Simply not running previews in the :app module would not be a viable solution, since that was already achieved (like I mentioned above). This is a requirement that allows me to run previews with logic that only exists in the :app module and not in the :compose module.

Upvotes: 0

Views: 108

Answers (1)

dmortal
dmortal

Reputation: 599

It seems to me that all screens can be in a separate module, for example authentication modules, settings modules and others which consist of an implementation and an interface, the implementation will contain screens, dependency injection, business logic and the interface will provide the ability to run the required functionality. Other modules as needed will depend only on the interface module. As a result, there will be no screens in the :app module.

Upvotes: 1

Related Questions