通过摇篮加入依赖于Android项目日食 [英] Add dependencies via gradle to eclipse in android project

查看:99
本文介绍了通过摇篮加入依赖于Android项目日食的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题会自动添加依赖通过摇篮到Eclipse Android项目。 我只有一点点经验摇篮。直到现在我都建了两个Java项目摇篮。其中一个罐子和一个可执行的-JAR。这工作没有任何问题。 我已经使用了Eclipse插件生成Eclipse项目,并添加dependenies到构建路径。我添加新的依赖于摇篮剧本,开始用摇篮月食摇篮,更新我的项目,存在于构建路径的依赖,我可以用它们。下面是该脚本的重要组成部分。

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

所以,现在我想它结合了Android插件。这里是我的洞摇篮的脚本。

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

如果我用摇篮日食没有任何反应。后来我发现了Java插件添加的依赖关系到构建路径。所以我加了

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'

它和得到了错误的Ja​​va插件不与Android插件兼容。 然后我找到了一个解决方案,以自动复制罐子到项目的lib文件夹。

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
}

不过这个任务需要Java插件也为configurations.runtime。 我需要的Andr​​oid插件创建的apk文件,所以它不是一个解决方案,以除去Android插件。 有人来过如果有可能的依赖关系添加到构建路径或的Ecipse项目的lib文件夹与Android插件兼容的想法?

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?

编辑: 我的一个想法是把java的插件到Eclipse,插件,这样,当eclipse插件将其应用将只适用。事情是这样的:

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

但我仍然得到错误的Ja​​va和Android插件不兼容。 也许我理解错的摇篮,但通常Java插件应该当我启动eclipse插件,而不是Android插件只适用。只怕我的理解和摇篮的体验不够好解决这个这样或理解为什么它是不可能的。

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.

推荐答案

我的解决方案是基于关闭拉斐尔的,它副本相关性,这是只有使用Android的libs目录。不过我走的更远,完全爆炸所引用的AAR的在Eclipse中使用。

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.

以下内容添加到你的Andr​​oid项目的结束build.gradle:

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

建筑与摇篮

运行:

摇篮干净的构建

您会发现在你的库目录中的所有依赖关系,并发生爆炸AARS。这是所有的Eclipse应该需要。

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

现在这才是真正的利益开始的地方。当你从摇篮步骤中产生的libs目录上面,你会发现里面还有太多的文件夹。这些新的文件夹是从你的build.gradle文件中的爆炸AAR的依赖。

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.

现在凉爽的部分是,当你导入现有的Andr​​oid项目到Eclipse也将随着项目它可以​​导入过检测爆炸AAR文件夹!

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 导入项目的目录下这些文件夹,没有导入任何'打造'的文件夹,它们是由摇篮

1. Import those folders under your project's libs directory, don't import any 'build' folders, they're generated by Gradle

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

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。现在在主Android项目,你可以只用添加任何ADT库引用也可以直接编辑project.properties文件,并添加

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 =库/ someExplodedAAR /

4 现在你可以在你的主要的Andr​​oid项目右键单击和运行方式 - > Android应用程序

4. Now you can right-click on your main Android project and Run as -> Android Application.

  1. 那么就意味着你不需要为了源$ C ​​$下您的任何Android的AAR摇篮的依赖同时引用它的类和资源在Eclipse中。

  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.

在摇篮构建脚本上面花费的AAR文件和prepares它在Eclipse中使用。一旦你把它添加到您的工作区,你就可以只专注于您的实际主要的Andr​​oid项目。

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.

您现在可以调试和开发使用Eclipse和部署使用ADT与AAR的依赖关系被正确捆绑在APK。当你需要做出一些具体的构建,那么你可以使用摇篮。

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项目日食的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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