build.gradle dependency on gitlab (local server) - java

Note: I am not very familiar with gradle
Several projects I've worked on with gradle have a dependencies section such as the following where I used the Sugar ORM:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.2'
compile 'com.github.satyan:sugar:1.3'
}
I can compile the Sugar ORM project using:
'com.github.satyan:sugar:1.3'
I have a local server hosting a small library on GitLab which I want to include in my next gradle project.
I want to be able to do the following in my dependencies section for my new project:
dependencies {
compile 'git.devstuff.com:?:?'
}
http://git.devstuff.com/admin/broadcast_lib.git (local server) is the where my git project is, but I don't know how to fill in the question marks in the above code snippet.
The end result I would like is to include the library in a future project via the following in my build.gradle file:
dependencies {
compile 'git.devstuff.com:broadcast_lib:1.0'
}
I'll update the question if I'm lacking information.

Related

Is there a way for Gradle to resolve dependencies using curl command? [duplicate]

I have tried to add my local .jar file dependency to my build.gradle file:
apply plugin: 'java'
sourceSets {
main {
java {
srcDir 'src/model'
}
}
}
dependencies {
runtime files('libs/mnist-tools.jar', 'libs/gson-2.2.4.jar')
runtime fileTree(dir: 'libs', include: '*.jar')
}
And you can see that I added the .jar files into the referencedLibraries folder here: https://github.com/WalnutiQ/wAlnut/tree/version-2.3.1/referencedLibraries
But the problem is that when I run the command: gradle build on the command line I get the following error:
error: package com.google.gson does not exist
import com.google.gson.Gson;
Here is my entire repo: https://github.com/WalnutiQ/wAlnut/tree/version-2.3.1
According to the documentation, use a relative path for a local jar dependency as follows.
Groovy syntax:
dependencies {
implementation files('libs/something_local.jar')
}
Kotlin syntax:
dependencies {
implementation(files("libs/something_local.jar"))
}
If you really need to take that .jar from a local directory,
Add next to your module gradle (Not the app gradle file):
repositories {
flatDir {
dirs("libs")
}
}
dependencies {
implementation("gson-2.2.4")
}
However, being a standard .jar in an actual maven repository, why don't you try this?
repositories {
mavenCentral()
}
dependencies {
implementation("com.google.code.gson:gson:2.2.4")
}
You could also do this which would include all JARs in the local repository. This way you wouldn't have to specify it every time.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
The following works for me:
compile fileTree(dir: 'libs', include: '*.jar')
Refer to the Gradle Documentation.
You can try reusing your local Maven repository for Gradle:
Install the jar into your local Maven repository:
mvn install:install-file -Dfile=utility.jar -DgroupId=com.company -DartifactId=utility -Dversion=0.0.1 -Dpackaging=jar
Check that you have the jar installed into your ~/.m2/ local Maven repository
Enable your local Maven repository in your build.gradle file:
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
implementation ("com.company:utility:0.0.1")
}
Now you should have the jar enabled for implementation in your project
A solution for those using Kotlin DSL
The solutions added so far are great for the OP, but can't be used with Kotlin DSL without first translating them. Here's an example of how I added a local .JAR to my build using Kotlin DSL:
dependencies {
compile(files("/path/to/file.jar"))
testCompile(files("/path/to/file.jar"))
testCompile("junit", "junit", "4.12")
}
Remember that if you're using Windows, your backslashes will have to be escaped:
...
compile(files("C:\\path\\to\\file.jar"))
...
And also remember that quotation marks have to be double quotes, not single quotes.
Edit for 2020:
Gradle updates have deprecated compile and testCompile in favor of implementation and testImplementation. So the above dependency block would look like this for current Gradle versions:
dependencies {
implementation(files("/path/to/file.jar"))
testImplementation(files("/path/to/file.jar"))
testImplementation("junit", "junit", "4.12")
}
The accepted answer is good, however, I would have needed various library configurations within my multi-project Gradle build to use the same 3rd-party Java library.
Adding '$rootProject.projectDir' to the 'dir' path element within my 'allprojects' closure meant each sub-project referenced the same 'libs' directory, and not a version local to that sub-project:
//gradle.build snippet
allprojects {
...
repositories {
//All sub-projects will now refer to the same 'libs' directory
flatDir {
dirs "$rootProject.projectDir/libs"
}
mavenCentral()
}
...
}
EDIT by Quizzie: changed "${rootProject.projectDir}" to "$rootProject.projectDir" (works in the newest Gradle version).
Shorter version:
dependencies {
implementation fileTree('lib')
}
The Question already has been answered in detail. I still want to add something that seems very surprising to me:
The "gradle dependencies" task does not list any file dependencies. Even though you might think so, as they have been specified in the "dependencies" block after all..
So don't rely on the output of this to check whether your referenced local lib files are working correctly.
A simple way to do this is
compile fileTree(include: ['*.jar'], dir: 'libs')
it will compile all the .jar files in your libs directory in App.
Some more ways to add local library files using Kotlin DSL (build.gradle.kts):
implementation(
files(
"libs/library-1.jar",
"libs/library-2.jar",
"$rootDir/foo/my-other-library.jar"
)
)
implementation(
fileTree("libs/") {
// You can add as many include or exclude calls as you want
include("*.jar")
include("another-library.aar") // Some Android libraries are in AAR format
exclude("bad-library.jar")
}
)
implementation(
fileTree(
"dir" to "libs/",
// Here, instead of repeating include or exclude, assign a list of paths
"include" to "*.jar",
"exclude" to listOf("bad-library-1.jar", "bad-library-2.jar")
)
)
The above code assumes that the library files are in libs/ directory of the module (by module I mean the directory where this build.gradle.kts is located).
You can use Ant patterns in includes and excludes as shown above.
See Gradle documentations for more information about file dependencies.
Thanks to this post for providing a helpful answer.
I couldn't get the suggestion above at https://stackoverflow.com/a/20956456/1019307 to work. This worked for me though. For a file secondstring-20030401.jar that I stored in a libs/ directory in the root of the project:
repositories {
mavenCentral()
// Not everything is available in a Maven/Gradle repository. Use a local 'libs/' directory for these.
flatDir {
dirs 'libs'
}
}
...
compile name: 'secondstring-20030401'
The best way to do it is to add this in your build.gradle file and hit the sync option
dependency{
compile files('path.jar')
}
The solution which worked for me is the usage of fileTree in build.gradle file.
Keep the .jar which need to add as dependency in libs folder. The give the below code in dependenices block in build.gradle:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
You can add jar doing:
For gradle just put following code in build.gradle:
dependencies {
...
compile fileTree(dir: 'lib', includes: ['suitetalk-*0.jar'])
...
}
and for maven just follow steps:
For Intellij:
File->project structure->modules->dependency tab-> click on + sign-> jar and dependency->select jars you want to import-> ok-> apply(if visible)->ok
Remember that if you got any java.lang.NoClassDefFoundError: Could not initialize class exception at runtime this means that dependencies in jar not installed for that you have to add all dependecies in parent project.
For Gradle version 7.4 with Groovy build file
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
implementation ':gson-2.2.4'
}
If you are on gradle 4.10 or newer:
implementation fileTree(dir: 'libs', includes: ['*.jar'])
Goto File -> Project Structure -> Modules -> app -> Dependencies Tab -> Click on +(button) -> Select File Dependency - > Select jar file in the lib folder
This steps will automatically add your dependency to gralde
Very Simple
Be careful if you are using continuous integration, you must add your libraries in the same path on your build server.
For this reason, I'd rather add jar to the local repository and, of course, do the same on the build server.
An other way:
Add library in the tree view. Right click on this one. Select menu "Add As Library".
A dialog appear, let you select module. OK and it's done.

