Gradle complains "No such file or directory" - groovy

I am using Android Studio & Gradle build script.
Under the root of my Android project, there is a folder named 'other', inside 'other' folder, there is a properties file my.properties.
-MyApp/
…
-other/
-my.properties
-build.gradle
In my build.gradle, I try to access my.properties :
task accessMyProperties {
def folderName = 'other';
//Gradle complains this line
def file = new File("$folderName/my.properties");
...
}
But gradle complains that:
* What went wrong:
A problem occurred evaluating project ':MyApp'.
> other/my.properties (No such file or directory)
Why gradle arises the above error? How to get rid of it?

Be careful about using pure relative paths in Gradle build scripts; those rely on the working directory at the time the build is invoked, which isn't something you should depend on. It's much better to ground your paths against $projectDir or $rootDir. Try something like this:
def file = new File("$projectDir/$folderName/my.properties");
See http://www.gradle.org/docs/current/dsl/org.gradle.api.Project.html for a list of variables you can use.

Related

How build.gradle file in android studio deal with relative path

I am using android studio 4.0.1, I have a ndk module called app, that has a fold structure like below image shows. Now I added a task to the highlighted build.gradle file, where I put:
task conanInstall {
def buildDir = new File("src/main/cpp/native_lib.build")
buildDir.mkdirs()
...
}
As you see, the build.gradle file is in the app folder, so I specified the path to be src/main/cpp/native_lib.build which is relative to the gradle file, however, this failed and I need to use app/src/main/cpp/native_lib.build to make it work. Why is that?
In the same gradle file I saw in the android section:
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.17.0"
}
}
The path there starts with src rather than app. This confuses me on how the gradle file deals with relative path.
java.io.File resolves relative path against the current user directory. This is what the doc says:
By default the classes in the java.io package always resolve relative pathnames against the current user directory. This directory is named by the system property user.dir, and is typically the directory in which the Java virtual machine was invoked.
To resolve a path relative to current project you can use file(...):
task conanInstall {
def buildDir = file("src/main/cpp/native_lib.build")
buildDir.mkdirs()
...
}
where file() is org.gradle.api.Project#file(java.lang.Object), and (according to javadoc) it does exactly what you need:
Resolves a file path relative to the project directory of this project.

Android Studio cannot open Android project with Kotlin DSL

I have converted one of our Android projects from the old Groovy settings.gradle and build.gradle files to the new Kotlin DSL, i. e. settings.gradle.kts and build.gradle.kts.
While "it works on my machine" - in particular: the original project I converted from Groovy to Kotlin works fine in its original directory - all my co-workers are unable to open the project when they clone the repo. Importantly, neither can I open the project myself (with the same AS installation on the same machine) when I clone the repo to some other directory. So, I suspect there is some additional detail missing in some configuration file but I cannot seem to figure out which...
Details:
When I just use File > Open... and then select the project folder, I only get the error message "The project 'xxx' is not a Gradle-based project"
When I instead go through Import Project (Gradle, Eclipse ADT, etc.) and then select Import project from external model and Android Gradle Android Studio will create an empty build.gradle file and fail with the error message "ERROR: Plugin with id 'com.android.library' not found." Deleting the build.gradle just goes back to the error message I described in the first bullet point.
I am aware of this Github issue, which seems to describe the same problem, but it's been very quiet and I thought someone around here must have figured out a solution to this...
Oh, command line builds work everywhere - this is purely an Android Studio problem.
UPDATE: When I copy the whole project to a new folder (instead of cloning the repository) I can open it without any problems. So, am I correct in assuming that there must be something inside the folder - but not in Git - that makes it work?
I was able to 'fix' it by deleting the .idea directory and reopening the project. The .idea directory is usually not committed in git but I guess copying the directory invalidates the directory structures in the files within the .idea directory.
The whole bug is easily reproducible when you click on File > Re-import Gradle project.
#Boni2k answer does not work for me.
I have to rename the root build.gradle.kts back to build.gradle, fix the syntax error, sync the project (which works fine immediately), and rename the file back to build.gradle.kts. Then the error is gone and I can sync the project successfully.
What I did to raise the error was that I moved the project to a different folder, and rename the project.

Get project name of task in gradle

I have a multi-project build. Sub-projects are in tree structure (not flat).
In root build.gradle file I have a method that does some common thing for compilation phase.
I would like to have separate target dir for each subproject. So when I'm compiling whole project I will have own artifacts for each subproject.
Method I mentioned above does compilation, so it needs to know which project it compiles.
If I use "gradle :subproject1:subsubproject1:compile" command then project.name still contains root project name. But I need in runtime to know project of task that called for method.
Questions are:
Is it good idea at all to have separate targets for subprojects?
How can I understand project of task that called for method inside this method?
If the task is defined in the subproject then you can simply use project.name to get the name of the project the task is run under.
You can try it out, for example by adding the following code to build.gradle located on the root project:
allprojects {
task printProjName << {
println ">> " + project.name
}
}
Then, executing the printProjName task will result in an output similar to the one below:
:printProjName
>> Gradle
:projA:printProjName
>> projA
:projB:printProjName
>> projB
:projA:projA1:printProjName
>> projA1
:projA:projA2:printProjName
>> projA2

Error while adding mp4parser library to a project in android studio

I'm trying to use mp4parser library in my project. My android studio version is 1.0.2. Here's what I've done so far:
I've downloaded mp4parser zipfile from the link: https://github.com/sannies/mp4parser
I've extracted the zip file to MyProjectName/app/libraries
Renamed the folder to mp4parser
Add this line of code to settings.gradle file:
include ':app:libraries:mp4parser'
Add this line of code to build.gradle (in dependencies block):
compile project('libraries:mp4parser')
Now I want to sync the project with gradle files. This error pops up:
Error:Configuration with name 'default' not found.
I don't have this problem with other libraries. Seems that its only mp4parser that I have problem with. How can I fix this?
If you're including the library as source, the best thing to do is to unpack it somewhere and import it as a module.
The error you're getting is cryptic but it means that the build system is looking for a build.gradle file at that location and not finding it (or it doesn't see an apply plugin statement in the file telling it what to do). I'm assuming that the library you're trying to use doesn't have a Gradle build script.
If you import the library using the Android Studio UI, a build script will be generated for the module and you should be good to go.

Change Gradle's working directory when compiling a Groovy project

When compiling a groovy project, gradle creates files in $projectRoot/build/. Is there a way to configure this to e.g. an absolute path?
Yes, there is. You can put the following line in your build script:
buildDir = 'your_directory'
Or in gradle.properties file (no quotes around your_directory in that case).
Build directory is a project property. You can view all project properties available to you by typing:
gradle properties
You can set project.buildDir in your build.gradle, or buildDir = PATH in gradle.properties.
And from environment variable like this:
export GRADLE_OPTS=-Dorg.gradle.project.buildDir=/tmp/gradle-build
See Build Environment section of the documentation here:
https://docs.gradle.org/current/userguide/build_environment.html#sec:gradle_environment_variables
And Writing Build Scripts here (for buildDir property):
https://docs.gradle.org/current/userguide/writing_build_scripts.html
(Current version of gradle when writing this: 4.5)
You can add project.buildDir = 'your/directory' to the file build.gradle
you can place it anywhere in separate line

Resources