Android package the project into a jar package, androidjar
Package Android into a jar package for users. You can use the java reflection technology to obtain the corresponding id, so that users can easily replace what they need.
1. Create an Android project: ActivityLibrary. This is the project that needs to be packaged into a jar package:
2. The role of the MResource class is to use J2SE reflection technology to obtain the corresponding ID resources. The Android SDK also provides the method for obtaining Id resources: getResources (). getIdentifier ("main_activity", "layout", getPackageName ());
Check the MResource code:
Package com. example. activitylibrary; import android. content. context;/*** obtain its ID value based on the resource name * @ author howlaa */public class MResource {// obtain the ID public static int getIdByName (Context context, string className, String name) {String packageName = context. getPackageName (); Class r = null; int id = 0; try {r = Class. forName (packageName + ". R "); Class [] classes = r. getClasses (); Class desireClass = null; For (int I = 0; I <classes. length; ++ I) {if (classes [I]. getName (). split ("\ $") [1]. equals (className) {desireClass = classes [I]; break ;}} if (desireClass! = Null) id = desireClass. getField (name ). getInt (desireClass);} catch (ClassNotFoundException e) {e. printStackTrace ();} catch (IllegalArgumentException e) {e. printStackTrace ();} catch (SecurityException e) {e. printStackTrace ();} catch (IllegalAccessException e) {e. printStackTrace ();} catch (NoSuchFieldException e) {e. printStackTrace ();} return id;} // obtain the ID array public static int [] getIdsByNam E (Context context, String className, String name) {String packageName = context. getPackageName (); Class r = null; int [] ids = null; try {r = Class. forName (packageName + ". R "); Class [] classes = r. getClasses (); Class desireClass = null; for (int I = 0; I <classes. length; ++ I) {if (classes [I]. getName (). split ("\ $") [1]. equals (className) {desireClass = classes [I]; break;} if (desireClass! = Null) & (desireClass. getField (name). get (desireClass )! = Null) & (desireClass. getField (name ). get (desireClass ). getClass (). isArray () ids = (int []) desireClass. getField (name ). get (desireClass);} catch (ClassNotFoundException e) {e. printStackTrace ();} catch (IllegalArgumentException e) {e. printStackTrace ();} catch (SecurityException e) {e. printStackTrace ();} catch (IllegalAccessException e) {e. printStackTrace ();} catch (NoSuchFieldException e) {e. printStackTrace ();} return ids ;}}
3. Create activity_main.xml in layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_below="@+id/button1" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_below="@+id/textView1" android:layout_marginTop="28dp" android:src="@drawable/ic_launcher" /></RelativeLayout>
4. Check mainactivity:
package com.example.activitylibrary;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;import android.app.Activity;public class MainActivity extends Activity {String msg = "hello bitch"; @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(MResource.getIdByName(getApplication(), "layout", "activity_main"));TextView mTextView = (TextView) findViewById(MResource.getIdByName(getApplication(), "id", "textView1")); mTextView.setText(msg); Button mButton = (Button) findViewById(MResource.getIdByName(getApplication(), "id", "button1")); mButton.setText(msg); mButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getApplication(), msg, Toast.LENGTH_SHORT).show(); } }); }}
5. We compress the ActivityLibrary project into a Jar package. Right-click the project ---> Export ----> Java ---> JAR file ----> Next, as shown in figure
You only need the code in SRC. Remove others
6. Create a project APP2 and use the jar package. Copy the exported jar package to libs.
7. If we want to use the mainactivity interface in the previous project: We can have a jump code here:
public void btnClick(View view){Intent intent = new Intent(); intent.setClassName(getApplication(), "com.example.activitylibrary.MainActivity"); startActivity(intent); }
8. Register mainActivity of activitylibrary in mainfest. xml:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.app2" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.app2.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.example.activitylibrary.MainActivity"></activity> </application></manifest>
Complete.