Android Studio Test Report/Results Not Found - android-studio

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?

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 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.

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/.

copy static files to build output folder in gradle

I was using ANT before (Android Project) and i had "static" files in the same packages as my code
Here is an example
src/com/my/app/test/Parser.java
src/com/my/app/test/json_to_parse.json
When executing the unit tests, the json file was copied into the gen folder, therfor it was possible to access the json in the test with
getClass().getResourceAsStream(fileName)
I had to convert the project to gradle, but now the tests are failing.
After checking the "build" folder, i've realised, the .json files are not there, therefor the getResourceAsStream method returns null.
Any idea how to include these "static" files (json, xml, ...) into the build folder?
Moving the files into the resources folder did not work out of the box in Android Studio (even though is should have)
This should be fixed in Android Studio 1.2.
However, this is what i did:
Moved all static files into the resources folder.
In my unit-test module i've added this to the build.gradle file
task copyTestResources(type: Copy) {
from "${projectDir}/src/test/resources"
into "${buildDir}/classes/test"
}
processTestResources.dependsOn copyTestResources
Now, all files located inside src/test/resources will be copied into /classes/test where i can access them with
getClass().getResourceAsStream(fileName)
If i keep the package structure inside the resources folder the same as it was in the java folder, i don't need to adjust any code.
To complete the story a bit more:
JUnit4 runner requires
getClass().getResourceAsStream(name)
while Robolectric requires
getClass().getClassLoader().getResourceAsStream(name)
The files you are asking about are called "resource files" in Maven/Gradle lingo.
Gradle assumes that you are using the Maven Standard Directory Layout.
So, either you move your files into src/test/resources (then Gradle will pick them up automatically), or you tell Gradle that it should look for resources in some other place.
In the latter case, you need to modify the processTestResources task. However, keeping resource files in the same directory as source code is a bad practice. So I advise the former option.
if your problem is happen when you create apk with AndroidStudio.
you can create a jar file that includes your resources with jar.exe
for example i put a.txt into resources directory
and run this code in cmd:
"C:\Program Files\Java\jdk1.7.0_79\bin\jar" cvfe res.jar -c resources
after that a jar file "res.jar" was created
then add that res.jar into libs folder in your project
when your apk is creating resources are added to your final apk and you can use this code to acsess a.txt:
someclass.class.getClassLoader().getResourceAsStream("resources/a.txt");
with this job no need to change Gradle setting.

Resources