Gradle Warning "unspecified depends on libraries but is a jar " while compiling an android module within a custom java library module

I am a newbee in Gradle. I have a simple project structure (shown bellow) having a main android app module, one android module (myandroidlibrary), and one pure java module (myjavalibrary). They have simple dependencies, app -> myjavalibary, and myjavalibary -> myandroidlibrary (pls see fig. below). Gradle files snapshots are also given below.
However, while sync the gradle it produces following error:
D:\MyTestCodes\MyTestApplication\app\build.gradle
Warning:Module version MyTestApplication:myjavalibrary:unspecified depends on libraries but is a jar
Pls help me out! I have spent this whole day to sort it out with no result!
MyProject
- app
- myjavalibrary (pure java library)
- myandroidlibrary (android library)
Now the dependency is as follows:
"app" depends on -> "myjavalibrary"
"myjavalibrary" depends on -> "myandroidlibrary"
Gradle files for each of the modules are as follows:
build.gradle for app:
apply plugin: 'com.android.application'
android {
// ommitting other detail
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
compile project(':myjavalibrary')
}
build.gradle for myjavalibrary:
apply plugin: 'java'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':myandroidlibrary')
}
build.gradle for myandroidlibrary:
apply plugin: 'com.android.application'
android {
//ommiting other detail.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
}
settings.gradle:
include ':app', ':myjavalibrary', ':myandroidlibrary'
Now while I sync the gradle files it shows the following error:
D:\MyTestCodes\MyTestApplication\app\build.gradle
Warning:Module version MyTestApplication:myjavalibrary:unspecified depends on libraries but is a jar
Warning is caused by pure-jave myjavalibrary module having a dependency on the myandroidlibrary one, which is an Android library.
Gradle warns you that a pure-java module doesn't know anything about Android specific stuff of myandroidlibrary (like Android resources, assets etc.). By having this dependency (pure-java to android library one) you might lose some stuff you expect to have.
A much cleaner dependency direction would be the one from a android library to a pure-java library. In this case Gradle won't give you any warnings.
If you want to create an Android app project from java code, use apply plugin: 'com.android.application'.
If you want to create a library project from java code, use apply plugin: 'com.android.library'.
If you want to use pre-built jar files, do not create any project for them. Just add them into the projects, into the libs folder, which depend on them. The compile fileTree(dir: 'libs', include: ['*.jar']) in the dependencies would take care of them.

java.lang.NoSuchMethodError on compile

I'm trying to compile an Android project unsuccessfully. The error message is:
Execution failed for task ':mobile:_compileAppDebug'.
java.lang.NoSuchMethodError: com.google.auto.common.MoreTypes.asTypeElements(Ljavax/lang/model/util/Types;Ljava/lang/Iterable;)Lcom/google/common/collect/ImmutableSet;
Here are my module's gradle dependencies in which I specify a number of libraries including google Auto:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':library')
compile 'com.google.dagger:dagger:2.0-SNAPSHOT'
provided 'com.google.auto.value:auto-value:1.0-rc1'
apt 'com.google.dagger:dagger-compiler:2.0-SNAPSHOT'
provided 'org.glassfish:javax.annotation:10.0-b28'
compile 'com.jakewharton:butterknife:6.1.0'
compile 'com.f2prateek.dart:dart:1.1.0'
}
When I looked at the dependencies I thought I just needed google auto value since that is where the missing method resides but adding the provided does not resolve the issue.
The project gradle file includes the retrolambda plugin
dependencies {
classpath 'me.tatarka:gradle-retrolambda:2.5.0'
classpath 'com.android.tools.build:gradle:1.0.1'
classpath 'com.jakewharton.sdkmanager:gradle-plugin:0.12.+'
classpath 'io.fabric.tools:gradle:1.+'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
}
Can anyone help me identify which dependencies cause the compile error? Interestingly enough, when I copy the gradle files into an empty project everything runs fine.
Dagger 2.0-SNAPSHOT depends on an Auto SNAPSHOT which had an API change: https://github.com/google/dagger/issues/113
This is perfectly normal and acceptable thing for libraries which are under development. If you cannot tolerate an occasional broken build, do not depend on non-release versions in a manner that can change at any time without warning.
I ran in a similar issue. Some libary I'm using bundles Guava within the jar file.
Thus exluding this specific dependency from the apt configuration fixed the problem:
configurations {
apt.exclude module: 'artifactId-Of-Library'
}

