I'm trying to use Picasso library in my gradle-android project. I'm using eclipse IDE. When I make gradle build to my project, that is build correctly but in my *.java files, that are using Picasso, I get "Picasso cannot be resolved". Using Ctrl-Shift-R on eclipse I found the jar of Picasso but In the folders build/intermediate/pre-dexed/release and build/intermediate/pre-dexed/debug. My gradle file is below, Could you help me please?
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.3'
}
}
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
minSdkVersion 16
targetSdkVersion 22
}
}
repositories {
mavenCentral()
}
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile 'com.squareup.picasso:picasso:2.5.0'
}
dependencies {
//compile 'com.parse.bolts:bolts-android:1.+'
//compile fileTree(dir: 'libs', include: 'Parse-*.jar')
compile fileTree(dir: 'libs', include: '*.jar')
compile 'com.android.support:appcompat-v7:21.0.0+'
}
to use Gradle you must use Android Studio
https://developer.android.com/sdk/index.html
Eclipse does not support gradle, only Maven
I had the same problem in using the Picasso library(Cannot be resolved). I faced it in eclipse. I don't know whether it would help you in Android studio. My solution was, I just deleted the libs folder in my project and created the same folder again. After that i copied my picasso jar file to that. And from that moment on wards import option is available for me and the error is gone. :-)
Related
I use Android Studio 2.2 and Gradle in offline mode. The value of Gradle Home is /path/to/gradle/gradle-2.14.1. I can run Android project but now I want to run a Java standard class to test some Java code before using them in Android project. So I followed this answer. But when I run class, I got an error like this:
Error:Gradle: A problem occurred configuring root project 'demo'.
> Could not resolve all dependencies for configuration ':classpath'.
> Could not resolve com.android.tools.build:gradle:2.2.2.
Required by:
:demo:unspecified
> No cached version of com.android.tools.build:gradle:2.2.2 available for offline mode.
Also here is content of build.gradle of Java library:
apply plugin: 'java'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
sourceCompatibility = "1.7"
targetCompatibility = "1.7"
How I can solve this problem? (I do not want to use another IDE or enabling online mode for Gradle)
You have to download com.android.tools.build:gradle:2.2.2...
And that requires internet. The package gradle-2.14.1 is not the same thing, as that is Gradle itself, and not the Android Gradle plugin.
Though, it is not clear why you have applied that plugin on a standard Java module.
All you need is
apply plugin: 'java'
In other words, Gradle simply builds Android code. It's not related to Android in anyway other than that, and you can run Java projects independent of Android if you set it up correctly.
Gradle - Java Plugin
For example,
java-code/build.gradle
apply plugin: 'java'
targetCompatibility = '1.7'
sourceCompatibility = '1.7'
test {
testLogging {
// Show that tests are run in the command-line output
events 'passed'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
}
app/build.gradle
apply plugin: 'com.android.application'
evaluationDependsOn(':java-code')
...
dependencies {
compile project(":java-code")
compile fileTree(dir: 'libs', include: ['*.jar'])
...
}
settings.gradle
include ':app', ':java-code'
I have Android Studio library project which has it's own Gradle dependencies:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
}
The problem is with those two external libraries (retrofit & converter-gson).
Result of this library project is .aar file. But when I use this .aar file in a separate project I get unresolved class error:
java.lang.NoClassDefFoundError: Failed resolution of: Lretrofit/Retrofit$Builder;
Is there a way to include those gradle dependencies from my library project into the final .aar file?
I tried this answer but it didn't work for me.
EDIT:
My whole build.gradle file from library project.
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
}
EDIT 2:
This is the part of the build.gradle script in my app project which uses the library which was generated before as an .aar file.
3 versions and none of them work (none of them download automatically dependencies form the library)
(note, I am adding this .aar file into the lib folder of my app project and adding the flatDir instruction into it's gradle.build). I am not doing File->New->NewModule->ImportExistingJAR/AAR
repositories {
flatDir {
dirs 'libs'
}
}
1.
compile(name:'mylibrary-release', ext:'aar')
2.
compile(name:'mylibrary-release', ext:'aar') {
transitive = true
}
3.
compile(':mylibrary-release#aar') {
transitive = true
}
Your AAR library is not the problem. Libraries are not supposed to include their dependencies as it may cause version conflicts further down.
The reason your app project is not transitively loading your library's dependencies is the flatdir. Here's the relevant part in the docs (https://docs.gradle.org/current/userguide/declaring_repositories.html#sub:flat_dir_resolver):
This type of repository does not support any meta-data formats like
Ivy XML or Maven POM files. Instead, Gradle will dynamically generate
a module descriptor (without any dependency information) based on the
presence of artifacts.
The correct approach would be to host your library in a public or private maven repository.
The accepted answer from How to include JAR dependency into an AAR library works. You need to add an evaluation listener, like this:
afterEvaluate {project -> project.tasks.preBuild.dependsOn copyLibs}
I have a "java module" which includes some jar files (lib.jar). They are included within my module build.gradle file using the fileTree method.
compile fileTree(dir: 'libs', include: ['*.jar'])
When I include the "java module" in the main app as a dependency, I always get ClassDefNotFoundException when the module tries to access classes within the lib.jar.
Following is the project hierarchy:
|--mylibrary (java module)
|
--libs/
|
--lib.jar
--build.gradle
|--app
|
--src/
--libs/
--build.gradle
This issue only happens when the module is a "java module". I've tried with an "android library module" as the dependency and the jars are included fine. What am I missing?
Java module build.gradle:
apply plugin: 'java'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
App build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.macolighting.mncp.myapplication"
minSdkVersion 21
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':mylibrary')
}
In case any was wondering how I got around this, I had to manually extract the jar dependency into the build jar. It's quite abit hacky but it works. Submitted a bug to google and hopefully they'll figure something out. https://code.google.com/p/android/issues/detail?id=186012
apply plugin: 'java'
configurations {
libDependency
}
jar {
from {
configurations.libDependency.collect {
it.isDirectory() ? it : zipTree(it)
}
}
}
dependencies {
FileTree libs = fileTree(dir: 'libs', include: '*.jar')
libDependency libs
compile libs
}
I'm using Qt 5.4. I imported SDK & NDK.
Actually, I was trying to use multiple line notification and I used this line in java file:
customMainActivity.java:
import android.support.v4.app.NotificationCompat;
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context);
I'm getting an error :
package android.support.v4.app does not exist
I read it and it and added android-support-v4.jar and android-support-v7-appcompat.jar but I don't know how to fix it in Qt.
Add Google's Maven repository to build.gradle
https://developer.android.com/studio/build/dependencies#google-maven
allprojects {
repositories {
jcenter()
maven {
url 'https://maven.google.com'
}
}
}
Add dependency to support-v4 library to build.gradle
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile "com.android.support:support-v4:24.+"
}
You need to add the dependency to your build.gradle file (it's created and added to your project when you create android manifest via Create Templates button).
Something like this:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile "com.android.support:support-v4:23.0.+"
}
Problem:
Up until now I've been using Gradle to handle all of my dependencies, and it seems to take care of any duplicate dependencies between other Gradle modules. However, this does not seem to be the case when a duplicate dependency exists within a jar.
Question:
Considering that I have control over what goes into the jar, What is the best practices for handling these dependency conflicts using Gradle:
Do not include any external dependencies in the jar, include them in the project itself using build.gradle
Include all external dependcies in the jar, remove duplicates as they occur by removing them from the project build.gradle. (NOTE: this does not seem scalable, e.g. if there are duplicates between jars themselves.)
Something better (that hopefully handles this automatically)
EDIT: build.gradle file:
apply plugin: 'com.android.application'
android {
signingConfigs {
release { ... }
debug { ... }
}
compileSdkVersion 19
buildToolsVersion '19.1.0'
defaultConfig { ... }
buildTypes {
release { ... }
debug { ... }
}
sourceSets {
instrumentTest.setRoot('tests')
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:20.0.0'
compile project(':jarModule')
}
When importing external jars that have a dependency that you also have in your local app, you can do two things:
Convert your jar dependency to a Gradle dependency and exclude the dependency
For example:
testCompile('org.robospock:robospock:0.5.0') {
exclude module: "groovy-all" // <-- excludes groovy from robo spock
}
or
Remove the local dependency in your app and rely on the one in the .jar
For example, in your case with Gson:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:20.0.0'
compile project(':jarModule')
// compile 'com.google.code.gson:gson:2.3.1' // <-- removed
}