Android Studio 中的 Android 库 [英] Android Libraries in Android Studio

查看:36
本文介绍了Android Studio 中的 Android 库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人想出/发现 Android 库是如何在 Android Studio 中工作的?

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

我还没有找到任何关于此的文档(Android 开发人员站点上的文档非常简单),我观察到根据我创建库项目的方式,我得到了完全不同的结果.

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.

如果我从 Eclipse 导入一个库(按照网站上的建议首先导出到 Gradle),我最终会得到一个包含库项目的新项目 + 模块.这似乎不太正确(intellij 项目 = eclipse 工作区),并且尝试在 intellij 中编译/制作它会导致许多错误(库项目找不到 android 支持库).

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).

如果我使用库模块从头开始创建一个新项目,那么我会得到一个带有 build.gradle 文件的 android 库项目.

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

如果我从一个项目中创建一个新模块(使用右键单击该项目),那么我将获得一个使用 Ant 构建的 Android 库.这编译得很好,但看起来很奇怪.当然,我们不应该在 android Studio 中将 Ant 用于 Android 库吗?顺便说一句,使用 File > New Module 创建一个新的库模块似乎不起作用.它只是创建了一个新的应用程序.

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.

有没有人找到任何信息来理解这一点?我也没有找到任何可以指定哪些库应该在哪些应用程序模块中使用的位置.我知道这是一个预览版本,但我很难相信像 Android 库这样的核心功能的支持如此之差.我错过了什么?

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?

2014-04-09 更新:

Update 2014-04-09:

所以我本周对 Android Studio 进行了新一轮的测试.虽然现在的问题与我最初写这篇文章时有所不同,但这仍然是一个大问题——非常令人难以置信.我仍然没有找到在 AS 中的多个项目(包括嵌套库)中使用 Android 库的好解释.我看到的一些建议建议将代码复制到多个位置 - 这完全违背了开始使用库的目的.

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.

我只是不明白谷歌对 Android Studio 的想法......很遗憾,因为它看起来是一个很棒的工具,但缺乏直观的处理代码重用等基本功能对我来说是一个大问题(与不断改进的跨平台开发工具相比,专为 Android 编写的代码变得越来越没有吸引力了.

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).

推荐答案

为了解决这个问题的人,这就是我正在做的解决"这个问题.IMO 与优雅的解决方案相去甚远,但它是我找到的最接近于使用 Git 在 Android Studio 中处理我的问题(重用 Android 库)的好的解决方案.

For the sake of anyone hitting on this, here's what I'm doing to "solve" this issue. It's very far from an elegant solution, IMO, but it's the closest to a good solution that I've found for handling my problem (reusing Android libraries) in Android Studio with Git.

基本思想是使用 Git 子模块.每个库都有自己的 Git 存储库.

Basic idea is to use Git submodules. Each library has it's own Git repository.

  1. 将 myLibrary 创建为模块,然后将其提交到存储库.
  2. 从项目根存储库,我执行一个 git submodule add 来获取库.
  3. 在 settings.gradle 中,我添加:include ':myLibrary' 并同步项目.
  4. 模块设置 >> 依赖项 >> + 模块依赖项并根据需要向模块添加依赖项.
  1. Create myLibrary as a module, and then commit it to a repository.
  2. From the project root repository, I do an git submodule add to fetch the library.
  3. In settings.gradle, I add: include ':myLibrary' and sync the project.
  4. Module Settings >> Dependencies >> + Module Dependency and add a dependency to the module as appropriate.

这与在 Eclipse 中使用 Android 库的简单优雅相去甚远,但它以一种时尚的方式工作.需要注意的事项:

It's a far cry from the simple elegance of working with Android libraries in Eclipse, but it works after a fashion. Things to keep in mind:

  • 记得经常使用 git pull 来获取更改(然后提交主项目以与子模块更改同步).
  • 如果你已经提交了对库的更改,请记住从模块目录到远程的 git push.

内置的 VCS 应该能够在这里提供帮助,但它对我来说很不稳定,所以我现在更喜欢自己从终端上做.

The built-in VCS should be able to help here, but it's been pretty flaky for me, so I prefer to just do it myself from a terminal now.

希望这会有所帮助.

这篇关于Android Studio 中的 Android 库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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