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'
Related
I'm trying to separate my android application into several modules. For instance I want to have 2 extra modules - Core and ViewModels. Both of them are pure java modules. However I'm having troubles when adding Dagger 2 dependencies to those java modules. Here's the build gradle file of one of the modules
apply plugin: 'java-library'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.google.dagger:dagger:2.15'
annotationProcessor 'com.google.dagger:dagger-compiler:2.15'
implementation 'org.greenrobot:eventbus:3.1.1'
}
sourceCompatibility = "1.7"
targetCompatibility = "1.7"
However, when I sync the gradle I get the following error
Could not find method classpath() for arguments [com.neenbedankt.gradle.plugins:android-apt:1.8] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
Open File
Any ideas why is this happening?
I've resolved the issue. Here's what my final build.gradle of the custom module looks like
plugins {
id "net.ltgt.apt" version "0.15"
}
apply plugin: 'java-library'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.google.dagger:dagger:2.15'
apt 'com.google.dagger:dagger-compiler:2.13'
}
sourceCompatibility = "1.8"
targetCompatibility = "1.8"
Dagger 2 successfuly generated necessary code and app worked like magic.
I have a Java module that generates some metadata for my Android app and needs to run as a regular Java application. After updating to Android Studio 3.0 (in 2.3.3 it worked as expected) I get a NoClassDefFoundError for the dependencies (in the case below for com/google/gson/GsonBuilder, the main method itself can be found).
I even tried putting the GSON Jar into the libs folder but it still throws the NoClassDefFoundError.
I assume the dependencies are not properly included. Any idea how to fix this?
The build.gradle looks like this:
apply plugin: 'java'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.google.code.gson:gson:2.8.1'
}
sourceCompatibility = "1.7"
targetCompatibility = "1.7"
My class looks like this:
import com.google.gson.GsonBuilder;
public class myClass {
public static void main(String[] args) {
GsonBuilder builder = new GsonBuilder();
}
}
Use api instead of implementation.
From the docs
When your module configures an implementation dependency, it's
letting Gradle know that the module does not want to leak the
dependency to other modules at compile time. That is, the dependency
is available to other modules only at runtime.
Using this dependency
configuration instead of api or compile can result in significant
build time improvements because it reduces the amount of projects that
the build system needs to recompile. For example, if an implementation
dependency changes its API, Gradle recompiles only that dependency and
the modules that directly depend on it. Most app and test modules
should use this configuration.
See difference between api and implementation here for more info.
This is a problem probably related with Android Gradle plug-in. Try using compile instead of implementation in yourbuild.gradle`:
apply plugin: 'java'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.code.gson:gson:2.8.1'
}
sourceCompatibility = "1.7"
targetCompatibility = "1.7"
The above is working in Android Studio 3.0 but with Android Gradle plug-in 2.3.3 in root build.gradle:
classpath 'com.android.tools.build:gradle:2.3.3'
UPDATE
I manage to make it works by adding google() to repositories block in root build.gradle. The following build.gradle is for the Java module:
apply plugin: 'java'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.google.code.gson:gson:2.8.1'
}
sourceCompatibility = "1.8"
targetCompatibility = "1.8"
And this is my root build.gradle:
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
}
}
allprojects {
repositories {
jcenter()
google()
}
}
Tested on Android Studio 3.0 with Gradle 4.1.
I am developing for Android, and I am trying to include another project's class files in my .jar file.
My build.gradle file for library project looks like this:
apply plugin: 'java'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.code.gson:gson:2.3'
compile project(':xyz')
}
My build.gradle file for xyz project looks like this:
apply plugin: 'java'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
}
The issue is that when I create another project that includes my jar library, it has unresolved references that exist in my xyz project. When I run that project, it complains about not being able to find a class that exists in the xyz project.
Right click on app , open module settings (F4), Dependencies, Green + and select library file. build.gradle is updated automatically :)
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. :-)
I tried to import a project(projLib) as dependency for another project(projAPK).
projAPK gradle has this :
dependencies {
compile project(':libs:NewsAPI')
compile project(':projLib')
}
but when i sync the gradle it gives this error:
Error:Dependency Android_2015:projLib:unspecified on project projAPK resolves to an APK archive which is not supported as a compilation dependency. File: /Users/myname/Documents/Development/Android_2015/libs/projAPK/build/outputs/apk/projLib-release-unsigned.apk
so I guess there are two solution to this:
somehow make gradle think that projLib is a library that shouldn't be compiled to apk
somehow make gradle NOT compile the projLib explicitly
The problem is, I couldn't find how to do any of that.
Would be awesome if you guys can help :)
In projLib's build.gradle file, you'll see a statement like this:
apply plugin: 'com.android.application'
which tells Gradle to build it as an application, generating an APK. If you change it to this:
apply plugin: 'com.android.library'
it will build as a library, generating an AAR, and it should work.
If you also need projLib to generate a separate APK, then you'll have to do some refactoring to pull the common code that you need out into a third library module, and have both APKs depend on it.
Libraries aren't allowed to set an applicationId, so if you see an error message to that effect, remove it from the library's build script.
In module gradle file-
Replace apply plugin: 'com.android.application' with apply plugin: 'com.android.library'
Then remove applicationId "xxx.xxx.xxxx"
Clean and Build
just add these lines to library gradle file and remove other sections
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion '23.0.2'
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.squareup.picasso:picasso:2.4.0'
compile 'com.google.code.gson:gson:2.2.4'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:gridlayout-v7:23.1.1'
,...
}
For Kotlin DSL (build.gradle.kts), use this notation:
plugins {
id("com.android.library")
// ...
}