Is it possible to ignore/exclude specifics modules...
# general
l
Is it possible to ignore/exclude specifics modules of a build when using
includeBuild()
? keyword: Composite Builds. I have the case where Project A (:app -> :anotherLibrary) -> Project B, Project B -> Project A (:library)
Copy code
Project A modules:
:library
:app
Project B modules:
:anotherLibrary
There is also a common module which is used in both projects and this causes to gradle build to fail with an error:
Failed to get Gradle name for :common
Any ideas?
v
This should usually not be necessary. Gradle only builds what is needed and avoids unnecessary work as usual. So even if
a1
from
A
depends on
b
from
B
and
b
depends on
a2
from
A
, there should be no problem unless you introduce an actual cycle like
a1
needs
b1
which in turn needs
a1
, that would be a problem of course. I think I've never seen "Failed to get Gradle name", is that really the error message? Can you provide an MCVE?
l
Thanks for the explanation. Yes thats the only message I get. The error occurs when I sync Project B. What does MCVE mean? I guess there is a cycle but I don't know how I could make it work. Edit: I don't have an example to show. Hard to provide I guess.
The stacktrace
Giving Project A, B, C, D a name:
Copy code
protocol
:app
:lib

data
:lib

common-android
:mylib

common-core
:lib
Dependencies:
Copy code
settings.gradle.kts (protocol)
rootProject.name = "protocol"
include(":app")
include(":lib")
includeBuild("../data")
includeBuild("../common-android") // this is causing the fail: Failed to get Gradle name for :mylib

settings.gradle.kts (data)
rootProject.name = "data"
include(":lib")
includeBuild("../protocol")

settings.gradle.kts (common-android)
rootProject.name = "common-android"
include(":mylib")
includeBuild("../common-core")
v
MCVE: https://stackoverflow.com/help/mcve Regarding the error, as you can see from the stacktrace you sent, this is some error within IntelliJ code. In such situations, first try to execute the build from the commandline. If there is a problem, fix it and maybe the IDE problem goes away too. If there is no problem from commandline, but it works as expected, and you still get an error from IDE, report it to JetBrains at youtrack.jetbrains.com.
Also, what you provided works fine here. I created the files you mentioned with the given content and opened the
protocol
project in IntelliJ. Sync worked fine and has all 4 projects present.
l
Stupid question but what exactly should I execute from commandline? Just
bash gradlew
? Or is there a task for the sync process which I could execute?
v
You cannot trigger the "sync process" the IDE uses
It injects custom things like own plugins and other configuration using init scripts when calling Gradle and uses special sync endpoints via the tooling api that you cannot reach through a "normal" task.
I meant just triggering a normal build
l
@Vampire ok I guess I found the problem. While the IDE build runs properly the command line build execution of the
common-android
project fails with following error:
Copy code
* What went wrong:
Execution failed for task ':mylib:compileDebugJavaWithJavac'.
> Could not resolve all files for configuration ':mylib:androidJdkImage'.
   > Failed to transform core-for-system-modules.jar to match attributes {artifactType=_internal_android_jdk_image, org.gradle.libraryelements=jar, org.gradle.usage=java-runtime}.
      > Execution failed for JdkImageTransform: /home/user/dev/android/sdk/platforms/android-32/core-for-system-modules.jar.
         > jlink executable /usr/lib/jvm/java-17-openjdk/bin/jlink does not exist.
I can't find this relation. I have removed the .idea folder but with no luck. Do you know whats the problem is? EDIT: I'm using the embedded JDK for all projects.
v
What "relation"? Why does your Java have no jlink executable?
l
I don't know, I see this error for the first time. I migrated from Windows 11 to Linux 3 weeks ago. So I'm not familiar with Android under Linux. I had no problems until now. Do you know how to solve this? With "relation" I meant I'm not using JDK 17 (anymore) and obvisouly this file is referecing this JDK version
v
I'm not too familiar with Android development at all. If you execute gradle from commandline, it uses the value of JAVA_HOME or if not present the java in the PATH for executing gradle. From IntelliJ or AS it depends on what you have configured in the settings. Which tool the build then uses to do actions depends on the build and plugins used. If you for example use Java Toolchains features, that should dictate which Java to use to run tools. But whatever plugin is calling
jlink
also might not adhere to that, but use the Java you run Gradle with, I don't know. No core Gradle code I know of uses
jlink
.
❤️ 1
l
Meanwhile I got everything right. command line execution (gradlew build) for every project runs without errors but the sync in project data still throws this error. I guess I have to file a bug. Thanks for the good explanation!
👌 1
@Vampire I didn't saw this:
Also, what you provided works fine here.
Sync worked fine and has all 4 projects present.
So that matter didn't let me rest. I have prepared a MCVE. I also tried it under Windows 11 but with no luck, same error message. Could you please have a look into this.
v
I cannot open that project. I'm not using AS, but IJ and the AGP you have in there is not compatible with latest IJ as Google did not yet send latest Android Plugin changes upstream
l
It's strange. When I replace the android library module of common-android with a kotlin library, it works. I have filed an issue: https://youtrack.jetbrains.com/issue/IDEA-291248
👌 1
@Vampire Quite frustrating because it will take weeks until there is some progress I guess. Are you aware of a workaround in the meantime? The error happens in
data
project when
protocol
includes
common-android
and
data
in turn includes protocol. Isn't it possible to only include protocol without its composite builds so that sync does not even try to load
common-android
?
Copy code
settings.gradle.kts (data)
rootProject.name = "data"
include(":lib")
includeBuild("../protocol") // somehow load only protocol without its composite builds
What would you do?
v
As a diiirty hack you could check for system property
idea.sync.active
and only include the build if it is not set to
true
l
Interesting approach but the dependencies for the builds cannot be resolved:
Copy code
data settings.gradle.kts
if (System.getProperty("idea.sync.active") != "true") {
    includeBuild("../protocol")
    includeBuild("../common-core")
}

data:lib build.gradle.kts
dependencies {
    implementation("protocol:lib")
    implementation("common-core:lib")
}
Failed to resolve: protocol:lib
Failed to resolve: common-core:lib
In turn I also cannot access the code base of protocol. The command line build runs successfully.
v
Of course, you excluded it as you asked for 😄
Another work-around would be to publish to some local repository and consume from there instead of using a composite build
Maven-style that is
l
@Vampire Ah ok I thought this will allow me to pass the sync but still provide the builds in the background. To publish it locally is what I wanted to prevent. But there is another easier solution. I have copied the :lib module folder from data to protocol. Temporarily this is a sufficient solution for me. I encountered another "bug" I guess. Did you ever saw something like this:
* What went wrong:
Execution failed for task 'applintAnalyzeDebug'.
> java.lang.NullPointerException (no error message)
It occurs when I execute
gradlew build
on command line in a fresh android project while
common-android
is included + its nested build is set as dependency (common-core), i.e.
Copy code
settings.gradle.kts

includeBuild("../dccommon-android")

:app build.gradle.kts

dependencies {
    implementation("common-core:common") // error occurs when this dep is set. It's a nested composite build: common-android includes common-core
    implementation("common-android:common") // this works, no lint error on build
}
What do you think? Might this be a bug?
v
No, never got that error, because as I mentioned, I'm not really into Android development or building. Opinions differ, but in my opinion any
NullPointerException
that happens is a bug. Either that the used thing is
null
, or that the caller does not check for
null
. So imho you should anyway report it to Google.
l
ok cool I will report this. I slightly understand that most problems come from AGP, AS or IntelliJ and not Gradle per se. Thanks for your patience!