Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Android preview to android-ios compose template #3499

Open
gastsail opened this issue Aug 15, 2023 · 28 comments
Open

Add Android preview to android-ios compose template #3499

gastsail opened this issue Aug 15, 2023 · 28 comments
Assignees
Labels

Comments

@gastsail
Copy link

gastsail commented Aug 15, 2023

Describe the bug
I'm trying to preview my composables on the android side to see what I'm doing before compiling.

I have added the preview tooling lib to the androidMain

Screen Shot 2023-08-15 at 15 02 21

And then used the @Preview annotation on my androidmain package inside my shared folder

Screen Shot 2023-08-15 at 15 02 47

I cannot even see the preview icon on the top right corner to see the composable preview

This is my setup

Screen Shot 2023-08-15 at 15 15 44

Affected platforms
Select one of the platforms below:

Versions

  • Kotlin version*: 1.9.0
  • Compose Multiplatform version*: 0.6.1
  • OS version(s)* (required for Desktop and iOS issues):
  • OS architecture (x86 or arm64):
  • JDK (for desktop issues): corretto-17

Expected behavior
I should be able to see the preview

@gastsail gastsail added bug Something isn't working submitted labels Aug 15, 2023
@mazunin-v-jb
Copy link
Contributor

mazunin-v-jb commented Aug 16, 2023

Hello! Thanks for the issue.
Could you please provide info about the affected platform? We don't have a working preview on iOS target yet.
And could you check again your Compose Multiplatform version?

@mazunin-v-jb mazunin-v-jb added wait for reply Further information is requested and removed submitted labels Aug 16, 2023
@gastsail
Copy link
Author

gastsail commented Aug 16, 2023

Hey how are you thanks for your reply, compose plugin version is 0.6.1, I have also tried downgrading jdk to 11 , 16 and still the same.

Im trying only to run this on the Android side, thanks.

@mazunin-v-jb
Copy link
Contributor

Thank you for providing information. Could you please try to create a new Android project and check if it works in it?
And if so, could you please check how you connect artifacts and dependencies in androidApp/build.gradle.kts in both your projects?

@gastsail
Copy link
Author

gastsail commented Aug 16, 2023

Hey @mazunin-v-jb I have used this template https://github.com/JetBrains/compose-multiplatform-ios-android-template#readme I did not start it from scratch, surprisingly I cannot find any example that uses the @Preview annotation, is just me or for now is it having issues ?

If you go to -> https://github.com/JetBrains/compose-multiplatform/blob/master/examples/README.md in any of these examples I cannot find the @Preview annotation on the androidMain folders , I assume that all the views are visualized on runtime.

@mazunin-v-jb mazunin-v-jb changed the title @Preview does not work on JVM 17 and Kotlin compiler 1.9.0 Add Android preview to android-ios compose template Aug 17, 2023
@mazunin-v-jb mazunin-v-jb removed the wait for reply Further information is requested label Aug 17, 2023
@mazunin-v-jb mazunin-v-jb self-assigned this Aug 17, 2023
@mazunin-v-jb
Copy link
Contributor

That's correct, that template doesn't have a preview setting for Android. We're planning to fix this later.

@gastsail
Copy link
Author

Thanks a lot, is there any workaround I can do on that template to be able to use the preview ?

@mazunin-v-jb
Copy link
Contributor

mazunin-v-jb commented Aug 18, 2023

Unfortunately, I can't provide a workaround for now. We'll try to fix it in the future. Please stay tuned, we'll inform here when we fix.

@gastsail
Copy link
Author

Thanks a lot @mazunin-v-jb , I will be waiting and trying out new things, will close this for now :)

@gastsail
Copy link
Author

or should we keep it open for when an update is available ?

@gastsail gastsail reopened this Aug 18, 2023
@fzanutto
Copy link

fzanutto commented Aug 19, 2023

I added these settings inside the android block of the root build.gradle.kts

buildFeatures {
    compose = true
}
composeOptions {
    kotlinCompilerExtensionVersion = "1.5.1"
}

Besides this dependencies for androidMain

api(compose.preview)
api(compose.uiTooling)

And my previews are showing

image

Here is the repo: https://github.com/fernandozanutto/water-reminder

@gastsail
Copy link
Author

Thanks a lot @fernandozanutto will try this one and let you know 👍

@gastsail
Copy link
Author

Ok, so, I have added

composeOptions {
        kotlinCompilerExtensionVersion = "1.5.1"
    }
    buildFeatures {
        compose = true
    }

As @fernandozanutto suggested inside the :androidApp buildGradle file , then added

api(compose.preview)
api(compose.uiTooling)

Inside androidMain in the :shared build.gradle file, and now from androidApp package I can render the previews

Screenshot 2023-08-21 at 11 14 06

Btw, on gradle.properties I'm using

#Gradle
org.gradle.jvmargs=-Xmx2048M -Dkotlin.daemon.jvm.options\="-Xmx2048M"

#Kotlin
kotlin.code.style=official

#MPP
kotlin.mpp.stability.nowarn=true
kotlin.mpp.enableCInteropCommonization=true
kotlin.mpp.androidSourceSetLayoutVersion=2

