Very baby step question: Gradle task from the docs...
# gradle
v
Very baby step question: Gradle task from the docs:
Copy code
tasks.register("hello") {
    doLast {
        println("Hello world!")
    }
}
I copy-paste that on top of my shared/build.gradle.kts. It never runs, I never see doLast/doFirst. What I am missing?
c
It will run when you ask it to 🙂
./gradlew hello
v
Yes it does. I thought it will run anytime I build the project
c
No, not unless you create dependencies. For example, if you want this to be part of the
build
lifecycle task you’d add a dependency on
hello
there.
v
Seems like I need to additionally somehow add it to the build chain. I thought that's for the "register" method
c
register
registers a new task. You can explicitly create dependencies (possible, but discouraged) or wire task inputs as outputs from other tasks to create a dependency graph.
v
I don't actually get. But that sounds like it is not that simple to add the hello task to the build lifecycle.
Is it a bad thing to do or what is happening? Can't even find info how I would do that
v
Actually, the question is off-topic, as you can find in the channel topic. 😉
It is not hard to get the task into the build lifecycle, the question is just where you want it to be. As that task implementation is surely not what your task ought to do in the end.
v
Yea, I thought KMP somehow blocks my task from execution. I had no clue that .register() not adding the task to the build. There are no mention of that in the gradle docs. Now I see that I just missed a fact that I need to somehow add my tasks into the chain
v
A typical task has inputs and outputs defined. And when you wire the task or part of its outputs to the input of another task, your task is automatically executed where necessary.
v
Well my task is copy-task, which copies resources from FolderX to main depends on X from params
v
If the task is really just a "print something" task, and you want it executed if you execute the
build
lifecycle task, you could just add a dependency from one to the other
But that is unlikely 😄
Well my task is copy-task, which copies resources from FolderX to main depends on X from params
To start with a copy task is seldomly what you want, usually you want a sync task. Then, if the target of your copy task is the output of another task, then you should definitely never ever ever even try to do that as you disturb up-to-date checks, caching, and might even get failed builds due to "missing" dependencies with Gradle 8+
You most probably just want to configure the
processResources
task to include the resources you want to have additionally.
Or no, actually you most probably want to configure the
main
sourceSet
with your additional resources like
Copy code
sourceSets {
    main {
        resources {
            srcDir("...")
        }
    }
}
or similar, depends on the exact details of the situation
v
Ha. I didn't even think yet into that direction
v
Gradle is not so much like Ant. In Ant you configured exactly what to do. In Gradle you should more model the situation and let Gradle do its magic. 🙂
v
We previously just had a bash script which was doing the job. I wanted to re-do this with gradle
👌 1
Wait. I don't even need my copy gradle task what. I just need to tell sourceSets where are the source files depends on X
what
That can't be that easy
v
That's Gradle 😄
v
I've spend 2 days reading gradle docs for starters. 🤦‍♂️
I almost started to implement my plugin
v
Yeah, the docs can be a bit overwhelming when starting out 😞
v
The problem without a copy is that iOS projects points to some of the files in the X source set via direct path aka
../shared/src/main
and because I haven't copied the resources, iOS project will not know (I assume) that the resources dir added for the main. I can't believe that gradle's magic is that strong
v
Well, I'm not familiar with iOS, but it sounds like you should not have such a direct path. 🤷‍♂️
v
And "main" source set is android thing, not KMP. So if I set
commonMain { resources.srcDir(customXdir) }
android can't see those resources even tho androd {} points to common:
sourceSets["main"].res.srcDirs("src/commonMain/res")
Copy code
it sounds like you should not have such a direct path
Likely yes. When we jumped into KMP our iOS developers ran. So we had to improvise. The initial POC idea was to have "everything possible into KMP" module. Both iOS icons folder and android icons folder. Then we natively per platform pointed to that folder. And now I want to remove that 2 native implementations and trying to implement KMP only way. It is "build variants/flavors" topic. There are like 10 different things: icons, app name, firebase json for both platforms, applinks associations etc
Pain
I thought it would be smart ass idea to just copy X folder into Main on build time
v
I'm not too familiar with KMP and practically not familiar with AGP or iOS, sorry. Maybe someone else can help that is more familiar with such a setup.
v
Na. This topic is doomed yet. Not even in the roadmap for JB. And I am working with a project which is heavily coupled with app variants
But ty for help anyway. Appreciate it
👌 1