首先,我知道这个问题的存在-如何将库项目添加到Android Studio?-不幸的是,它没有帮助我。
我的目标很简单。我想使用Android Studio IDE(0.2.11)和代码中的Guava库编写一个Android应用程序。
我不知道Gradle,我只是开始使用Android Studio,而我的Visual Studio / C#背景却使我失望了,对此我深表歉意(在那个米老鼠世界中,您通常只是添加一个库引用而已)。
我将用屏幕截图记录我的步骤。我大多遵循此答案中给出的建议。
我在libraries
项目文件夹下创建了一个文件夹。
我将Guava存储库克隆到其中。
文件成功出现。
我去Project Structure
选了Import Module
。
我选择Create module from existing sources
并同意所有默认选择。
我将settings.gradle
文件更新为include ':libraries:guava', ':Test'
:
和我的build.gradle
文件compile project(":libraries:guava")
:
但是,每当我尝试重建项目时,我得到的就是:
Error: Gradle: A problem occurred configuring project ':Test'.
> Failed to notify project evaluation listener.
> Configuration with name 'default' not found.
我确实尝试build.gradle
在guava
文件夹中放入如下内容:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android-library'
dependencies {
compile 'com.android.support:support-v4:13.0.+'
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
}
(如上述答案所述)。
我已经在谷歌上下搜索以找到build.gradle
番石榴的“正确”字样。
https://code.google.com/p/guava-libraries/wiki/UseGuavaInYourBuild - didn't help me, either.
I did try countless things which I will not describe here as they were rather haphazard - I tried adding a module dependency, I tried turning Use auto-import
on in Gradle settings
, etc.
I know it's not a way of solving issues and I promise I will diligently read Gradle's User Guide from 1 through 5.4.2 to 7.3, but I can't believe this is really prerequisite to achieve something as unremarkable as merely adding a library to a project? Why is there no default build.gradle
file from which one could start to fiddle with all sorts of things if necessary?
And the real question - how do I create an app (in Android Studio) that builds, actually runs on an Android device and on the top of that allows me to use Guava so I could sort a map by values without writing 50 lines of code? :)
Sorry about the chatty tone of my question, I know the drill around here, it's just my way of venting my frustration off.
Judging by how many votes were casted for questions and answers that tackled similar issues, I'm sure I'm not the only one who would benefit from some more instructions. I would start a bounty on it straight away, but the rules forbid me.
If you just need to use a stable, released version of the Guava libraries, importing it is extremely easy.
Just go to the build.gradle
file of the module where you want to use the library (i.e GuavaTestProject/GuavaTest/build.gradle
) and, right after
repositories {
mavenCentral()
}
add a Maven dependency:
dependencies {
compile group: 'com.google.guava', name: 'guava', version: '15.0'
}
Rebuild your project if needed and that's all (tested right now with a fresh project created with Android Studio 0.2.13).
If you really need to include the source code of the Guava library and compile it yourself as a module of your Gradle build that's an entirely different problem because Guava is build with Maven and so you need to move the Guava build system from Maven to Gradle, which I think is overwhelmingly complex for your goals.
If you just need to browse the source or view it while debugging, what I would do is:
Download Guava source code on a separate folder:
git clone https://code.google.com/p/guava-libraries/
git checkout v15.0
When Android Studio doesn't find the sources, click on "Attach sources" and point to this alternative location.
我认为,如果您不需要实际修改和编译Guava源代码,这是最简单的解决方案。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句