Android packages the project into a jar package using

Source: Internet
Author: User

Pack Android into a jar package for people to use, and use Java's reflection technology to get the appropriate ID. Easy for users to replace what they need.

1. New Android Project: Activitylibrary, this is the project that we need to package into a jar package:


The function of the 2.MResource class is to use J2SE reflection technology to obtain the corresponding ID resource. The Android SDK also provides a way to get the ID resource:getresources (). Getidentifier ("main_activity", "Layout", Getpackagename ());


Look at the code for Mresource:

Package Com.example.activitylibrary;import android.content.context;/** * gets its ID value based on the name of the resource * @author HOWLAA */public class Mresource {//Get ID Public by name public static int Getidbyname (context context, string ClassName, String name) {string P          Ackagename = 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; }//Get ID array public static int[] Getidsbyname (context context, string ClassName, String name) {String PA          Ckagename = 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 a new activity_main.xml under 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 "Androi        d: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 Oid:layout_alignparentright= "true" android:layout_below= "@+id/button1"/> <imageview android:id= "@+ Id/imageview1 "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "android:layou T_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. Look at the 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 (V              Iew v) {toast.maketext (Getapplication (), MSG, Toast.length_short). Show (); }          }); }}

5.we activitylibrary the project into a jar package. Right-click Project--->export---->java--->jar file---->next as



Just need the code inside SRC to be able. The other is removed

6. Create a new project APP2, using the jar package just now. Copy the jar package that you just exported 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 Activitylibrary's mainactivity 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= "/>" <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_n                Ame "> <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> </app Lication></manifest>

Complete.



Android packages the project into a jar package using

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.