#Compose
org.jetbrains.compose.experimental.uikit.enabled=true
compose.ios.resources.sync=false

#Android
android.useAndroidX=true
android.compileSdk=34
android.targetSdk=33
android.minSdk=24

#Versions
kotlin.version=1.9.0
agp.version=8.0.2
compose.version=1.5.0-beta02

@realityexpander
Copy link

This has been broken again with kotlin 1.9.10, compose 1.5.2, agp 8.0.2

@dima-avdeev-jb dima-avdeev-jb reopened this Oct 5, 2023
@dima-avdeev-jb
Copy link
Contributor

Related PR and discussion here: JetBrains/compose-multiplatform-template#18

@ariefannur
Copy link

same with @realityexpander got this error
Cannot cast androidx.compose.compiler.plugins.kotlin.ComposePluginRegistrar to org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar

@virtanAnton
Copy link

To solve ambiguous behaviour in gradle plugin and make Layout Inspector work, you may define separate gradle module for Android build.gradle(:app) and for multiplatform build.gradle (:shared) with mentioned configuration #3499 (comment)

android {
    buildFeatures {
        compose = true
    }

    composeOptions {
        kotlinCompilerExtensionVersion = extensions.getByType(ComposeExtension::class.java)
            .dependencies.compiler.auto.substringAfterLast(":")
    }

    packaging {
        resources {
            excludes += "/META-INF/{AL2.0,LGPL2.1}"
        }
    }

    dependencies {
        debugImplementation(compose.uiTooling)
    }
}

@realityexpander
Copy link

To solve ambiguous behaviour in gradle plugin and make Layout Inspector work, you may define separate gradle module for Android build.gradle(:app) and for multiplatform build.gradle (:shared) with mentioned configuration #3499 (comment)

android {
    buildFeatures {
        compose = true
    }

    composeOptions {
        kotlinCompilerExtensionVersion = extensions.getByType(ComposeExtension::class.java)
            .dependencies.compiler.auto.substringAfterLast(":")
    }

    packaging {
        resources {
            excludes += "/META-INF/{AL2.0,LGPL2.1}"
        }
    }

    dependencies {
        debugImplementation(compose.uiTooling)
    }
}

What are the versions of the plug-ins you are using for this?

I tried to apply your suggestion to my project : https://github.com/realityexpander/FredsRoadtripStoryteller.git

image

@virtanAnton
Copy link

To solve ambiguous behaviour in gradle plugin and make Layout Inspector work, you may define separate gradle module for Android build.gradle(:app) and for multiplatform build.gradle (:shared) with mentioned configuration #3499 (comment)

android {
    buildFeatures {
        compose = true
    }

    composeOptions {
        kotlinCompilerExtensionVersion = extensions.getByType(ComposeExtension::class.java)
            .dependencies.compiler.auto.substringAfterLast(":")
    }

    packaging {
        resources {
            excludes += "/META-INF/{AL2.0,LGPL2.1}"
        }
    }

    dependencies {
        debugImplementation(compose.uiTooling)
    }
}

What are the versions of the plug-ins you are using for this?

I tried to apply your suggestion to my project : https://github.com/realityexpander/FredsRoadtripStoryteller.git

image

Check resources demo project here: https://github.com/virtanAnton/compose-multiplatform/tree/local-dev
To open: open ./components/build.gradle.kts
It's use compose.version=1.6.0-dev1378

@hafiz013
Copy link

Why my ticket request feature link to this ticket which actually not answering my request which is compose preview not applicable in folder composeApp.

@brucemax
Copy link

Why my ticket request feature link to this ticket which actually not answering my request which is compose preview not applicable in folder composeApp.

Totaly agree, I haven't "Code/Splite/Design" buttons for files in composeApp folder at all.

BTW Preview feature is very important, but all their template projects just ignore it, disappointed

@dima-avdeev-jb
Copy link
Contributor

@hafiz013 @brucemax
Thanks for the information.

@hafiz013 I will answer in the original Issue

@hafiz013
Copy link

I know that preview can do inside android folder but it is wasting time double instead can focus only composeApp folder. Please make it visibile to view. Thanks in advance

@dima-avdeev-jb
Copy link
Contributor

@hafiz013 Thanks for description.
Yes, we plan to do so in commonMain - but it is really a huge task.

@gastsail
Copy link
Author

@dima-avdeev-jb take your time, you guys are doing an awesome job already porting compose to multiplatform, thanks a lot !

@brucemax
Copy link

brucemax commented Feb 15, 2024

@dima-avdeev-jb take your time, you guys are doing an awesome job already porting compose to multiplatform, thanks a lot !

Agreed. Compose and KMP is best thing happened with mobile development last 10 years 👍

@andrey2ag
Copy link

Following the thread... I would love to see the Preview issue fixed

@brucemax
Copy link

brucemax commented Mar 8, 2024

Following the thread... I would love to see the Preview issue fixed

Do you use @Preview in commonMain ? But how?
I placed implementation (compose.components.uiToolingPreview) in commonMain. Preview annotation is available but design section not appeared. Or does it workable only for Fleet?

@dev-lcc
Copy link

dev-lcc commented Mar 18, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests