我在通过 gradle 自动添加依赖项到 eclipse android 项目时遇到问题。 我对 gradle 只有一点点经验。到目前为止,我已经用 gradle 构建了两个 java 项目。一个 jar 和一个可执行 jar。这没有问题。 我已经使用 eclipse 插件生成 eclipse 项目并将依赖项添加到构建路径。我向 gradle 脚本添加了新的依赖项,使用 gradle eclipse 启动了 gradle,更新了我的项目,依赖项存在于构建路径中,我可以使用它们。这是该脚本的重要部分。
apply plugin: 'java'
apply plugin: 'eclipse'
repositories {
mavenCentral()
}
dependencies {
compile 'commons-io:commons-io:2.4'
}
所以,现在我尝试将它与 android 插件结合使用。这是我的 hole gradle 脚本。
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')
}
}
如果我使用 gradle eclipse,什么也不会发生。然后我发现java插件将依赖项添加到构建路径中。所以我加了
apply plugin: 'java'
然后得到java插件与android插件不兼容的错误。 然后我找到了一个解决方案,将jar自动复制到项目的lib文件夹中。
def libDir = file('libs')
task copyLibs(type: Copy) {
doFirst {
libDir.mkdirs()
}
from configurations.runtime
into libDir
}
但是这个任务也需要 java 插件用于 configurations.runtime。 我需要 android 插件来创建 apk 文件,所以它不是删除 android 插件的解决方案。 有人知道是否可以将依赖项添加到与 android 插件兼容的 ecipse 项目中的构建路径或 lib 文件夹中吗?
编辑: 我的想法之一是将 java-plugin 放到 eclipse-plugin 中,这样它只会在应用 eclipse 插件时应用。像这样:
apply plugin: 'eclipse'
eclipse{
apply plugin: 'java'
}
但我仍然得到java和android插件不兼容的错误。 也许我对 gradle 的理解有误,但通常只有在我启动 eclipse 插件而不是 android 插件时才应应用 java 插件。恐怕我对 gradle 的理解和经验不足以以这种方式解决这个问题或理解为什么这是不可能的。
最佳答案
我的解决方案基于 Rafael's因为它将依赖项复制到仅供 Android 使用的 libs 目录。然而,我进一步完全分解了在 Eclipse 中使用的引用 AAR。
将以下内容添加到您的 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 clean build”
您应该在 libs 目录中找到所有依赖项和展开的 AAR。这就是 Eclipse 应该需要的全部内容。
现在这就是真正的好处开始的地方。从上面的 gradle 步骤生成 libs 目录后,您会注意到那里也有文件夹。这些新文件夹是来自您的 build.gradle 文件的展开的 AAR 依赖项。
现在很酷的部分是,当您将现有的 Android 项目导入 Eclipse 时,它还会检测展开的 AAR 文件夹作为它也可以导入的项目!
1. 在项目的 libs 目录下导入这些文件夹,不要导入任何“构建”文件夹,它们是由 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=libs/someExplodedAAR/
4. 现在您可以右键单击您的主要 Android 项目并运行方式 -> Android 应用程序。
嗯,这意味着您不需要任何 Android AAR Gradle 依赖项的源代码即可在 Eclipse 中引用它的类和资源。
上面的 gradle 构建脚本获取 AAR 文件并准备好在 Eclipse 中使用。将它添加到工作区后,您就可以专注于实际的主要 Android 项目了。
您现在可以使用 Eclipse 进行调试和开发,并使用 ADT 进行部署,AAR 依赖项已正确 bundle 在 APK 中。当您需要进行一些特定的构建时,您可以使用 gradle。
关于android - 在android项目中通过gradle添加依赖到eclipse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16709305/
当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它
我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以
我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘
我已经像这样安装了一个新的Rails项目:$railsnewsite它执行并到达:bundleinstall但是当它似乎尝试安装依赖项时我得到了这个错误Gem::Ext::BuildError:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcheckingforlibkern/OSAtomic.h...yescreatingMakefilemake"DESTDIR="cleanmake"DESTDIR="
我正在尝试修改当前依赖于定义为activeresource的gem:s.add_dependency"activeresource","~>3.0"为了让gem与Rails4一起工作,我需要扩展依赖关系以与activeresource的版本3或4一起工作。我不想简单地添加以下内容,因为它可能会在以后引起问题:s.add_dependency"activeresource",">=3.0"有没有办法指定可接受版本的列表?~>3.0还是~>4.0? 最佳答案 根据thedocumentation,如果你想要3到4之间的所有版本,你可以这
我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("
假设我有这个范围:("aaaaa".."zzzzz")如何在不事先/每次生成整个项目的情况下从范围中获取第N个项目? 最佳答案 一种快速简便的方法:("aaaaa".."zzzzz").first(42).last#==>"aaabp"如果出于某种原因你不得不一遍又一遍地这样做,或者如果你需要避免为前N个元素构建中间数组,你可以这样写:moduleEnumerabledefskip(n)returnto_enum:skip,nunlessblock_given?each_with_indexdo|item,index|yieldit
当谈到运行时自省(introspection)和动态代码生成时,我认为ruby没有任何竞争对手,可能除了一些lisp方言。前几天,我正在做一些代码练习来探索ruby的动态功能,我开始想知道如何向现有对象添加方法。以下是我能想到的3种方法:obj=Object.new#addamethoddirectlydefobj.new_method...end#addamethodindirectlywiththesingletonclassclass这只是冰山一角,因为我还没有探索instance_eval、module_eval和define_method的各种组合。是否有在线/离线资