写自己的ANDROIDSDK 参考资料1
2013-10-17 15:26
267 查看
http://www.vogella.com/articles/AndroidLibraryProjects/article.html
Copyright © 2011, 2012, 2013 Lars Vogel
03.06.2013
Android Library Projects
This tutorial describes how to create and use library projects in Android. The tutorial is based on Eclipse 4.2, Java 1.6 and Android 4.2.
Table of Contents
1. Android library projects
1.1. Motivation1.2. Creating and using library projects1.3. Priorities for conflicting resources
2. Using JAR files in Android3. Prerequisite4. Tutorial: Create Android library projects
4.1. Create library project4.2. RSS - Really Simple Syndication4.3. Create Model class4.4. Create instances4.5. Set as library project4.6. Use library project
5. Thank you6. Questions and Discussion7. Links and Literature
7.1. Source Code7.2. Android Resources7.3. vogella Resources
1. Android library projects
1.1. Motivation
Android library projects allow to store source code and resources which are used by several other Android projects. The Android development tools compile the content of library into the Android project by creating a JAR file.
Library projects cannot be compiled to Android applications directly.
Using library projects help you to structure your application code. Also more and more important Open Source libraries are available for Android. Understanding library projects is therefore important for every Android programmer.
1.2. Creating and using library projects
If the Android development tools build a project which uses a library project, it also builds the components of the library and adds them to the
Therefore a library project can be considered to be a compile-time artifact. A Android library project can contain Java classes, Android components and resources. Only assets are not supported.
To create a library project, set the Mark this project as library flag in the Android project generation wizard.
To use such a library, select the generated project, right-click on it and select properties. On the Android tab add the library project to it.
The library project must declare all its components, e.g.
activities, service, etc. via the
Other projects can now use this library project. This can also be set via the properties of the corresponding project.
1.3. Priorities for conflicting resources
The Android development tools merges the resources of a library project with the resources of the application project. In the case that a resources ID is defined several times, the tools select the resource from the application, or the library with highest
priority, and discard the other resource.
2. Using JAR files in Android
To use a Java library inside your Android project directly, you can create a folder called
3. Prerequisite
The following example assumes that you have created a normal Android project called
com.example.android.rssfeed based on
Android Fragments tutorial .
4. Tutorial: Create Android library projects
4.1. Create library project
Create a new Android project called com.example.android.rssfeedlibrary. Do not need to create an
activity.
Our library project will not contribute Android components but a data model and a method to get the number of instances. We will provide RSSfeed data. The following gives a short introduction into RSS.
4.2. RSS - Really Simple Syndication
An RSS document is an XML file which can be used to publish blog entries and news. The format of the XML file is specified via the RSS specification.
RSS stands for Really Simple Syndication (in version 2.0 of the RSS specification).
Typically a RSS file is provided by a web server, which RSS client read. These RSS clients parse the file and display it.
4.3. Create Model class
Create an
Use Eclipse code generation capabilities which can be found in the menu under
Source to generate the getters and setter, the constructor and a
4.4. Create instances
Create a new class called
Solve the TODOs to create example instances of the
4.5. Set as library project
Right-click on the Android project and select Properties. Ensure that the
is Library flag is set.
4.6. Use library project
In your application project defines that you want to use the library project via the project properties.
Use the static method of
To send the new data change your
6. Questions and Discussion
If you find errors in this tutorial, please notify me (see the top of the page). Please note that due to the high volume of feedback I receive, I cannot answer questions to your implementation. Ensure you have read the
vogella FAQ as I don't respond to questions already answered there.
7. Links and Literature
7.2. Android Resources
Android Tutorial
Android Location API and Google Maps
Android and Networking
7.3. vogella Resources
vogella Training Android and Eclipse Training from the vogella team
Android Tutorial Introduction to Android Programming
GWT Tutorial Program in Java, compile to JavaScript and HTML
Eclipse RCP Tutorial Create native applications in Java
JUnit Tutorial Test your application
Git Tutorial Put all your files in a distributed version control system
Android Library Projects - Tutorial
Lars Vogel
Version 1.3Copyright © 2011, 2012, 2013 Lars Vogel
03.06.2013
Revision History | |||
---|---|---|---|
Revision 0.1 | 16.03.2012 | Lars Vogel | Created |
Revision 0.2 - 1.3 | 16.04.2012 - 03.06.2013 | Lars Vogel | bugfixes and updates |
This tutorial describes how to create and use library projects in Android. The tutorial is based on Eclipse 4.2, Java 1.6 and Android 4.2.
Table of Contents
1. Android library projects
1.1. Motivation1.2. Creating and using library projects1.3. Priorities for conflicting resources
2. Using JAR files in Android3. Prerequisite4. Tutorial: Create Android library projects
4.1. Create library project4.2. RSS - Really Simple Syndication4.3. Create Model class4.4. Create instances4.5. Set as library project4.6. Use library project
5. Thank you6. Questions and Discussion7. Links and Literature
7.1. Source Code7.2. Android Resources7.3. vogella Resources
1. Android library projects
1.1. Motivation
Android library projects allow to store source code and resources which are used by several other Android projects. The Android development tools compile the content of library into the Android project by creating a JAR file.Library projects cannot be compiled to Android applications directly.
Using library projects help you to structure your application code. Also more and more important Open Source libraries are available for Android. Understanding library projects is therefore important for every Android programmer.
1.2. Creating and using library projects
If the Android development tools build a project which uses a library project, it also builds the components of the library and adds them to the.apkfile of the compiled application.
Therefore a library project can be considered to be a compile-time artifact. A Android library project can contain Java classes, Android components and resources. Only assets are not supported.
To create a library project, set the Mark this project as library flag in the Android project generation wizard.
To use such a library, select the generated project, right-click on it and select properties. On the Android tab add the library project to it.
The library project must declare all its components, e.g.
activities, service, etc. via the
AndroidManifest.xmlfile. The application which uses the library must also declare all the used components via the
AndroidManifest.xmlfile.
Other projects can now use this library project. This can also be set via the properties of the corresponding project.
1.3. Priorities for conflicting resources
The Android development tools merges the resources of a library project with the resources of the application project. In the case that a resources ID is defined several times, the tools select the resource from the application, or the library with highestpriority, and discard the other resource.
2. Using JAR files in Android
To use a Java library inside your Android project directly, you can create a folder calledlibsand place your JAR into this folder.
Tip
If you call the folderlibs, the Android Developer Tools automatically add the JAR file to the classpath of your project.
3. Prerequisite
The following example assumes that you have created a normal Android project calledcom.example.android.rssfeed based on
Android Fragments tutorial .
4. Tutorial: Create Android library projects
4.1. Create library project
Create a new Android project called com.example.android.rssfeedlibrary. Do not need to create anactivity.
Our library project will not contribute Android components but a data model and a method to get the number of instances. We will provide RSSfeed data. The following gives a short introduction into RSS.
4.2. RSS - Really Simple Syndication
An RSS document is an XML file which can be used to publish blog entries and news. The format of the XML file is specified via the RSS specification.RSS stands for Really Simple Syndication (in version 2.0 of the RSS specification).
Typically a RSS file is provided by a web server, which RSS client read. These RSS clients parse the file and display it.
4.3. Create Model class
Create an RssItemclass which can store data of an RSS entry.
package com.example.android.rssfeedlibrary; public class RssItem { private String pubDate; private String description; private String link; private String title; }
Use Eclipse code generation capabilities which can be found in the menu under
Source to generate the getters and setter, the constructor and a
toString()method. The result should look like the following class.
package com.example.android.rssfeedlibrary; public class RssItem { private String pubDate; private String description; private String link; private String title; public RssItem() { } public RssItem(String title, String link) { this.title = title; this.link = link; } public String getPubDate() { return pubDate; } public void setPubDate(String pubDate) { this.pubDate = pubDate; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getLink() { return link; } public void setLink(String link) { this.link = link; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } @Override public String toString() { return "RssItem [title=" + title + "]"; } }
4.4. Create instances
Create a new class called RssFeedProviderwith a static method to return a list of
RssItemobjects.
package com.example.android.rssfeedlibrary; import java.util.ArrayList; import java.util.List; public class RssFeedProvider { // Helper method to get a list // of RssItems public static List<RssItem> parse(String rssFeed) { List<RssItem> list = new ArrayList<RssItem>(); // create some example data RssItem item = new RssItem("test1", "l1"); list.add(item); item = new RssItem("test2", "l2"); list.add(item); // TODO Create a few more instances of RssItem return list; } }
Solve the TODOs to create example instances of the
RssItemclass and add it to the list. This method does currently only return test data.
4.5. Set as library project
Right-click on the Android project and select Properties. Ensure that theis Library flag is set.
4.6. Use library project
In your application project defines that you want to use the library project via the project properties.Use the static method of
RssFeedProviderto get the list of
RssItemobjects and display the number in your
DetailFragmentinstead of current system time.
To send the new data change your
MyListFragmentclass.
// Update the method updateDetail() { // more code.... // reading the RSS items List<RssItem> list = RssFeedProvider .parse("http://www.vogella.com/article.rss"); String text = String.valueOf(list.size()); // TODO send text to the Detail fragment
5. Thank you
Please help me to support this article:6. Questions and Discussion
If you find errors in this tutorial, please notify me (see the top of the page). Please note that due to the high volume of feedback I receive, I cannot answer questions to your implementation. Ensure you have read thevogella FAQ as I don't respond to questions already answered there.
7. Links and Literature
7.1. Source Code
Source Code of Examples7.2. Android Resources
Android TutorialAndroid Location API and Google Maps
Android and Networking
7.3. vogella Resources
vogella Training Android and Eclipse Training from the vogella teamAndroid Tutorial Introduction to Android Programming
GWT Tutorial Program in Java, compile to JavaScript and HTML
Eclipse RCP Tutorial Create native applications in Java
JUnit Tutorial Test your application
Git Tutorial Put all your files in a distributed version control system
相关文章推荐
- 自己动手写操作系统之参考资料
- iOS 远程推送参考资料及自己遇到的问题
- git参考资料,及自己整理的命令
- 把世界杯积分榜加入到自己的blog中来
- [C#] 自己封装的一个数据库访问类 让ado.net 用起来和ado一样
- 该为自己的框架定个名"铂系框架"
- 自己可以原谅自己!了解自己
- 德鲁克:管理自己
- win8平板App - 参考资料
- 自己创建的一个ORM框架
- 动手实现自己的 STL 容器 《1》---- vector
- 如何创建自己的第三方SDK(环境搭建)
- Mr.Cao 提出的百度面试题,以及自己从网上找的答案
- flume 自己定义 hbase sink 类
- 为什么人们经常认为自己是受害者?
- 如何将自己的文件上传至github-从注册账号说起
- 自己定义控件三部曲视图篇(一)——測量与布局
- 搭建一个自己的导航网页
- 正确面对移动解决方案-给自己一点小小的打击!
- 打造自己的专业图像工具-Visual C++ 2005图像编程系列【五】(中)