Is it possible to collect all dependencies into one place as jar-files in Gradle Android project?
I tried to use: Gradle: collect application dependencies
But Gradle returns error:
Could not find property 'testRuntime' on configuration container.
Well testRuntime is a configuration that's seems not to be available in your project. to collect all configurations in a single folder you can do something like this:
task collectJars(type: Copy){
into "$buildDir/output/lib"
configurations.all{ config ->
from config
}
}
Related
I want to add an enum called modules with the path of the sub module and some compilation types.
I used to have this in the buildSrc before gradle 6 and it was accessible in the settings.gradle
But from gradle 6.0, settings.gradle is compiled before buildSrc project. I have moved my enum to the settings.gradle, now it is not accessible to other project level gradle scripts.
The behaviour change is outlined in the below release notes.
https://docs.gradle.org/current/userguide/upgrading_version_5.html#changes_to_plugins_and_build_scripts
They suggest to add the enums / classes used in the settings.gradle to the build script closure, but I am not really sure how to do that.
https://docs.gradle.org/current/userguide/upgrading_version_5.html#plugins_and_classes_loaded_in_settings_scripts_are_visible_to_project_scripts_and_buildsrc
I've recently hit a similar issue, my company have custom code for authenticating with our Nexus which we were keeping in buildSrc. I can't turn this into a plugin since I'd need to store that in our Nexus and then would be in a catch-22 situation as I'd need to authenticate to get the authentication plugin!
I can see 2 potential workarounds for this:
Publicly published jar.
Build your custom classes as a separate jar, or a Gradle plugin if this fits the use case. Publish the jar to a maven repository that you can access from settings.gradle buildscript (for me this is difficult as it's sensitive company specific code).
This might look something like the following in your settings.gradle:
include "project-name"
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.companyname:gradle-utils:0.0.1'
}
}
Commit the binary
This isn't a desirable option but you can manage the source code for your custom buildSrc classes in another repository, then every time you make a change to them (hopefully infrequently) - you can build the new version and commit the built jar into the repositories that need to use it (perhaps under buildSrc). If your custom code has dependencies that aren't naturally on the classpath when gradle runs, you'd need to package these into the jar that you commit as well.
Your settings.gradle might then look like:
include "project-name"
buildscript {
dependencies {
classpath files("buildSrc/gradle-utils.jar")
}
}
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 was experimenting with my project's build.gradle. Currently my project consist of several modules, each of these module have common dependencies, such as android support or network library. I'm Experimenting with gradle dependencies.
I've declared a group of dependencies named lib_mandatory() in file lib-group.gradle, but when I tried to include it in my app's build.gradle the gradle sync failed.
Error:Could not find method lib_mandatory() for arguments [] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
How can I fix this? or any hints about what this error means?
Update:
Here's the lib-group.gradle
def dependencyGroup(Closure closure) {
closure.delegate = dependencies
return closure
}
def lib_mandatory = dependencyGroup{
implementation libraries.rxjava
implementation libraries.rxandroid
}
and here's the app's build.gradle
apply from: '../lib-group.gradle'
dependencies {
lib_mandatory()
}
The approach that is used in that article is a bit different, the article used the function inside the lib-group.gradle and only applied (apply from ...) the lib-group.gradle in the app's build.gradle.
In your approach, you're trying to use the function in the app's build.gradle.
If you want your functions to be accessed from other files, you should use ext instead of def. You might want to read this:
Gradle def vs ext
I have 2 project modules: A, B and a library module. Both projects using a library. I also have some connected tests in both projects (UI Automator). That tests have common utility code that I'd like to move to the library. How can I do it?
What I've tried. First way:
Copy common code to library project to src/androidTest/java/xxx
folder
Add to library build.gradle UI automator dependencies under
androidTestCompile
Add to project build.gradle following:
dependecies {
...
androidTestCompile project(path: ':library', configuration: "debug")
...
}
That cause build issue in project because as I understand src/androidTest folder is not used during building debug configuration
Second way:
First 2 steps are same
Add to library build.gradle
task connectedTestsJars(type: Jar, dependsOn: "assembleXXXAndroidTest") {
classifier = 'connectedTests'
includes = ['com/**']
from "$buildDir/intermediates/classes/androidTest/XXX"
}
configurations {
connectedTestArtifact
}
artifacts {
connectedTestArtifact connectedTestsJars
}
Add to project build.gradle
dependencies {
...
androidTestCompile project(path: ':library', configuration: "connectedTestArtifact")
...
}
That way all compiles fine. It crashes during connected test runtime because of missing resources from library (it has a lot of common code and resources from both projects)
As I understand that happens because I am using only sources from the library, but I need to use aar file instead (aar with adding src/androidTest contents?).
What will be task type in that case? Or what actions I should make in my custom task to get aar file as result? Other way?
I understand that I can move connected tests common code to another (second) library module and use, but I'd like to avoid it to save compilation time.
Is there any way to keep current module structure and move connected tests common code to library module?
I did find a solution. Add to project's build.gradle:
android {
sourceSets {
androidTest {
java.srcDirs = ['../<LIBRARY_MODULE_NAME>/src/androidTest/java', 'src/androidTest/java']
}
}
...
}
I am sure there is a better solution since the common code will be compilated in both projects. Who knows it?
I have an android project with two modules. Both modules depend on android support library. Currently the support library dependency is added to gradle scripts of both modules.
When I upgrade support library version, I have to upgrade the version in both gradle files. I may sometimes forget to do this in both files. So I need a way to declare the support version in a one common place.
This page describes one approach, where you declare the dependencies in a separate file and include it in the root gradle file.
This approach worked for me, but there are some limitations. For an example, if a new support library is available, in previous approach, android studio gave me an inspection warning that a newer version is available. With new approach, android studio no longer does that. Also, whenever you make a change in a gradle file, android studio asks for re-sync the project. But, if I made change to the separated gradle dependency file, android studio doesn't ask me to re-sync.
I tried to directly add the support dependencies to the dependencies section of the root gradle file like given below, but android studio gives me a warning.
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
compile "com.android.support:appcompat-v7:23.2.1"
}
}
Warning:
Fix plugin version and sync project.
Does anyone know any other ways which I can declare dependencies in a central place?
The easiest thing to do is declare an extension variable in your buildscript block that manages the entire build. Let's take a Kotlin project, for example, where the version of kotlin must be consistent between all the build components. Here's a top-level build.gradle that defines the buildscript:
buildscript {
ext.kotlin_version = '1.0.1'
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Notice that it defines ext.kotlin_version, then uses it below in the plugin dependency.
Then, in the main app module:
dependencies {
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:design:23.2.1'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
I can use the variable again to use the same version of the kotlin stdlib compile dependency. But I only had to define the version string in one place. ext is a special gradle way of declaring extra properties for gradle domain objects. Defining one in buildscript makes it visible elsewhere for use.