i have a problem adding dependencies automatically to eclipse android project via gradle. I have only a little bit experience with gradle. Till now I have build two java projects with gradle. One jar and an executable-jar. This works without problems. I have used the eclipse plugin to generate the eclipse project and add the dependenies to the build path. I added new dependencies to the gradle script, started gradle with gradle eclipse ,update my project and the dependencies exist in the build path and I can used them. Here is the important part of that script.
apply plugin: 'java'
apply plugin: 'eclipse'
repositories {
mavenCentral()
}
dependencies {
compile 'commons-io:commons-io:2.4'
}
So, now I tried it in combination with the android plugin. Here is my hole gradle script.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android'
apply plugin: 'eclipse'
repositories {
mavenCentral()
}
dependencies {
compile 'org.apache.commons:commons-lang3:3.1'
}
android {
compileSdkVersion 17
buildToolsVersion "17"
defaultConfig {
minSdkVersion 14
targetSdkVersion 17
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest.setRoot('tests')
}
}
If I use gradle eclipse nothing happens. Then I found out that the java plugin adds the dependencies to the build path. So I added
apply plugin: 'java'
to it and got the error that the java plugin is not compatible with the android plugin. Then I found a solution to copy the jars automatically to the lib folder of the project.
def libDir = file('libs')
task copyLibs(type: Copy) {
doFirst {
libDir.mkdirs()
}
from configurations.runtime
into libDir
}
But this task needs the java plugin too for the configurations.runtime. I need the android plugin to create the apk file, so it is not a solution to remove the android plugin. Has somebody an idea if it is possible to add the dependencies to the build path or lib folder in ecipse project that is compatible with the android plugin?
EDIT: One of my ideas was to put the java-plugin to the eclipse-plugin, so that it will be only applied when the eclipse plugin will be applied. Something like this:
apply plugin: 'eclipse'
eclipse{
apply plugin: 'java'
}
But I still get the error that the java and android plugins are not compatible. Maybe I understand gradle wrong, but normally the java plugin should be applied only when I start the eclipse plugin and not the android plugin. I´m afraid that my understanding and experience of gradle is not good enough to solve this this way or understand why it is not possible.
Import an existing Gradle project You can also import existing Gradle projects into Eclipse. Select the File Import… Gradle Gradle Project menu entry for this. After pressing the Next > button, you need to specify the root directory of your Gradle project.
To add a dependency to your project, specify a dependency configuration such as implementation in the dependencies block of your module's build. gradle file. This declares a dependency on an Android library module named "mylibrary" (this name must match the library name defined with an include: in your settings.
Buildship is an Eclipse plugin that allows you to build applications and libraries using Gradle through your IDE.
Android Studio uses Gradle, an advanced build toolkit, to automate and manage the build process, while allowing you to define flexible custom build configurations. Each build configuration can define its own set of code and resources, while reusing the parts common to all versions of your app.
My solution is based off Rafael's in that it copies dependencies to the libs directory which is only used by Android. However I go further to completely explode the referenced AAR's for use in Eclipse.
Add the following to the end of your Android projects build.gradle :
task copyJarDependencies(type: Copy) {
description = 'Used for Eclipse. Copies all dependencies to the libs directory. If there are any AAR files it will extract the classes.jar and rename it the same as the AAR file but with a .jar on the end.'
libDir = new File(project.projectDir, '/libs')
println libDir
println 'Adding dependencies from compile configuration'
configurations.compile.filter {it.name.endsWith 'jar'}.each { File file -> moveJarIntoLibs(file)}
println 'Adding dependencies from releaseCompile configuration'
configurations.releaseCompile.filter {it.name.endsWith 'jar'}.each { File file -> moveJarIntoLibs(file)}
println 'Adding dependencies from debugCompile configuration'
configurations.debugCompile.filter {it.name.endsWith 'jar'}.each { File file -> moveJarIntoLibs(file)}
println 'Adding dependencies from instrumentTestCompile configuration'
configurations.instrumentTestCompile.filter {it.name.endsWith 'jar'}.each { File file -> moveJarIntoLibs(file)}
println 'Extracting dependencies from compile configuration'
configurations.compile.filter {it.name.endsWith 'aar'}.each { File file -> moveAndRenameAar(file) }
println 'Extracting dependencies from releaseCompile configuration'
configurations.releaseCompile.filter {it.name.endsWith 'aar'}.each { File file -> moveAndRenameAar(file) }
println 'Extracting dependencies from debugCompile configuration'
configurations.debugCompile.filter {it.name.endsWith 'aar'}.each { File file -> moveAndRenameAar(file) }
println 'Extracting AAR dependencies from instrumentTestCompile configuration'
configurations.instrumentTestCompile.filter {it.name.endsWith 'aar'}.each { File file -> moveAndRenameAar(file) }
}
void moveJarIntoLibs(File file){
println 'Added jar ' + file
copy{
from file
into 'libs'
}
}
void moveAndRenameAar(File file){
println 'Added aar ' + file
def baseFilename = file.name.lastIndexOf('.').with {it != -1 ? file.name[0..<it] : file.name}
// directory excluding the classes.jar
copy{
from zipTree(file)
exclude 'classes.jar'
into 'libs/'+baseFilename
}
// Copies the classes.jar into the libs directory of the expoded AAR.
// In Eclipse you can then import this exploded ar as an Android project
// and then reference not only the classes but also the android resources :D
copy{
from zipTree(file)
include 'classes.jar'
into 'libs/' + baseFilename +'/libs'
rename { String fileName ->
fileName.replace('classes.jar', baseFilename + '.jar')
}
}
}
Run :
"gradle clean build"
You should find all dependencies and exploded AARs in your libs directory. This is all Eclipse should need.
Now this is where the real benefit begins. After you've generated the libs directory from the gradle step above you'll notice there are folders in there too. Those new folders are the exploded AAR dependencies from your build.gradle file.
Now the cool part is that when you import your existing Android project into Eclipse it will also detect the exploded AAR folders as projects it can import too!
1. Import those folders under your project's libs directory, don't import any 'build' folders, they're generated by Gradle
2. Ensure you perform a Project -> Clean on all AAR projects you've added. In your workspace check that each AAR exploded project has the following in the project.properties :
target=android-<YOUR INSTALLED SKD VERSION GOES HERE>
android.library=true
3. Now in your main Android project you can just add the library references with either ADT or you can just edit the project.properties file and add
android.libraries.reference.1=libs/someExplodedAAR/
4. Now you can right-click on your main Android project and Run as -> Android Application.
Well it means you don't need the source code for any of your Android AAR Gradle dependencies in order to reference both it's classes and resources in Eclipse.
The gradle build script above takes the AAR file and prepares it for use in Eclipse. Once you add it to your workspace you're ready to just focus on your actual main Android project.
You can now debug and develop using Eclipse and deploy using ADT with AAR dependencies being properly bundled in the APK. When you need to make some specific builds then you can use gradle.
Finally I have found a solution that works for me. This task copies the dependencies to the projects libs folder.
task copyDependencies(type: Copy)
{
description = 'Copy depencies to libs. Useful for Eclipse'
libDir = new File(project.projectDir, '/libs')
println libDir
println 'Adding dependencies from compile configuration'
for(file in configurations.compile)
{
println 'Added ' + file
copy
{
from file
into libDir
}
}
println 'Adding dependencies from releaseCompile configuration'
for(file in configurations.releaseCompile)
{
println 'Added ' + file
copy
{
from file
into libDir
}
}
println 'Adding dependencies from debugCompile configuration'
for(file in configurations.debugCompile)
{
println 'Added ' + file
copy
{
from file
into libDir
}
}
println 'Adding dependencies from instrumentTestCompile configuration'
for(file in configurations.instrumentTestCompile)
{
println 'Added ' + file
copy
{
from file
into libDir
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With