Question about resource ID when Android exports lib

Source: Internet
Author: User

When you needAndroidWhen exporting as a jar package, if it is useful to resources, such as R. layout and R. IDProgramWhen this package is introduced, the ID cannot be found.

This is because the R. ID in the jar has been replaced with an int constant during export, and the resource ID will be re-generated in the new program. If the two do not match, a problem will occur.

So hereAndroidThe import com. XX. r cannot be used in the project. Otherwise, a problem may occur. You must use another function to obtain its real ID when using the resource.

The solution is to use the Java reflection mechanism to locate the class through string and locate the variable value.

Method 1
Public static int GETID (context paramcontext, string paramstring1, string paramstring2) {try {class localclass = Class. forname (paramcontext. getpackagename () + ". r$ "+ paramstring1); field localfield = localclass. getfield (paramstring2); int I = integer. parseint (localfield. get (localfield. getname ()). tostring (); return I;} catch (exception localexception) {log. E ("getidbyreflection error", localexception. getmessage ();} return 0 ;}
Method 2

This is also the method to replace theme.

Public static int getlayoutresidbyname (context, string name) {return context. getresources (). getidentifier (name, "layout", context. getpackagename ();} public static int getidresidbyname (context, string name) {return context. getresources (). getidentifier (name, "ID", context. getpackagename ();} public static int getstringresidbyname (context, string name) {return context. getresources (). getidentifier (name, "string", context. getpackagename ();} public static int getdrawableresidbyname (context, string name) {return context. getresources (). getidentifier (name, "drawable", context. getpackagename ();} public static int getrawresidbyname (context, string name) {return context. getresources (). getidentifier (name, "Raw", context. getpackagename ());}
Method 3

AndroidProjects are mutually dependent and only applicable to development in eclipse.

Refer to official instructions for http: // developer.Android. Com/GUIDE/developing/projects/projects-eclipse.html

Related Article

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.