android 項目打包成Library

一、打包成Library:

 

android 項目打包成Library其實是比較簡單的,可以在新建項目時勾選Mark this project as a library,這樣這個項目就變成庫項目瞭,另外一種方式,可以點擊項目右鍵,點擊Properties,然後選Android,勾選Is Library,然後Apply,OK。

 

二、引用Library庫項目:

 

右鍵項目文件夾,點擊Properties,選擇Android,然後點擊Add,選擇你的庫項目,然後Apply,OK。

 

需要註意的是,在你的項目AndroidManifest.xml中仍然需要聲明庫項目中需要用到的activity、service、receiver等組件。

 

對於引用多個庫項目時,可以使用up和down可以設置他們的相對的優先級和合並順序,工具在合並引用的庫的時候順序是從低優先級(列表的下面)到高優先級(列表的上面), 應用程序自身擁有最高的優先級。那麼,如果不隻一個庫定義瞭相同的資源ID,這個工具選擇資源時會選擇高優先級的資源。

 

下面給出一個簡單的例子:

 

主項目的MainActivity:

 

[java]  

package com.home.testlibrary;  

  

import com.home.library.LibraryActivity;  

  

import android.app.Activity;  

import android.content.Intent;  

import android.os.Bundle;  

import android.view.View;  

import android.view.View.OnClickListener;  

import android.widget.Button;  

  

public class MainActivity extends Activity {  

  

    @Override  

    protected void onCreate(Bundle savedInstanceState) {  

        super.onCreate(savedInstanceState);  

        setContentView(R.layout.activity_main);  

        Button turnBtn = (Button) findViewById(R.id.main_btn);  

        turnBtn.setOnClickListener(new OnClickListener() {  

  

            @Override  

            public void onClick(View arg0) {  

                // 跳轉至Library庫項目  

                Intent intent = new Intent(MainActivity.this,  

                        LibraryActivity.class);  

                startActivity(intent);  

            }  

        });  

    }  

}  

主項目佈局XML:

 

[html]  

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"  

    android:layout_width="match_parent"  

    android:layout_height="match_parent" >  

  

    <Button  

        android:id="@+id/main_btn"  

        android:layout_width="match_parent"  

        android:layout_height="wrap_content"  

        android:text="跳轉至庫項目" />  

  

</LinearLayout>  

主項目的配置文件:

 

[html]  

 <activity  

    android:name="com.home.testlibrary.MainActivity"  

    android:label="@string/app_name" >  

    <intent-filter>  

        <action android:name="android.intent.action.MAIN" />  

  

        <category android:name="android.intent.category.LAUNCHER" />  

    </intent-filter>  

</activity>  

<activity   

    android:name="com.home.library.LibraryActivity">              

</activity>  

 

庫項目的LibraryActivity:

 

[java] 

package com.home.library;  

  

import android.os.Bundle;  

import android.app.Activity;  

  

public class LibraryActivity extends Activity {  

  

    @Override  

    protected void onCreate(Bundle savedInstanceState) {  

        super.onCreate(savedInstanceState);  

        setContentView(R.layout.activity_library);  

    }  

  

}  

其佈局文件:

 

[html]  

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"  

    android:layout_width="match_parent"  

    android:layout_height="match_parent">  

  

    <TextView  

        android:layout_width="wrap_content"  

        android:layout_height="wrap_content"  

        android:text="Library 項目" />  

  

</LinearLayout>  

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *