使用Eclipse将Android项目打Library包

转载自:

http://blog.csdn.net/super_mt/article/details/50628208

本文是使用Eclipse将Android项目打成Library包提供给其它应用使用,Library包不用像Jar包一样注意资源引用的问题,Library包会将源码裸露给其它应用,资源文件复制到目标应用的相应位置即可,注意不要有重名。

1. 用Eclipse新建Android项目时会有Make this project as a library选项,如下图:



我们默认没有选择Make this project as a library选项,直接创建一个Android项目,名字为LibraryTest,该项目只有一个类LibraryTestActivity,定义了一个按钮,点击会弹出toast提示,内容如下

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.example.librarytest;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.Menu;  
  6. import android.view.MenuItem;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.Toast;  
  11.   
  12. public class LibraryTestActivity extends Activity {  
  13.     private Button bn;  
  14.   
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_librarytest);  
  19.         bn = (Button) findViewById(R.id.bn);  
  20.         bn.setOnClickListener(new OnClickListener(){  
  21.   
  22.             @Override  
  23.             public void onClick(View v) {  
  24.                 // TODO Auto-generated method stub  
  25.                 Toast.makeText(LibraryTestActivity.this"LibraryTest", Toast.LENGTH_SHORT).show();  
  26.             }  
  27.               
  28.         });  
  29.           
  30.     }  
  31. }  
该Activity对应的layout名为activity_librarytest.xml,只布局了一个Button按钮,layout的内容如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:orientation="vertical" >  
  5.   
  6.     <Button  
  7.         android:id="@+id/bn"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="LibraryTest" />  
  11.   
  12. </LinearLayout>  
将该项目编辑好了之后, 右键该项目,选择Properties--->Android--->Library,选中Is Library,点击Apply,OK 。如下图所示:


这样,该项目就成为了一个Library了。打开该项目下的project.properties文件,会看到存在android.library=true

(我们也可以选择该项目下的project.properties文件,打开,添加命令android.library=true,将该项目设为Library)

2. 新建一个Android测试项目,该项目名为LibraryDemo,该项目设置了一个按钮,点击按钮,会使用Intent打开上述Library的LibraryTestActivity。测试项目LibraryDemo的MainActivity.java的内容如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.example.librarydemo;  
  2.   
  3. import com.example.librarytest.LibraryTestActivity;  
  4.   
  5. import android.app.Activity;  
  6. import android.content.Intent;  
  7. import android.os.Bundle;  
  8. import android.view.Menu;  
  9. import android.view.MenuItem;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.Button;  
  13.   
  14. public class MainActivity extends Activity {  
  15.   
  16.     private Button bn;  
  17.   
  18.     @Override  
  19.     protected void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.activity_main);  
  22.         bn = (Button) findViewById(R.id.bn);  
  23.         bn.setOnClickListener(new OnClickListener() {  
  24.   
  25.             @Override  
  26.             public void onClick(View v) {  
  27.                 // TODO Auto-generated method stub  
  28.                 Intent intent = new Intent();  
  29.                 intent.setClass(MainActivity.this, LibraryTestActivity.class);  
  30.                 startActivity(intent);  
  31.             }  
  32.   
  33.         });  
  34.     }  
  35. }  
该Activity对应的布局文件和LibraryTestActivity的布局文件一样,如下所示:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent" >  
  4.   
  5.     <Button  
  6.         android:id="@+id/bn"  
  7.         android:layout_width="wrap_content"  
  8.         android:layout_height="wrap_content"  
  9.         android:text="@string/hello_world" />  
  10.   
  11. </RelativeLayout>  

编辑好了之后,右键该项目,选择Properties--->Android--->Library,点击Add,选择上文生成的Library,点击OK,Apply,OK。这样,在LibraryDemo的目录结构中,多出了一个Android Dependencies,里面有我们的librarytest.jar,可以看到里面的内容,里面只有.class文件,没有资源文件。(观察下测试项目LibraryDemo目录中的project.properties文件,发现多了android.library.reference.1=../LibraryTest)


3. 这样,我们就把library导入到测试项目中了,注意还需要将资源文件及AndroidManifest所需的内容放入测试项目,我需要将LibraryTest的activity_librarytest.xml放入测试项目LibraryDemo的layout目录中,并且在测试项目LibraryDemo的AndroidManifest.xml文件中添加activity声明:

<activity android:name="com.example.librarytest.LibraryTest"></activity>


4. 然后启动LibraryDemo,点击按钮,就可以进入LibraryTest生成的Library的LibraryTestActivity,点击里面的按钮,就会有toast提示。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值