转自 http://blog.chinaunix.net/uid-23392298-id-3322769.html
2014-02-20 12:37
281 查看
创建library供多个工程共享代码、资源是非常常见的需求,网上这种资料非常少,基本上都是讲创建java工程,然后export,这种方式缺点非常多,大家可以自己google一下。
本文着重介绍如何创建Android library,并且在工程中使用此library提供的资源,具体步骤如下:
1. 创建一个Android工程,命名为MyLib
2. 进入工程设置选中Is Library
3. 创建另一个Android工程,命名为MyProj
4. 进入工程设置,添加MyLib
5. 在MyProj的AndroidManifest.xml中加入对library中activity的引用
<activity android:name="net.devdiv.mylib.MyLib" />
6. 由于编译后library中的资源和引用它的project资源是合并在一起的,为了避免重名问题,需要对library中资源进行重命名
1). 把main.xml改为mylib.xml,同时修改MyLib.java代码setContentView(R.layout.mylib);
2). strings.xml修改为
<?xml version="1.0" encoding="utf-8"?>
< resources>
< string name="mylibhello">String fetched from lib!</string>
< string name="mylib_app_name">MyLib</string>
< /resources>
7. 在MyProj中引用MyLib的资源
package net.devdiv.myproj;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import net.devdiv.mylib.*;
import android.content.Intent;
public class MyProj extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView)findViewById(R.id.myprojtext);
tv.setText(R.string.mylibhello);
Intent it = new Intent(this, MyLib.class);
startActivity(it);
}
}
个人觉得应注意:自己定义的library用的权限或是声明,在要引用的工程中要做补充
创建library供多个工程共享代码、资源是非常常见的需求,网上这种资料非常少,基本上都是讲创建java工程,然后export,这种方式缺点非常多,大家可以自己google一下。
本文着重介绍如何创建Android library,并且在工程中使用此library提供的资源,具体步骤如下:
1. 创建一个Android工程,命名为MyLib
2. 进入工程设置选中Is Library
3. 创建另一个Android工程,命名为MyProj
4. 进入工程设置,添加MyLib
5. 在MyProj的AndroidManifest.xml中加入对library中activity的引用
<activity android:name="net.devdiv.mylib.MyLib" />
6. 由于编译后library中的资源和引用它的project资源是合并在一起的,为了避免重名问题,需要对library中资源进行重命名
1). 把main.xml改为mylib.xml,同时修改MyLib.java代码setContentView(R.layout.mylib);
2). strings.xml修改为
<?xml version="1.0" encoding="utf-8"?>
< resources>
< string name="mylibhello">String fetched from lib!</string>
< string name="mylib_app_name">MyLib</string>
< /resources>
7. 在MyProj中引用MyLib的资源
package net.devdiv.myproj;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import net.devdiv.mylib.*;
import android.content.Intent;
public class MyProj extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView)findViewById(R.id.myprojtext);
tv.setText(R.string.mylibhello);
Intent it = new Intent(this, MyLib.class);
startActivity(it);
}
}
个人觉得应注意:自己定义的library用的权限或是声明,在要引用的工程中要做补充
相关文章推荐
- linux系统/var/log目录下的信息详解 http://blog.chinaunix.net/uid-24250828-id-3198922.html
- epoll是如何监控多个描述符及如何获得通知(2) http://blog.chinaunix.net/uid-23629988-id-3575499.html?/gov-17311/
- 平衡树:2-3-4 Tree的实现与分析 http://blog.chinaunix.net/uid-23629988-id-3152495.html
- zookeeper基本原理及适用场景 转:http://blog.chinaunix.net/uid-26748613-id-4536290.html
- epoll是如何监控多个描述符及如何获得通知(1) http://blog.chinaunix.net/uid-23629988-id-3569332.html
- socket选项总结(setsocketopt) http://blog.chinaunix.net/uid-24517549-id-4044883.html
- curl 转自 http://blog.chinaunix.net/uid-22655387-id-3283161.html
- epoll模型详解 2010-10-09 17:39:38 http://blog.chinaunix.net/uid-20583479-id-1920065.html
- 浅析linux内核中timer定时器的生成和sofirq软中断调用流程(转自http://blog.chinaunix.net/uid-20564848-id-73480.html)
- zookeeper基本原理及适用场景 转:http://blog.chinaunix.net/uid-26748613-id-4536290.html
- 进程0 进程1 http://blog.chinaunix.net/uid-26874138-id-3183711.html
- 高性能网络I/O框架-netmap源码分析(1) http://blog.chinaunix.net/uid-23629988-id-3594118.html
- RawSocket(转载)来自http://blog.chinaunix.net/uid-23629988-id-85891.html
- 修饰函数和函数返回值的const的差别 http://blog.chinaunix.net/uid-12673432-id-2923858.html
- http://blog.chinaunix.net/uid-22666718-id-1771703.html
- Linux内核网络协议栈3-创建socket http://blog.chinaunix.net/uid-22359610-id-461249.html
- gentoo with gnome (来自 http://blog.chinaunix.net/uid-11009175-id-3549857.html)
- (转)一篇图片处理优化思路 原文地址http://blog.chinaunix.net/uid-20806919-id-132246.html
- fd_set具体是怎样实现的 http://blog.chinaunix.net/uid-20680966-id-1896524.html
- 解释器与编译器 (本文转自:http://blog.chinaunix.net/uid-22363424-id-113275.html)