在Android项目中通过gradle添加依赖关系到eclipse [英] Add dependencies via gradle to eclipse in android project

查看:585
本文介绍了在Android项目中通过gradle添加依赖关系到eclipse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,通过gradle将依赖项自动添加到eclipse的android项目中。
我只有一点点毕业生的经验。到目前为止,我已经建立了两个带有毕业生的java项目。一个jar和一个可执行jar。这没有问题。
我使用eclipse插件来生成eclipse项目,并将依赖关系添加到构建路径中。我添加了新的依赖关系到毕业生脚本,开始gradle与gradle eclipse,更新我的项目和依赖项存在于构建路径,我可以使用它们。这是脚本的重要部分。

 应用插件:'java'
应用插件:'eclipse'
存储库{
mavenCentral()
}

依赖关系{
编译'commons-io:commons-io:2.4'
}

所以,现在我结合使用android插件。这是我的小孔毕业生脚本。

  buildscript {
repositories {
mavenCentral()
}
依赖关系{
classpath'com.android.tools.build:gradle:0.4'
}
}

应用插件:'android'
应用插件:'eclipse'

存储库{
mavenCentral()
}

依赖关系{
compile'org.apache .commons:commons-lang3:3.1'
}

android {
compileSdkVersion 17
buildToolsVersion17

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']
资产.srcDirs = ['assets']
}

instrumentTest.setRoot('tests')
}
}

如果我使用gradle eclipse没有任何反应。然后我发现java插件将附件添加到构建路径。所以我添加了

 应用插件:'java'

,并得到java插件与android插件不兼容的错误。
然后我发现一个解决方案将jar自动复制到项目的lib文件夹。

  def libDir = file ('libs')

任务copyLibs(类型:复制){

doFirst {
libDir.mkdirs()
}
从configuration.runtime
into libDir
}

但这个任务需要java插件对于.config配置。
我需要android插件来创建apk文件,所以它不是解决方案来删除android插件。
有人可以将依赖项添加到与android插件兼容的ecipse项目中的构建路径或lib文件夹吗?



编辑:
我的一个想法是将java插件放到eclipse插件中,这样只有当eclipse插件应用。这样的东西:

 应用插件:'eclipse'

eclipse {
应用插件:'java'
}

但是我仍然收到java和android插件的错误不兼容
也许我理解毕业生错误,但是通常只有当我启动eclipse插件而不是android插件时,才应用java插件。我恐怕我对毕业生的理解和经验不够好,以这种方式解决这个问题,或者明白为什么不可能。

解决方案

我的解决方案基于 Rafael的,因为它将依赖关系复制到仅由Android使用的libs目录。但是,我进一步完全爆炸了在Eclipse中使用的引用的AAR。



Gradle Build File



添加在Android项目结束之后build.gradle:

  task copyJarDependencies(type:Copy){
description =用于Eclipse将所有依赖项复制到libs目录。如果有任何AAR文件,它将提取classes.jar并将其重命名为与AAR文件相同,但在.jar结尾。
libDir = new File(project.projectDir,'/ libs')
println libDir
println'从编译配置中添加依赖项
configured.compile.filter {it.name.endsWith'jar'}。每个{File file - > moveJarIntoLibs(file)}
println'从releaseCompile配置添加依赖项
configured.releaseCompile.filter {it.name.endsWith'jar'}。每个{File file - > moveJarIntoLibs(file)}
println'从debugCompile配置中添加依赖项
configurations.debugCompile.filter {it.name.endsWith'jar'}。每个{File file - > moveJarIntoLibs(file)}
println'从instrumentTestCompile配置添加依赖项
configured.instrumentTestCompile.filter {it.name.endsWith'jar'}。每个{文件文件 - > moveJarIntoLibs(file)}
println'从编译配置中提取依赖项
configured.compile.filter {it.name.endsWith'aar'}。每个{File file - > moveAndRenameAar(file)}
println'从releaseCompile配置中提取依赖关系
configured.releaseCompile.filter {it.name.endsWith'aar'}。每个{File file - > moveAndRenameAar(file)}
println'从debugCompile配置中提取依赖项
configured.debugCompile.filter {it.name.endsWith'aar'}。每个{File file - > moveAndRenameAar(file)}
println'从instrumentTestCompile配置中提取AAR依赖项
configured.instrumentTestCompile.filter {it.name.endsWith'aar'}。每个{File file - > moveAndRenameAar(file)}
}

void moveJarIntoLibs(文件文件){
println'添加jar'+文件
copy {
from file
into'libs'
}
}

void moveAndRenameAar(文件文件){
println'添加了aar'+文件
def baseFilename = file .name.lastIndexOf('。')。与{it!= -1? file.name [0 ..< it]:file.name}

//目录不包括classes.jar
copy {
from zipTree(file)
将'classes.jar'
转换为'libs /'+ baseFilename
}

//将classes.jar复制到已经被转载的AAR的libs目录中。
//在Eclipse中,您可以导入这个爆炸的ar作为一个Android项目
//,然后不仅引用类,还可以引用Android资源:D
copy {
from zipTree(file)
include'classes.jar'
into'libs /'+ baseFilename +'/ libs'
rename {String fileName - >
fileName.replace('classes.jar',baseFilename +'.jar')
}
}
}



使用Gradle构建



运行:



您可以在libs目录中找到所有的依赖关系和分解的AAR,这样你就可以找到所有的依赖项。这就是Eclipse应该需要的。



在Eclipse中导入



现在这是真正的好处开始了。从上面的渐变步骤生成libs目录后,您会注意到那里还有文件夹。这些新文件夹是您的build.gradle文件中的AAR依赖关系。



现在很酷的部分是当您将现有的Android项目导入Eclipse时,它还会将爆炸的AAR文件夹检测为可以导入的项目!



1。在项目的 libs 目录下导入这些文件夹,不要导入任何'build '文件夹,它们由Gradle生成



2。确保执行 项目 - >在您添加的所有AAR项目中清除 。在你的工作区,检查每个AAR的爆炸项目在project.properties中有如下:

  target = android-<你的安装SKD VERSION GOES HERE> 
android.library = true

3。主要的Android项目,你可以添加使用ADT的库引用,或者你可以只是编辑project.properties文件,并添加



android.libraries。 reference.1 = libs / someExplodedAAR /



4。现在,您可以右键单击主要的Android项目



但这甚至是什么意思?




  1. 这意味着您不需要任何Android AAR Gradle依赖项的源代码,以便在Eclipse中引用它的类和资源。


  2. 上面的gradle构建脚本使用AAR文件并准备在Eclipse中使用。一旦你把它添加到你的工作区,你就可以专注于实际的主要的Android项目。


  3. 现在,您可以使用Eclipse进行调试和开发,并使用ADT将其部署,并将其中的AAR依赖关系正确地捆绑在APK中。当你需要做一些特定的构建,那么你可以使用gradle。



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.

解决方案

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.

Gradle Build File

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')
        }
    }
}

Building with Gradle

Run :

"gradle clean build"

You should find all dependencies and exploded AARs in your libs directory. This is all Eclipse should need.

Importing in Eclipse

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.

But what does this even mean?

  1. 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.

  2. 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.

  3. 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.

这篇关于在Android项目中通过gradle添加依赖关系到eclipse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