Can Gradle C++ dependencies get set differently for different applications? - gradle

I have some applications in the same project that have the same dependencies that are also being built, some of which need them as static and others need them shared. I built a very basic show of this error based off of the answer at https://github.com/gradle/gradle-native/issues/1017 and it's getting practically the same error. It's a basic project with 2 libraries, :1 and :2, and a cpp-application at :app. This spits out the following error:
Could not determine the dependencies of task ':app:installDebug'.
> Could not resolve all task dependencies for configuration ':app:nativeRuntimeDebug'.
> Could not resolve project :1.
Required by:
project :app
> Module 'gradletest:1' has been rejected:
Cannot select module with conflict on capability 'gradletest:1:unspecified' also provided by [gradletest:1:unspecified(debugSharedRuntimeElements), gradletest:1:unspecified(debugStaticRuntimeElements)]
> Could not resolve project :1.
Required by:
project :app > project :2
> Module 'gradletest:1' has been rejected:
Cannot select module with conflict on capability 'gradletest:1:unspecified' also provided by [gradletest:1:unspecified(debugSharedRuntimeElements), gradletest:1:unspecified(debugStaticRuntimeElements)]
Here's my sample that I got to break this. I'm pretty sure I followed everything the answer said in github, and have perused the gradle docs for everything I can find that may be relevant. In my current, non-MVP situation I would have an :app2 that would have :1 and :2 linked as shared libraries, so just setting :1 and :2 to be static will not fix the issue unfortunately.
1/build.gradle.kts
plugins {
`cpp-library`
`cpp-unit-test`
}
library {
linkage.set(listOf(Linkage.STATIC, Linkage.SHARED))
}
2/build.gradle.kts
plugins {
`cpp-library`
`cpp-unit-test`
}
library {
linkage.set(listOf(Linkage.STATIC, Linkage.SHARED))
dependencies {
implementation(project(":1"))
}
}
app/build.gradle.kts
plugins {
`cpp-application`
`cpp-unit-test`
}
dependencies {
implementation(project(":1")) {
attributes { attribute(Attribute.of("org.gradle.native.linkage", Linkage::class.java), Linkage.STATIC) }
}
implementation(project(":2")) {
attributes { attribute(Attribute.of("org.gradle.native.linkage", Linkage::class.java), Linkage.STATIC) }
}
}
application {
}
This was done on Gradle 5.4.1, and unfortunately it is incredibly hard to get updates so I was unable to test to see if it's the old version that's the problem.

Related

Gradle Task to check if configured dependencies are available

I have a project that builds into an SDK library.
Before building a release I would like to verify that all configured dependencies are available using a gradle task. Somehow current build operations pass, because it manages to resolve a different version of a misconfigured dependency as it gets used by some other library. However, this is not a guaranteed situation and therefor I'd like a task that verifies if all configured dependencies are actually available or not and otherwise, fail.
I started a Task like this:
abstract class CheckDependenciesTask : DefaultTask() {
#TaskAction
fun checkDependencies() {
project.configurations.forEach { config ->
if (config.isCanBeResolved) {
println("Resolving ${config.name}")
config.resolve()
} else {
println("Not resolving ${config.name}")
}
}
}
}
But that really aims to resolve configs, rather than dependencies. Somehow I'm not able to figure out how to check if dependencies are available. When running lintAnalyzeDebug --info it neatly prints that they are missing, so there must be some command that checks this, but then just fails instead of try to resolve it.
Any idea or pointers on how to achieve this would be appreciated. Thank you.

Caused by groovy.lang.MissingPropertyException Could not get unknown property 'android' for project ':lib1' of type org.gradle.api.Project

