Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
736 views
in Technique[技术] by (71.8m points)

intellij idea - Android Libraries in Android Studio

Has anyone figured/found out how Android libraries are intended to work in Android studio?

I have not been able to find any documentation on this yet (the documentation on the Android Developer Site is incredibly bare-boned), and I observe that depending on how I create the library project, I get completely different results.

If I import a library from Eclipse (following the recommendations on the website to export to Gradle first) I end up with a new project + module containing the library project. This does not seem quite right (an intellij project = eclipse workspace), and attempts to compile/make this in intellij results in many errors (the library project can't find the android support libraries).

If I create a new project from scratch with a library module, then I get an android library project with a build.gradle file.

If I create a new module from within a project (using right click on the project), then I get an Android library built using Ant. This compiles fine, but seems very odd. Surely it is not intended that we should use Ant for Android libraries in android Studio? Creating a new library module using File > New Module doesn't seem to work, incidentally. It just creates a new application instead.

Has anyone picked up any information to make sense of this? I also haven't found any location where one can specify which libraries should be used in which application modules. I understand this is a preview release, but I'm having difficulty believing that a core feature like Android libraries is so poorly supported. What am I missing?

Update 2014-04-09:

So I took a new round with Android Studio this week. And while the problems now are different than when I originally wrote this, this is - quite incredibly - still a big problem. I've still not found a good explanation of using Android libraries in multiple projects in AS (including nested libs). Some of the suggestions I've seen recommend copying code into multiple locations - which completely defeats the purpose of having a library to begin with.

I just don't get what Google are thinking with Android Studio... It's a pity, because it seems like a great tool, but the lack of an intuitive handling something so basic as code reuse is a big issue for me (never mind that coding specifically for Android is becoming less and less attractive, when compared to the ever-improving cross-platform development tools).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want to use additional libraries in your Android Studio project:

  1. Copy your .jar library into libs folder in your project
  2. Right click on the library and choose "Add as library"
  3. In build.gradle replace

    compile files('libs/android-support-v4.jar')

with

compile fileTree(dir: 'libs', include: '*.jar')

If it still doesn't work, navigate to the root folder of your project with our terminal and run a gradlew clean command.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...