Android packaging Jar (post SDK) considerations and problem solving

Source: Internet
Author: User
Tags reflection

During the Android development process, we often have this need to package ourselves a class library as a jar package for others (other people generally referred to as developers), rather than packaging apk files directly for end users to use. After packaging into a jar, you will often test yourself to see if the jar bag can be used directly, and then it will often be like a few questions:

1) Error ... Found duplicate file for apk:res/drawable-xxx/xxx.xx

2) Res. Resources$notfoundexception:xxx id#0x7f090015

3) Java.lang.NullPointerException

4) Java.lang.noclassdeffounderror:com.xxx.xxx.r$layout

A few of the problems are common because resource files are at mischief.

In the first question, a resource file is packaged into a jar, a resource file with the same ID in the target using project, and a conflict, and the solution is to not package the resource files, pack them in, and then mention them later. Perhaps you mentioned that the resource IDs in SRC Project, which provides a jar package, could be changed to solve the problem superficially.

Do not package resource files, but SRC project also references res resource files, even if you copy the corresponding layout XML, String.xml, and so on to target project, it can also cause such as res.resources$ Notfoundexception or java.lang.NullPointerException, because some activity in SRC calls resources in the XML, the control ID cannot be found, such as a btn = Findviewbyid ( R.ID.BTNXXX); This btn is empty, it will nullpointerexception. The reason for this problem is that some people give a reasonable explanation on StackOverflow.

As you are want to import the Resources,since Android makes R class automatically with files Under/res folder, Usin G R class as Final static is impossible.in your source code which would be exported in jar file, DON ' T use R variable Becau Se It is replaced with the final static memory address in compile time. Instead of using R use method below ...

SRC Project Export src folder into a jar, does not contain resource files, resource files copy to the other projects to be referenced, but SRC can not get the layout of the XML through R.ID.XXX. Because the final int dies when packaged into class (see Gen/xxx.xx.r.java), it does not correspond in the new project. The solution is to use a reflection method. The code is as follows.

public static int Getresourseidbyname (string packagename, String className, string name)
{
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;

}

Then take advantage of int id = getresourceidbyname (context.getpackagename (), "layout", "main"), get main.xml configuration layout under Layout folder, through int id = Getresourceidbyname (Context.getpackagename (), "string", "Text1″"); Gets the string that is Text1 under the String.xml key, via int id = Getresourceidbyname (Context.getpackagename (), "id", "btn") gets the control with the ID btn, and so on. This code is found using reflection runtime.
Of course in the specific application process you can according to the above method of refactoring to remove the cycle to find the configuration and modify into a separate number of ID (key), Dimen (key), Color (key), and so on. Here is inconvenient (the work need) to give the source code of course also does not need to give out.

Online said using the library method to load a resource file can be resolved, will be SRC project as the library, target project reference src Project. Then say that this approach solves the problem of obtaining resources through R.XX.XX, but there is also a problem that it is not possible to release the source code for others to use. I tried it, and in my project, the control that seemed to get through r.xx.xx was still null, leaving a blank pointer exception.

But this provides me with a way of thinking about how to provide an SDK for a more convenient solution. Because the above mentioned kind of need manual copy layout and so on XML file. If you use the library, you can avoid copy of this operation. The concrete solution is that the same SRC project is packaged into Lib.jar, and then manually build a library project as Middle project, This project adds Lib.jar and copy the corresponding XML file to this project. NEBULA_SDK in the following figure, and then publish NEBULA_SDK this project to others, which satisfies both functional requirements and also makes the code less prone to leak ( The packaging process can still use certain code obfuscation techniques.

Create a new class library item, such as a named NEBULA_SDK.

After the



is published, the developer needs to refer to the nebula_sdk mentioned above as a library reference. The following figure.



Results in two types of Android release SDK scenarios:

Build jar, release jar package and provide layout Configuration resource files such as XML. This is also the way many SDK releases, such as the sdkdomob_android_sdk of advertising, the SDK of statistical analysis, etc.

to generate the jar package, first post the library project and publish this library project. This method can save the developer copy resource file this process.

Of course if SRC Project involves only (r.drawable.xx) files such as pictures (not involving controls such as layout through r.id.xxx), you can copy the picture to the Assert directory and then file Io way to get the resource files that should have been placed under drawable. Of course, if there is a layout and other controls, there is another solution then the layout can be hard-coded in a hard coding (do not read the XML layout), but this should be more cumbersome.

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.