Java 如何为 Android 开发创建自己的库,以便在您编写的每个程序中使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4085313/
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
How to create your own library for Android development to be used in every program you write?
提问by Edelcom
I am a Delphi programmer and have written, over the years, hundreds of classes and routines which I can use in every Delphi program I write.
我是一名 Delphi 程序员,多年来编写了数百个类和例程,我可以在我编写的每个 Delphi 程序中使用它们。
This libraryis called dlib and can be used in every Delphi programby putting this folder in my library path and using one of the units in the uses section of a Delphi unit.
该库称为 dlib,可以通过将此文件夹放在我的库路径中并使用 Delphi 单元的使用部分中的单元之一在每个 Delphi 程序中使用。
Being completely new to Java and Android development, I am wondering how to do this in similar way.
作为 Java 和 Android 开发的新手,我想知道如何以类似的方式做到这一点。
So my question, how can I write own classes, put them in some global folder, and use these classes and routines in every Android program I write?
所以我的问题是,如何编写自己的类,将它们放在某个全局文件夹中,并在我编写的每个 Android 程序中使用这些类和例程?
I know this is a basic question, which I can probably find out by searching Google and trying it out in Eclipse, but if someone can put me on the right track, I know I will save much time.
我知道这是一个基本问题,我可能可以通过搜索 Google 并在 Eclipse 中尝试找到它,但是如果有人能让我走上正轨,我知道我会节省很多时间。
Thanks.
谢谢。
采纳答案by Denis Palnitsky
You have to create Android Library Project. Create android project in Eclipse, enter Project Properties -> Android and check isLibrary property. Now you can add this library to your Android Application project by adding it to list on the same property page.
您必须创建 Android 库项目。在 Eclipse 中创建 android 项目,进入 Project Properties -> Android 并检查 isLibrary 属性。现在,您可以将此库添加到您的 Android 应用程序项目中,方法是将其添加到同一属性页上的列表中。
More detailed instructions herein Working with Library Projects section
更详细的说明这里的工作与库项目部
回答by Andreas Dolk
With java, you create a Java Archive (jar) that contains all your classes (*.class files) of that library and the jar file isyour library.
使用 java,您可以创建一个 Java 存档 (jar),其中包含该库的所有类(*.class 文件),而 jar 文件就是您的库。
To use it, simply add it to the classpath.
要使用它,只需将它添加到classpath。
(For "jar" and "classpath": basic Java concepts, please use google to find tutorials, you'll have to understand those concepts anyway, the sooner, the better ;) )
(对于“jar”和“classpath”:基本的Java概念,请使用google查找教程,无论如何您必须了解这些概念,越早越好;))
回答by jorge
If your library is in .java files composed of java code. There's a really detailed tutorial of how to use the library at mobile.tutsplus.com. Link below:
如果您的库位于由 java 代码组成的 .java 文件中。mobile.tutsplus.com 上有关于如何使用该库的非常详细的教程。下方链接:
For Example I wanted to use the Pull To Refresh library by Chrisbanes at Github.com here https://github.com/chrisbanes/Android-PullToRefresh/tree/master/library. The library's structure is in the form of an Android app. It has the form like below:
例如,我想使用 Chrisbanes 在 Github.com 上的 Pull To Refresh 库https://github.com/chrisbanes/Android-PullToRefresh/tree/master/library。该库的结构采用 Android 应用程序的形式。它的形式如下:
res/
src/
AndroidManifest.xml
pom.xml
project.properties
How to use on Eclipse:
如何在 Eclipse 上使用:
- Create new project in Eclipse. Give a name to your project. Select "Create project from existing source". Select the location of the root folder containing the above mentioned files in "Location". Select your target and click finish.
- Select properties of the newly project you created. Select "Android" option. Select the "Is Library" checkbox if it's not already selected. close properties.
- Add a reference to the library from the project that is going to use this library. Select your project that uses this library. Open Properties. Select "Android" option. At the bottom on the "Is Library". Don't select checkbox of "Is Library". Click "Add" button on the right. Your project that you created on step 1 and 2 should be listed ready for selection. select it and click apply. close properties.
- You're ready to reference the classes from your project.
- 在 Eclipse 中创建新项目。为您的项目命名。选择“从现有源创建项目”。在“位置”中选择包含上述文件的根文件夹的位置。选择您的目标并单击完成。
- 选择您创建的新项目的属性。选择“安卓”选项。如果尚未选中,请选中“是库”复选框。关闭属性。
- 从将要使用此库的项目中添加对库的引用。选择使用此库的项目。打开属性。选择“安卓”选项。在“是图书馆”的底部。不要选中“是库”复选框。单击右侧的“添加”按钮。您在第 1 步和第 2 步中创建的项目应列出以供选择。选择它并单击应用。关闭属性。
- 您已准备好从您的项目中引用类。
回答by Kumar Shorav
Convert all of your class in Java and make a jar file. Use this jar in your android project by copying in libs/ folder and then adding in to build path. Make a clean of project and then run it.
将所有类转换为 Java 并制作一个 jar 文件。通过在 libs/ 文件夹中复制然后添加到构建路径中,在您的 android 项目中使用此 jar。清理项目,然后运行它。
回答by BSMP
Instructions for creating a library in Android Studio:
Create a library module
To create a new library module in your project, proceed as follows:
Click File > New > New Module.
In the Create New Modulewindow that appears, click Android Library, then click Next.
There's also an option to create a Java Library, which builds a traditional JAR file. While a JAR file is useful for many projects—especially when you want to share code with other platforms—it does not allow you to include Android resources or manifest files, which is very useful for code reuse in Android projects. So this guide focuses on creating Android libraries.
Give your library a name and select a minimum SDK version for the code in the library, then click Finish.
Once the Gradle project sync completes, the library module appears in the Projectpanel on the left. If you don't see the new module folder, make sure it's displaying the Android view.
Convert an app module to a library module
If you have an existing app module with all the code you want to reuse, you can turn it into a library module as follows:
Open the module-level
build.gradle
file.Delete the line for the
applicationId
. Only an Android app module can define this.At the top of the file, you should see the following:
apply plugin: 'com.android.application'
Change it to the following:apply plugin: 'com.android.library'
Save the file and click Tools > Android > Sync Project with Gradle Files.
创建库模块
要在您的项目中创建新的库模块,请执行以下操作:
单击文件 > 新建 > 新建模块。
在出现的“创建新模块”窗口中,单击“Android 库”,然后单击“下一步”。
还有一个选项可以创建一个Java 库,它构建一个传统的 JAR 文件。虽然 JAR 文件对许多项目很有用——尤其是当你想与其他平台共享代码时——但它不允许你包含 Android 资源或清单文件,这对于 Android 项目中的代码重用非常有用。所以本指南着重于创建 Android 库。
为您的库命名并为库中的代码选择最低 SDK 版本,然后单击完成。
Gradle 项目同步完成后,库模块将出现在左侧的“项目”面板中。如果您没有看到新的模块文件夹,请确保它显示的是Android 视图。
将应用模块转换为库模块
如果您有一个包含所有要重用的代码的现有应用程序模块,您可以将其转换为库模块,如下所示:
打开模块级
build.gradle
文件。删除
applicationId
. 只有 Android 应用模块可以定义它。在文件的顶部,您应该看到以下
apply plugin: 'com.android.application'
内容:apply plugin: 'com.android.library'
将其更改为以下内容: 保存文件并单击Tools > Android > Sync Project with Gradle Files。