Different dependencies for debug and release in gradle and Android Studio

I have an Android project, that depends on pure Java project. Both of them depend on another Java library, also in my multiproject gradle set in Android Studio. I have two versions of that library and want both Android and Java projects to depend on one of them in debug mode, and another - in release.
Is it possible for Android project? For pure Java project? How?
Build Types (debug, release, or custom) can have their own dependencies.
To specify a dependency specific to a build type, do the following:
dependencies {
debugCompile "mydebugdependency"
releaseCompile "myreleasedependency"
}
If your java project and android project are both using gradle, you can do the above in both of their build.gradle files.
My buildDebug dependency was also getting ignored. My setup is the app module and a library module, and I have the need to propagate the build type from the app to the library modules, i.e., when I compile the debug type on the app I want to get the library debug type too.
As mentioned, I tried having a specific dependency for each build type on the app gradle file, but to no avail:
buildTypes {
debug {
debuggable true
applicationIdSuffix ".debug"
dependencies {
debugCompile project(":library")
}
}
}
Ultimately what did the trick for me was this: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Library-Publication
So, now the library dependency is managed (as usual) in the global dependencies scope in the app gradle file:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
releaseCompile project(path: ':library', configuration: 'release')
debugCompile project(path: ':library', configuration: 'debug')
}
and had to add this to the library's gradle build file:
android {
publishNonDefault true
}
This publishes all of the dependencies' build types. Note that if it takes a lot of time to compile your dependency, this solution might not be right for you.
You are able to do this using next pattern
build_variant_name dependency_configurations "dependency"
build_variant_name dependency_configurations project(path: ':libName', configuration: 'build_variant_name of libName')
For example
dependencies {
FreeDebugImplementation "dependency"
PaidReleaseApi project(path: ':libName', configuration: 'release')
}
You can read more about
build variants - https://developer.android.com/studio/build/build-variants
dependency configurations - https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration#new_configurations

Adding Gradle depdencies to IntelliJ compiler classpath

Given a build.gradle that has dependencies like these:
dependencies {
compile fileTree(dir: 'myLegacyLibsNotYetConverted/lib', include: '*.jar')
compile fileTree(dir: 'myOtherLegacyLibsNotYetConverted/lib', include: '*.jar')
compile 'org.springframework:spring-tx:3.1.2.RELEASE'
compile 'org.springframework:spring-jms:3.1.2.RELEASE'
compile 'org.springframework:spring-jdbc:3.1.2.RELEASE'
(...)
testCompile "junit:junit:4.11"
testCompile fileTree(dir: "legacyBuild/test-lib", include: "*.jar")
(...)
providedCompile "javax.servlet:servlet-api:2.5"
providedCompile "javax.servlet.jsp:jsp-api:2.2"
}
What is the easiest/recommended way to add them to the IntelliJ's IDE's classpath so that all my interactive compiler and tests have all the class dependencies they need? Is it possible to sync it every time it changes?
A flexible solution that could work in Eclipse with minor changes would be much appreciated too.
Assuming you've got Gradle plugin installed(Preferences>Plugins>Install JetBrains plugin>Gradle), using IntelliJ 12, just clicking on Refresh Gradle Project should put all dependencies in your classpath.
The easiest solution is to use the IDE plugins. IntelliJ 13 (EAP) ships with great out-of-the-box support for Gradle. (I don't recommend to use the IntelliJ 12 JetGradle plugin as it's too limited.) For Eclipse there is the Eclipse Gradle Tooling. Both plugins will resync dependencies at the push of a button.
The other approach is to use the eclipse/idea Gradle plugins and (re)generate IDE project files whenever necessary. This approach is documented in the Gradle User Guide.

Categories

Resources