使用 Android Studio 的 Android 库项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21298658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Library Project using Android Studio
提问by kalehv
This question might have been answered somewhere but couldn't find the appropriate one.
这个问题可能在某处得到了回答,但找不到合适的答案。
I want to know how can I create a common utility library project in Android Studio. I want to write some common classes and some common methods within them to use in Android app projects. Probably like how .dll are in Windows world - a set of common methods that can be shared among multiple consumers.
我想知道如何在 Android Studio 中创建通用实用程序库项目。我想在其中编写一些常用类和一些常用方法,以便在 Android 应用程序项目中使用。可能就像 Windows 世界中的 .dll 一样 - 一组可以在多个使用者之间共享的通用方法。
Thanks in advance.
提前致谢。
采纳答案by pyus13
Simplest way to do this :
最简单的方法:
Right click on your opened project in Android Studio and select
New > Module
In the left Pane choose Android Library and click on next.
Enter all details, untick Create Activity, Theme and all if not required.
Choose API level same as your project and Next, Next, Next .
右键单击您在 Android Studio 中打开的项目并选择
New > Module
在左侧窗格中选择 Android 库,然后单击下一步。
输入所有详细信息,如果不需要,取消勾选创建活动、主题和所有内容。
选择与您的项目相同的 API 级别,然后选择 Next、Next、Next。
Now you will see an another directory inside your project, build.gradle
for library will be automatically configured for you.
现在您将在项目中看到另一个目录,build.gradle
因为库将自动为您配置。
If your module/library name is "mylibrary",
如果您的模块/库名称是"mylibrary",
include ':mylibrary'
will be automatically added in settings.gradlefile inside root directory of your project.
将自动添加到项目根目录下的settings.gradle文件中。
Now open your main module and insert this line in dependency block :
现在打开你的主模块并在依赖块中插入这一行:
compile project(':mylibrary')
If you want to use same library in other projects, you have to copy the library module to that particular project using File Explore and have to configure settings.gradleand main module's build.gradlemanually.
如果您想在其他项目中使用相同的库,则必须使用 File Explore 将库模块复制到该特定项目,并且必须手动配置settings.gradle和主模块的build.gradle。
回答by justinvdk
An old question, but even now there doens't seem to be a propper solution for libraries in AndroidStudio. I've been looking into making the migration step for some time now and this is what I found.
一个老问题,但即使是现在,AndroidStudio 中的库似乎也没有合适的解决方案。一段时间以来,我一直在考虑进行迁移步骤,这就是我发现的。
My 'solution'
我的“解决方案”
Lets say we've got a Library (called lib) which contains shared code and an Application project (called app) which wants to use said library's code.
假设我们有一个包含共享代码的库(称为 lib)和一个想要使用所述库代码的应用程序项目(称为 app)。
The key is defining a project in settings.gradle
in the Project's root (/Some/Path/MyProjects/Project/settings.gradle
)
This file should already exist and contain something like include ':app'
.
关键是settings.gradle
在项目的根目录中定义一个项目( /Some/Path/MyProjects/Project/settings.gradle
) 这个文件应该已经存在并且包含类似include ':app'
.
We will modify this file to also include the library AND define the library with the following two lines:
我们将修改此文件以包含库并使用以下两行定义库:
/Some/Path/MyProjects/Project/settings.gradle
/Some/Path/MyProjects/Project/settings.gradle
...
// tell gradle we want to include a module called 'lib'
include 'lib'
// tell gradle there is a module called 'lib', found in a specific directory
// Note the /app on the end, the project structure wraps a project around a module, you want to refer that module!
project (':lib').projectDir = new File('/Some/Path/MyProjects/Library/app')
...
Also edit the projects build.gradle /Some/Path/MyProjects/Project/app/build.gradle
to depend on the newly added module lib
还要编辑项目 build.gradle/Some/Path/MyProjects/Project/app/build.gradle
以依赖新添加的模块lib
/Some/Path/MyProjects/Project/app/build.gradle`
/Some/Path/MyProjects/Project/app/build.gradle`
...
dependencies {
...
compile project (':lib') // we want to depend on our library
}
...
Extra
额外的
When working with multiple developers or for the sake of flexibility, I use my gradle.properties
in my .gradle
directory (for *nix based systems usually found in homedir, not sure where Windows looks for it). Do notethat you might need to create the directory and file yourself.
当与多个开发人员一起工作时或为了灵活性,我gradle.properties
在我的.gradle
目录中使用 my (对于通常在 homedir 中找到的基于 *nix 的系统,不确定 Windows 在哪里查找它)。做笔记,你可能需要创建的目录和文件自己。
In this file you can create, if you like, constants that can be used by you throughout your gradle files. For example, mine contains something like the following:
在这个文件中,如果你愿意,你可以创建你可以在整个 gradle 文件中使用的常量。例如,我的包含如下内容:
theconstant=/Some/Path/MyProjects/Library/app
note the seemingly missing quotes (not sure whether thats really needed tho) Now we can replace
注意看似丢失的引号(不确定是否真的需要)现在我们可以替换
project (':lib').projectDir = new File('/Some/Path/MyProjects/Library/app')
with
和
project (':lib').projectDir = new File(theconstant)
Now you and your team could define this constant (which might differ per machine) and change the path accordingly.
现在您和您的团队可以定义这个常量(每台机器可能不同)并相应地更改路径。
pros
优点
- No copying of whole libraries anymore!
- Flexibility, you can edit the library in the project's window
- Multiple developers working on the same projects can define their own paths
- Library gets compiled at the projects' compile time (
BuildConfig.DEBUG
!)
- 不再复制整个图书馆!
- 灵活性,您可以在项目的窗口中编辑库
- 在同一项目上工作的多个开发人员可以定义自己的路径
- 库在项目的编译时编译(
BuildConfig.DEBUG
!)
cons
缺点
- None so far
- 目前没有
I havn't found the chance to properly test this through, yet this seems like the most elegant solution for the time being! I would like to hear your thoughts on this.
我还没有找到机会正确地测试它,但这似乎是目前最优雅的解决方案!我想听听您对此的看法。
回答by stefan
I'm just doing:
我只是在做:
include '..:commons'
in settings.gradle
在 settings.gradle 中
and:
和:
compile project(':..:commons')
in build.gradle of the referencing project
在引用项目的 build.gradle 中
so you can place your commonly used lib outside the project
这样您就可以将常用的库放在项目之外
回答by Bri6ko
Your build.gradle should look something like this
你的 build.gradle 应该是这样的
apply plugin: 'android-library'
repositories {
mavenCentral()
}
dependencies {
compile 'com.android.support:support-v4:18.0.0'
}
android {
compileSdkVersion 18
buildToolsVersion '18.1.1'
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
res.srcDirs = ['res']
}
}
}
Look at the first line
看第一行
回答by adityajones
when you create a new project in Android Studio, one of the options is "mark as library".
alternatively, just create the project like any other. then when using it elsewhere, in the library's gradle file make sure you change apply-plugin "android"
to apply-plugin "android-library"
and add it as a dependency to the app that's using it. For more on how to add the created project as a library, see here.
在 Android Studio 中创建新项目时,选项之一是“标记为库”。
或者,只需像其他项目一样创建项目。然后在其他地方使用它时,请确保在库的 gradle 文件中将其更改apply-plugin "android"
为apply-plugin "android-library"
并将其添加为正在使用它的应用程序的依赖项。有关如何将创建的项目添加为库的更多信息,请参见此处。
回答by Ojonugwa Jude Ochalifu
Simply create it as a normal project, if you want to use it as library open project structure
in the app that wants to use it and add the intended library under Dependencies
.
只需将其创建为普通项目,如果您想将其用作project structure
在想要使用它的应用程序中打开的库,并在Dependencies
.