h0tk3y

    h0tk3y

    3 years ago
    Hi everyone, In the latest 1.3.20 EAP, we've added a few improvements to Multiplatform Projects, including publishing Android libraries (AAR) as a part of a multiplatform library. See the details in the thread. You are welcome to try it and share your feedback!
    The EAP build is
    1.3.20-eap-100
    published to the Maven repo
    <https://kotlin.bintray.com/kotlin-eap>
    , here's how to apply it: https://discuss.kotlinlang.org/t/kotlin-1-3-20-early-access-preview/10748/10
    Publishing Android libraries is disabled by default, to enable it, specify the list of the variants that you want to publish in the scope of the Android target.
    kotlin {
        android { 
            // Or android("foo") { ... }, if you use a 
            // custom name for this target
            publishLibraryVariants("release", "debug")
        }
    }
    If you have no product flavors in the library, then it may be just the
    release
    variant. With product flavors, specify full variant names, like
    fooBarRelease
    . Publishing is only supported for library variants, not application or test variants. To publish all library variants, including the debug ones, instead call
    publishAllLibraryVariants()
    . If you publish several variants that share the product flavor and differ in the build type, like
    fooBarRelease
    and
    fooBarDebug
    , you may group them into a single publication by specifying
    publishLibraryVariantsGroupedByFlavor = true
    (false by default), then the build types become classifiers for the artifacts, and the
    release
    artifact is published with no classifier. Publishing is set up with the
    maven-publish
    plugin, which you also need to apply. The publications are created automatically, just as for other kinds of MPP targets, but you still need to setup the repositories for publishing, or publish to the local Maven repo. More details about
    maven-publish
    in the Gradle docs: https://docs.gradle.org/current/userguide/publishing_maven.html
    Another change in the 1.3.20-eap-100 is how the dependencies are written in the POMs. Since POMs are only read by the consumers who can't read Gradle module metadata, the plugin will now always rewrite dependencies in the POMs, replacing each dependency on a multiplatform library with Maven coordinates of the actual target module that the dependency resolved to. For example, in a JVM target's POM, a dependency on a multiplatform library
    foo
    will be replaced with a dependency on the foo's JVM module,
    foo-jvm
    . This will work for both project-to-project dependencies and dependencies on Maven publications. At this point, this only affects dependencies of multiplatform projects. We will likely expand this to single-platform modules in the future updates.
    Also, a minor improvement is that a
    KotlinCompilation
    now exposes
    allKotlinSourceSets
    , which is a transitive closure of the source sets included into the compilation via the
    dependsOn
    relation, so you don't need to collect them manually.
    h

    Hauke Radtki

    3 years ago
    Works like a charm, only thing I encountered is, that access to the
    publishing.publications.androidRelease
    (e.g. for publishing to something else than mavenLocal) is only available when wrapped in an
    afterEvaluate
    block, whereas the others are available immediately.
    h0tk3y

    h0tk3y

    3 years ago
    Yes, this happens because the Android Gradle plugin only creates the variants after the project is evaluated. As a simpler way to do that, you can configure the Maven publications from within the target scope, using
    mavenPublication { ... }
    . Note that the lambda will be called on each of the publications if there are more than one.
    a

    addamsson

    3 years ago
    Can I publish to Maven Central now?
    Just regular multiplatform artifacts, not Android ones
    h0tk3y

    h0tk3y

    3 years ago
    @addamsson Yes, there should be nothing to stop you. 🙂 But it will require you to do some additional configuration for the publications:1) setup signing using the plugin https://docs.gradle.org/current/userguide/signing_plugin.html 2) add Javadoc JARs to all publications (it can be an empty JAR) 3) if you publish the library with Gradle module metadata, add a sources JAR to the publication named
    kotlinMultiplatform
    4) configure the POMs Also please note that publishing with Gradle module metadata is not yet considered stable, and future Gradle versions may reject the published metadata. I would recommend doing that along with a stable publication, i.e. without metadata. Anyway, please experiment with a staging repository first.
    a

    addamsson

    3 years ago
    Is there a tutorial for this? I know it is possible but I have been banging my head against this for days then I gave up so I'm just waiting for an official guide on this (or even an example project). I think this would be really important to get multiplatform libraries off the ground. Right now I'm using Jitpack as a backup plan until this problem is solved for projects using the
    kotlin-multiplatform
    plugin.
    h0tk3y

    h0tk3y

    3 years ago
    There's no tutorial for Kotlin MPP in particular, but it should not be much different from publishing anything else with the
    maven-publish
    plugin, only that the
    kotlin-multiplatform
    plugin creates the publications automatically and instead of creating them manually, you just need to do some additional configs to the existing ones, and signing configuration will differ. As for publishing to Maven Central in general, I could find this article: https://medium.com/@nmauti/sign-and-publish-on-maven-central-a-project-with-the-new-maven-publish-gradle-plugin-22a72a4bfd4b Of course you'll need to tweak it here and there, it won't just work as-is. I'll definitely consider writing a tutorial for Kotlin MPP sometime soon.
    a

    addamsson

    3 years ago
    I've already tried this tutorial this doesn't work with
    kotlin-multiplatform
    for example this whole block doesn't work
    task sourceJar(type: Jar) {
        classifier "sources"
        from sourceSets.main.allJava
    }
    it complains about missing
    allJava
    IIRC
    I've tried every source ant tutorial which I could find but I couldn't make any of them work with
    kotlin-multiplatform
    that's why I think that a good tutorial or example project would be paramount
    Having this plugin is useless if we can't use it with Maven Central
    h

    Hauke Radtki

    3 years ago
    @addamsson is there something special with mavenCentral? I got it to work with artifactory (maven).
    h0tk3y

    h0tk3y

    3 years ago
    @Hauke Radtki Basically, Maven Central requires a lot of customizations to the publications, like sources & Javadoc JARs, signing, POMs. There's nothing beside that I suppose, but it all needs to be done a bit differently for Kotlin MPP than for an ordinary Java/Kotlin project.
    a

    addamsson

    3 years ago
    Yep, that's the case
    so right now I either use the old way which is really clunky
    or just skip maven central altogether and use jitpack
    but this is pretty lame in the long run as people expect to use maven central
    sometimes outright reject anything else for good reason (https://blog.autsoft.hu/a-confusing-dependency/)
    h

    Hauke Radtki

    3 years ago
    Maybe https://github.com/orangy/multiplatform-lib helps a bit (I think it's outdated a bit with 1.3.20-eap-100), it helped me to get my stuff running and it contains a few tasks that look like they fiddle around with javaDoc etc.
    h0tk3y

    h0tk3y

    3 years ago
    That repo is a good example for publishing to Bintray, but it misses some bits for Maven Central, too.
    a

    addamsson

    3 years ago
    yep
    so JetBrains devs push things to Maven Central
    so I think that it wouldn't be a big leap to publish a tutorial on how they do it
    what I noticed is that the official JetBrains libs use the old way (not the
    kotlin-multiplatform
    plugin) which makes me think that it is not ready for prime time otherwise they would have been dogfooding it I presume
    m

    msink

    3 years ago
    At least one official lib - https://github.com/Kotlin/kotlinx.atomicfu - uses
    kotlin-multiplatform
    . Maybe because it is relatively simple... 😒imple_smile:
    orangy

    orangy

    3 years ago
    We are migrating to mpp one library at a time. E.g. #ktor was migrated, coroutines are on the way
    a

    addamsson

    3 years ago
    great, thanks!
    I'll try this one