微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Android项目中通过gradle添加依赖关系到eclipse

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

dependencies {
   compile 'commons-io:commons-io:2.4'
}

所以,现在我结合使用android插件.这是我的洞毕业脚本.

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插件将附件添加到构建路径.所以我补充说

apply plugin: 'java'

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

def libDir = file('libs')

task copyLibs(type: copy) {

    doFirst {
        libDir.mkdirs()
    }
    from configurations.runtime
    into libDir
}

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

编辑:
我的一个想法是把java插件放到eclipse插件上,这样它只适用于eclipse插件.这样的事情

apply plugin: 'eclipse'

eclipse{
    apply plugin: 'java'
}

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

解决方法

我的解决方案基于 Rafael’s,因为它将依赖关系复制到仅由Android使用的libs目录.不过我会进一步彻底地爆炸引用的AAR在Eclipse中的使用.

Gradle构建文件

将以下内容添加到Android项目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')
        }
    }
}

建筑与Gradle

跑 :

“毕业干净”

您应该在libs目录中找到所有的依赖项和分解的AAR.这就是Eclipse应该需要的.

在Eclipse中导入

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

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

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

2.确保执行项目 – >清理您添加的所有AAR项目.在您的工作区中,检查每个AAR爆炸项目在project.properties中具有以下内容

target=android-<YOUR INSTALLED SKD VERSION GOES HERE>
android.library=true

3.现在在您的主要Android项目中,您可以使用ADT添加库引用,也可以编辑project.properties文件添加

android.libraries.reference.1 =库/ someExplodedAAR /

4.现在您可以右键单击您的主要Android项目并运行为 – > Android应用程序

但这甚至是什么意思?

>这意味着您不需要任何Android AAR Gradle依赖项的源代码,以便在Eclipse中引用它的类和资源.>上面的gradle构建脚本使用AAR文件并准备在Eclipse中使用.一旦你把它添加到你的工作区,你就可以专注于实际的主要的Android项目.>您现在可以使用Eclipse进行调试和开发,并使用ADT进行部署,并将AAR依赖关系正确地捆绑在APK中.当你需要做一些特定的构建,那么你可以使用渐变.

原文地址:https://www.jb51.cc/android/312007.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