I have a android project with multiple modules, module B that depends on module A
dependencies {
implementation project(':features:A')
}
I want unit test in module B to extend base test from module A. An IDE resolved all dependencies successfully, but in build time only production code from module A is accessable in test.
I tried to add dependency specificly for tests:
dependencies {
testCompile project(':features:A').sourceSets.test.output
}
This results an error:
Could not get unknown property 'test' for SourceSet container of type org.gradle.api.internal.tasks.DefaultSourceSetContainer.
Same if I'm trying to create custom artifact with tests.
How can I get access to test-specific code in base module from dependent module?
AS 3.1, gradle 4.1
The best solution I found is to create third module just for tests, put my base test classes and test dependencies there.
And use it in both module A and module B (and all other modules - just one module for all unit test dependencies)
testImplementation project(':features:basetests')
Disadvantage - it is possible to import it in non-test artifacts (e.q. implementation project(':features:basetests')). Good code review can protect from such mistakes.
Looks like there is no convenient way to define base shared test-only code.
Related
I have two separate Gradle projects. The first is an Android project and the second is a library project with multiple modules.
AppRootProject
-> app
LibraryRootProject
-> lib-feature
-> lib-core
-> lib-utils
-> lib-sample-app
Is there a clean way to set up the dependencies like so:
app depends on lib-feature
lib-feature declares API dependencies on lib-core, lib-utils and third party libraries.
LibraryRootProject is a separate repository and I would normally depend on the remote artifact. But I need to make changes and test locally to the library and test locally.
Currently the only way I can work out how to set this up locally is with the following in app's build.gradle:
implementation files('../../LibraryRootProject/lib-feature/build/outputs/aar/lib-feature-release.aar')
implementation files('../../LibraryRootProject/lib-utils/build/outputs/aar/lib-utils-release.aar')
implementation files('../../LibraryRootProject/lib-core/build/libs/core.jar')
implementation 'com.third.party.lib:1.0.0' // transitive api dependency of lib-feature
Currently I have two problems:
I need to declare all the transitive dependencies from lib-feature in app's build.gradle
I need to build the LibraryRootProject separately
As replied in a comment, you should check out composite builds that are designed exactly for that use case.
It is as simple as doing:
./gradlew --include-build path/to/LibraryRootProject <tasks>
I'm following this tutorial on how to manage Gradle dependencies with Kotlin in my Android app and now I would like to use the versions defined in my Versions object in my app module (the scenario is that I have an open source screen and I want to show the library versions). How can I do this?
I have tried adding the buildSrc directory to settings.gradle:
include ':app', ':buildSrc'
and then in my build.gradle file, adding it to the dependencies block:
dependencies {
implementation project(':buildSrc')
}
However, when I try to compile and run, the following error is thrown:
Don't know how to compute maven coordinate for artifact 'Gradle API' with component identifier of type 'class org.gradle.internal.component.local.model.OpaqueComponentIdentifier'.
and there is no much information in the web about this error.
Try to remove the buildSrc module from your settings & build.gradle. Its automatically build and accessible for all other modules in this project.
I am working on a gradle android project which has custom project structure. We used sourceSets.main apis to make mappings for "AndroidManifest.xml ", "res" and others. There are no issues with this set up and all the functionality works fine.
In the project we are planning to implement dynamic feature module. As part of project configuration I have followed all the steps mentioned in android documentation https://developer.android.com/studio/projects/dynamic-delivery#feature_build_config.
As part of instructions, one has to put, base module as a dependency of the dynamic feature module, like below
dependencies {
// Declares a dependency on the base module, ':app'.
implementation project(':app')
}
When I compile the project, build is failing with below error, ("KSApp" is my main project name and "dynamic_feature" is dynamic feature module)
"Project with path ':KSApp' could not be found in project ':dynamic_feature'."
Can some one please explain, whats going wrong and how do I put base module as my dependency in dynamic feature module ?
What I tried :
Using implementation project(${project.rootDir}") in dependencies section of dynamic feature module.
Using implementation file(${project.rootDir}") in dependencies section of dynamic feature module.
Note :
I am able to successfully implement dynamic feature module in a regular projected created in Android studio. I see problem only in project with custom project structure .
Issue is in referring base module form sub module.
As my project has custom android structure, base module is in root project folder. In this case, to refer base module from sub module one should use below approach:
dependencies {
implementation project(':')
}
This is solving the issue.
I have an Android application. It contains two modules of app and pax-lib. app module depends on pax-lib module.
I have libs folder under pax-lib that contains some jar files. I have linked them in to gradle file of this module and use it across this module without any issue. This is how I have defined them:
dependencies {
...
// Local libs not in Maven Central
implementation files('libs/commons-io-1.3.2.jar')
implementation files('libs/commons-lang3-3.2.1.jar')
implementation files('libs/httpclientandroidlib-4.3.0.jar')
implementation files('libs/Kahuna_442.jar')
implementation files('libs/mapquest-android-sdk-1.0.5.jar')
...
}
This is how I defined this dependency in gradle file of app module.
dependencies {
implementation project(':pax-lib')
...
}
I am able to use all classes I have defined in pax-lib without any issue, however, I am not able to use .jar files that have defined in Gradle file of pax-lib module. My expectation is to be able to use them as I was in Gradle version below 3.0.
I must be able to copy/paste these jar files under app module but I want to make sure I am not doing something wrong first.
Use api rather than implementation
I am using Robolectric and the "android-unit-test" plugin in my android project. I have multiple modules (eg foo and bar) and would like to use the same test runner across all the modules. To achieve this I created a Test module to hold my test runner and any custom shadow classes. In foo's build.gradle I have testCompile project (":main:Test")however android studio will not import my runner unless I also include androidTestCompile (":main:test"). Furthermore, when I run ./gradlew test from the command line I get various errors saying that my test runner and shadow classes cannot be found. Anyone have any advice?
With your given information I guess you missed one little aspect: test classes appear not in jars.
When your shared TestRunner is under src/test/java then other modules will not see it. When I'm correct then place you TestRunner at src/main/java and all modules should have access to our support classes.
Here is also an example where i did the same https://github.com/nenick/AndroidAppDevelopment