To solve my problem here: Applying JaCoCo to all Android Studio gradle modules, I applied the solution here. This works fine so far for modules with
plugins {
id("com.android.library")
}
As soon as I add the required apply from: '../jacoco/modules.gradle' into a module labeled as a Java library
plugins {
id("java-library")
}
I get a
Caused by: groovy.lang.MissingPropertyException: Could not get unknown property 'android' for project ':lib1' of type org.gradle.api.Project.
I would like to add the apply from to the java library, so that JaCoCo reports were generated for them as well. What am I missin here?
I assume you have also applied the snippet from the “Improvements” section of the solution you have linked to your ../jacoco/modules.gradle file? In that case you could replace that snippet with the following:
project.afterEvaluate {
if (project.pluginManager.hasPlugin('com.android.library')) {
android.libraryVariants.all { variant ->
tasks.create(…)
}
} else {
tasks.create(…)
}
}
If that doesn’t solve it, then I’d recommend to run the build with Gradle’s --stacktrace option. That should give you more details on where exactly the missing property was found.
Without more information on your exact build setup it’s hard to really say more.

Unable to resolve a plugin using the new plugin mechanism in Gradle

While trying to upgrade some of our scripts to Gradle 4.0.1 on of the plugins we are using is failing and I thought of fixing that plugin first. The plugin is a third party open source project.
So I have cloned the project and tried to compile it. However it fails with following message:
c:\source\gradle-xld-plugin>gradlew build
FAILURE: Build failed with an exception.
* Where:
Build file 'C:\source\gradle-xld-plugin\build.gradle' line: 2
* What went wrong:
Plugin [id: 'com.gradle.plugin-publish', version: '0.9.7'] was not found in
any of the following sources:
- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- maven(https://artifactory/java-v) (Could not resolve plugin artifact 'com.gradle.plugin-publish:com.gradle.plugin-publish.gradle.plugin:0.9.7')
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --
debug option to get more log output.
BUILD FAILED in 0s
The build.gradle script for the plugin starts like this:
plugins {
id "com.gradle.plugin-publish" version "0.9.7"
id "com.github.hierynomus.license" version "0.11.0"
id 'nebula.nebula-release' version '4.0.1'
id "com.jfrog.bintray" version "1.7.3"
}
In addition to this the company policy dictates we have to go through an internal artifactory server, so following has been added to the settings.gradle file:
pluginManagement {
repositories {
maven {
url "https://artifactory/java-v"
}
}
}
The jar file exists at following location: https://artifactory/java-v/com/gradle/publish/plugin-publish-plugin/0.9.7/plugin-publish-plugin-0.9.7.jar
but when I look at the error message I am a little puzzled that it says that it cannot find com.gradle.plugin-publish:com.gradle.plugin-publish.gradle.plugin:0.9.7.
It seems to have suffixed the id with .gradle.plugin.
Does anyone know whether I am looking at the wrong location or how come it is suffixing the id with .gradle.plugin. And shouldn't it look at a location that has the GAV like this: com.gradle.plugin-publish:com.gradle.plugin-publish:0.9.7?
And does anyone know about how the resolution mechanism for the new plugin mechanism in Gradle works.
Thanks in advance
Edit
Thanks to Mateusz Chrzaszcz I was able to progress.
The only caveat I have with the solution is that it seems like a workaround rather than a solution. But it works!
In addition to his solution you had to resolve the plugins. I was able to hack my way to actually resolve the appropriate names.
In order to do so one has to do as follows:
In a webbrowser go for the plugin: id "com.github.hierynomus.license" version "0.11.0" go to following URL: https://plugins.gradle.org/api/gradle/4.0.1/plugin/use/com.github.hierynomus.license/0.11.0
The json returned contains the GAV needed in the useModule call. Use that
The following serves as an example:
resolutionStrategy {
eachPlugin {
if (requested.id.namespace == 'com.gradle' && requested.id.name == 'plugin-publish') {
useModule('com.gradle.publish:plugin-publish-plugin:0.9.7')
} else if(requested.id.namespace == 'com.github.hierynomus' && requested.id.name == 'license') {
useModule('nl.javadude.gradle.plugins:license-gradle-plugin:0.11.0')
}
}
}
Try to implement Plugin Resolution Rules.
According to gradle documentation:
Plugin resolution rules allow you to modify plugin requests made in plugins {} blocks, e.g. changing the requested version or explicitly specifying the implementation artifact coordinates.
To add resolution rules, use the resolutionStrategy {} inside the pluginManagement {} block
like that:
pluginManagement {
resolutionStrategy {
eachPlugin {
if (requested.id.namespace == 'com.gradle.plugin-publish') {
useModule('com.gradle.plugin-publish:0.9.7') //try a few combinations
}
}
}
repositories {
maven {
url 'https://artifactory/java-v'
}
}
}
Keep in mind this is incubating feature though.

Changing configuration with the gretty plugin?

I haven't done anything with Gradle for a while, so it appears I've forgotten how configuration resolution works.
I'm trying to use the gretty plugin (instead of core, deprecated jetty), but I cannot seem to create a custom configuration.
I've boiled it down to a very short, simple script (using Gradle 3.4):
buildscript {
repositories {
maven {
url 'https://plugins.gradle.org/m2/'
}
}
dependencies {
classpath 'org.akhikhl.gretty:gretty:1.4.0'
}
}
plugins {
id 'org.akhikhl.gretty' version '1.4.0'
}
configurations {
fooTest
}
configurations.fooTest.each {
println it.toString()
}
It seems to not like me iterating over the fooTest configuration.
Assuming I need to know the dependencies for that configuration (I stripped that part from the code above)
What am I doing wrong here?
The script above gives me this:
org.gradle.api.InvalidUserDataException: Cannot change strategy of configuration ':fooTest' after it has been resolved.
The key point here was that I needed an unresolved configuration to loop over. Admittedly this information was neglected in the initial description as I didn't know it was critical information. We needed to loop over the files in the dependency and copy/unzip them into certain locations.
However, we cannot do that with a resolved configuration. That said, we can copy the configuration into a unresolved one, and loop over that instead:
configurations.fooTest.copy().each {
println it.toString()
}
This will successfully print out the files involved in the dependency (or unzip them, as my case needs).

Android Studio 2.2's incremental compiler can't see generated protobufs

I upgraded my stable version of Android Studio to 2.2 and now the IDE's "incremental compiler" can't find any of the symbols for generated protobuf classes. I open the project and it can build and deploy the app to a device just fine. But when I open a Java class file that contains generated protobuf references, Android Studio marks them as errors soon after the file opens. Every Java import of a generated proto class is marked with "Cannot resolve symbol".
I first noticed this a month ago on the canary channel but didn't think anything of it because I was floundering with other protobuf issues (upgrading to 3.0.0 with its javalite split). I forgot about it until today. It's still possible to work on the project, it's just that the IDE is near useless since it thinks there are errors (even though real compiles are fine with it).
For reference.
gradle-2.14.1
com.android.tools.build:gradle:2.2.0
com.google.protobuf:protobuf-gradle-plugin:0.8.0
com.google.protobuf:protobuf-lite:3.0.0
com.google.protobuf:protoc:3.0.0
com.google.protobuf:protoc-gen-javalite:3.0.0
And in the modules that contain .proto files:
protobuf {
protoc {
artifact = google_protoc_artifact
}
plugins {
javalite {
artifact = google_protoc_javalite_artifact
}
}
generateProtoTasks {
all().each { task ->
task.builtins {
remove java
}
task.plugins {
javalite { }
}
}
}
}
We had the same issue and found out the following:
1) In order for idea (studio) to see your source, you need to help it by adding the idea plugin to your module:
apply plugin: 'idea'
idea {
module {
// Use "${protobuf.generatedFilesBaseDir}/main/javalite" for LITE_RUNTIME protos
sourceDirs += file("${protobuf.generatedFilesBaseDir}/main/java");
}
}
2) Another bug, Android Studio seems to ignore any source directory under build/. You have to move your generated directory outside of build:
protobuf {
generatedFilesBaseDir = "$projectDir/src/generated"
}
These two changes fixed the problem introduced by Android Studio 2.2 for us.
In my case, I was using the kotlin protobuf plugin and to fix the error of the IDE not being able to resolve it. I tweaked the other answer above to point to the main folder.
// build.gradle.kts
plugins {
idea
}
idea {
module {
// explicitly tell intellij where to resolve generated proto files
sourceDirs.plusAssign(file("build/generated/source/proto/main/"))
}
}

Resources