I am still very gradle noob so basic principles are eluding me. After I discovered there was nothing in the supplied framework to handle native library declarations in java, I read the docs and forums and found code to do it. I then abstracted a method, resulting in:
def nativeLibraryTask(File nativeSubDir) {
logger.info('nativeLibraryTask: nativeSubDir={} exists={}', nativeSubDir, nativeSubDir.exists())
def parentDir = nativeSubDir.getParent()
repositories {
flatDir {
dirs parentDir
}
}
dependencies {
implementation files(nativeSubDir)
}
eclipse {
classpath {
file {
whenMerged {
def nativeLib = entries.find { it.path.contains nativeSubDir.getName() }
nativeLib.nativeLibraryLocation = nativeSubDir
}
}
}
}
}
//todo this works but only when the nativeLibraryTask is defined in the same file
dependencies {
//todo using project.rootDir.parent because I could not make relative paths between projects work
nativeLibraryTask(new File(project.rootDir.parent , '<other-project>/<native-lib>'))
}
I would now like to move this method to the master build.gradle so it is available to all sibling projects. However when I do that, the classpath reference is not created. I presume this is a relative file reference issue but have not figured it out yet.
Correction: Logging tells me that the relative file reference is working (the file exists). So here I would like to know how to create a relative path more elegantly using $rootDir or the project reference. But my bigger problem is that the code inside the method is not creating the classpath reference.
Apologies for thinking I was ready to share the problem but then finding out more with further investigation. Once some logging issues were fixed, I got further.
The string escaping problem was solved by learning the difference between "{foo}" and '{foo}’.
$rootDir refers to the master project and not the current project (the one calling the method). So "$rootDir/<other-project>/<native-lib>"
becomes "<absolute-path-to-parent-folder>/master/<other-project>/<native-lib>"
instead of the correct "<absolute-path-to-parent-folder>/<other-project>/<native-lib>"
. (“master” should not be in the path.)
The problem with moving nativeLibraryTask to the master build.gradle is that it operates on the master project instead of the current project. So it adds the native path to the master project instead of the current project. I did not expect that. So now I need to change the code to either pass the current project to nativeLibraryTask as an argument or figure out how to make a task inside a method refer to the calling project instead of the project of the file it is defined in.
The following works. In the master build.gradle file:
def nativeLibraryTask(Project callingProject, String relPath) {
File nativeSubDir = new File(callingProject.rootDir.parent, relPath)
if (!nativeSubDir.exists()) {
logger.error('nativeLibraryTask: nativeSubDir={} does not exist', nativeSubDir)
return
}
def parentDir = nativeSubDir.getParent()
callingProject.repositories {
flatDir {
dir parentDir
}
}
callingProject.dependencies {
implementation files(nativeSubDir)
}
callingProject.eclipse {
classpath {
file {
whenMerged {
def nativeLib = entries.find { it.path.contains nativeSubDir.getName() }
nativeLib.nativeLibraryLocation = nativeSubDir
}
}
}
}
}
And in the subproject build.gradle file:
dependencies {
nativeLibraryTask(project, '<other-project>/<native-lib>')
}
Any improvements welcome.