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

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.

Related

How to make Android Studio aware of source code directory for prefab-published .AAR libraries?

Using Android Studio Electric Eel, AGP 7.5, NDK r23 and prefab. In my local Maven repository, I have a precompiled .AAR with some prefab-published static libraries. I.e. the .AAR contains both .a and .h files.
In my native app (using NDK-build, not CMake), I add the debug-compiled version of my library:
dependencies {
implementation 'org.foo:mylib-debug:1.0'`
}
When debugging the app, I can indeed step through the native code, but not always:
Setting breakpoints and stepping into methods of...
The native app itself, works! E.g. app/main.cpp
Inline mylib methods in the .h files (packaged in the .AAR), works!
Non-inlined mylib methods in .c files opened in Android Studio, works!
Non-inlined mylib methods in .c files not opened in Android Studio, does not work!
It is apparent that Android Studio is not aware of the source directory for my native source code. Manually opening a source file indeed makes it aware of that particular file. So the question is:
How do you specify a known source directory for debugging a pre-compiled static library?
Ideally, I would want the mylib library project to specify this path, so that consumers don't need to remember adding some folder location (unless the path changed). This library is mainly to be used locally, so the source folder will rarely change.
What I tried so far
In tried specifying a folder in app/build.gradle, but it didn't help:
sourceSets {
main {
jni.srcDirs = ["${System.env.MYLIB_SOURCES}/src"]
}
}
I also tried setting some idea specific directory:
idea {
module {
sourceDirs += file("${System.env.MYLIB_SOURCES}/src")
}
}
Also tried adding the source directory under Debug Configuration -> Symbol Directories, but that didn't help.
In Visual Studio this whole thing would be trivial; just try stepping into a method and it will ask you for the source location if it can't find it. How hard can it be?

How do I safely change the "lib" directory name in Flutter Android Studio project?

I've created a Flutter app in Android Studio and I want to change the name of the "lib" directory (in which Dart source files reside) to "src".
However when I do that, the import to 'package:/main.dart' fails. How can I change that?
I changed the path in the .iml file, and the project compiles and runs, but this test file still shows an error.
This name is hardcoded and there is no way to change it.
The whole pub package system depends on that directory name.
There is also a convention that tools like the analyzer support code in lib/src being considered package-private when not exported by files in other directories in lib/.

Android Studio Test Report/Results Not Found

I followed the instructions here: https://developer.android.com/studio/test/index.html. I added this block to the Android block in the app-module gradle file:
testOptions {
// Changes the directory where Gradle saves test reports. By default, Gradle saves test reports
// in the path_to_your_project/module_name/build/outputs/reports/ directory.
// '$rootDir' sets the path relative to the root directory of the current project.
reportDir "$rootDir/test-reports"
// Changes the directory where Gradle saves test results. By default, Gradle saves test results
// in the path_to_your_project/module_name/build/outputs/test-results/ directory.
// '$rootDir' sets the path relative to the root directory of the current project.
resultsDir "$rootDir/test-results"
}
Yet, I couldn't find any test results/report in my project folder (I searched the whole folder and found nothing.
I am running Android test (not unit tests). The doc says that this block works for all types of tests, so... why it's not working?

Gradle complains "No such file or directory"

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.

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