54

How do you setup a project that can result in a jar library file that can be used for android? I want to create a custom library across all projects.

Few other questions:

  1. Does it need to be compiled against a specific version of android sdk?
  2. When an android package is compiled against a jar library does the classes necessary to work with the code get compiled with main code into the apk or does the entire jar get included?
  3. Any notable optimizations or pitfalls I need to know about with using a jar instead of integrating the code directly?
  4. Does the jar have to be signed like the apk needs to?
Jeremy Edwards
  • 14,620
  • 17
  • 74
  • 99

7 Answers7

29

There is nothing special you need to do to your project setup to create a jar file that will work in an Android app. Just create a normal Java project in Eclipse.

  1. It doesn't need to be compiled against Android at all. You can include any jar file as long as it doesn't reference classes that aren't included in Android. Just include your jar file in your build path of your Android projects in Eclipse, and it will automatically be included in your APK file.

  2. Not sure but I assume all classes in the jar file get included in the APK.

  3. I don't think just including some classes in a jar vs. in the project directly will make any difference in the resulting APK.

Mark B
  • 183,023
  • 24
  • 297
  • 295
  • 9
    4. The JAR does not need to be signed. – Christopher Orr Dec 31 '09 at 02:27
  • 1
    I just ran into a roadblock. I have custom layouts that add ids to the R.java file and I have other Android dependencies. How do I get these into a jar format? – Jeremy Edwards Feb 06 '10 at 17:11
  • 3
    You can only have Java class files in a jar. You can't put custom layout XML in there. You have to just copy those into your project. – Mark B Feb 06 '10 at 20:30
  • 1
    mbaird: Not true! JAR is just a zip file, you can store any kind of resources in there. It's just that Android doesn't yet know how to look there to find it. – Mark Renouf Feb 16 '12 at 22:17
  • @MarkRenouf that's exactly what I meant, he asked if he could put custom Android layout files in the jar and he can't. – Mark B Feb 16 '12 at 23:29
  • @mbaird if I need to use some android classes (no assets and layouts) in my java library file (want it to be a JAR file), can I add android.jar to my java project? If so how can I do it? – Krishnabhadra May 31 '12 at 05:03
  • Are you sure this is possible? In a normal Java project, any imports of `android.*` will not work, will they? – caw Jul 31 '13 at 03:43
  • @MarcoW. What you are asking isn't the same as the original question. – Mark B Jul 31 '13 at 13:53
  • @mbaird Why not? I don't see the difference? I can just guess that you are referring to Java-only projects (without referring to Android classes). But it does not explicitly say so. – caw Jul 31 '13 at 16:31
29

Google just release a new version of the SDK that handles Shared Libraries!

https://developer.android.com/tools/projects/projects-eclipse.html#SettingUpLibraryProject

ADT 0.9.7 (May 2010)

Library projects:
The ADT Plugin now supports the use of library projects during development, a capability that lets you store shared Android application code and resources in a separate development project. You can then reference the library project from other Android projects and, at build time, the tools compile the shared code and resources as part of the dependent applications. More information about this feature is available in the Developing in Eclipse with ADT document. If you are not developing in Eclipse, SDK Tools r6 provides the equivalent library project support through the Ant build system.

namuol
  • 9,816
  • 6
  • 41
  • 54
ddcruver
  • 911
  • 1
  • 7
  • 12
  • I just want to also mention, as of writing this MotoDev Studio if you use it does not support the ability to use Librarys. It overrides many of the standard ADT GUIs and it hides the one that allows you to make a Project a Library. I am sure it will be fixed in time. – ddcruver May 23 '10 at 17:54
  • MotoDev Studio 1.2.1 supports shared libraries now. – Timothy Lee Russell Jun 01 '10 at 17:11
  • Yeah they update it about two days after Google release the new ADT forgot to update this. – ddcruver Jun 05 '10 at 20:10
  • 7
    ADT 0.9.7 gets us 80% of what we want, but it still has these drawbacks: (1) a library project cannot hold .aidl files, (2) a library project cannot depend on another library project, (3) a library project cannot hold assets – Jo Jo Jul 01 '10 at 01:21
  • Yeah that is an unfortunate limitation. Possibly you will have to try to create pure java projects for some parts in which don't directly need to be compiled by the android compiler. Also in eclipse you could possibly use file links in eclipse to include code from other android libraries but I haven't tested this. – ddcruver Sep 21 '10 at 13:56
  • If I read the SDK page correctly, the library file is shared at the "code" level, correct? That is, if, for example, I had 5 applications all referencing the same library, then the library's compiled code would be on the device 5 times. Is this correct? – Dave Nov 01 '10 at 14:55
  • 1
    It is almost a year old. Any updates on the limitations? "3. Any notable optimizations or pitfalls I need to know about with using a jar instead of integrating the code directly?" "(1) a library project cannot hold .aidl files, (2) a library project cannot depend on another library project, (3) a library project cannot hold assets" – eros Aug 10 '11 at 06:59
5

I asked myselft the same question: can I export and reuse a layout in Android. I tried the following: export a project with XML resources to a JAR file, and then try to access a layout through it's R.layout. Of course it didn't work, as R.layout.main was an integer, the same integer as was associated to the single layout (relative.xml) defined in the host project's R.layout.

The Android SDK kompiles layout xml files into View resources, and as XML files in the included JAR file are not layout xml files, they are not compiled and no ID is associated to them in R.layout.

If you want to export a layout from your project, then you should export it as XML, and import it to the new project as a layout resource, not as part of an external library.

If you want to reuse layouts from a JAR, you have to create a sublcalss of a View, and build the layout from code. Than you can instantiate it and set the created instance as the View of the Activity, or add the instance to the View of your Activity.

Andras Balázs Lajtha
  • 2,576
  • 25
  • 32
5

This work for me!!!. if you wanna use a .jar file into your application from a library project that uses resources like attrs.xml (and suppose other ones like layouts, strings, etc) that generate a R.java. you should in eclipse go to export->Java/Jar file-> and check Export all output folders for checked projects -> Finish... this will ensure that at run time your app that uses this jar does not generate an exception for missing clases and resources.

Of course, to include the jar into you new project go to. project properties-> Java build path -> Libraries -> Add external jar -> and you are done.

Hope this help!

Abdul Rahman
  • 2,097
  • 4
  • 28
  • 36
cags
  • 51
  • 1
  • 1
3

to create a jar file, you should use the Export option of Eclipse. In the export dialog, choose java, and the suboption JAR. THen a wizard is displayed where you need to make some selections. You need to specify the location where the jar file will be stored.

An error I made the first time I created a jar file, was to include the Android Manifest. When adding the created jar file, I got a conflict of double android manifest.

Stefaan V
  • 31
  • 1
2

You can use a custom method to reach R file dynamicly, example link is below.

Packaging Android resource files within a distributable Jar file

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;
}
Community
  • 1
  • 1
ymutlu
  • 6,585
  • 4
  • 35
  • 47
0

If you look in the android.jar file under platforms/android-X.X/android.jar file it does include layout XML items so you should be able to create one as well. I just am not sure on exactly how you do this and how you reference them in the other project.

ddcruver
  • 911
  • 1
  • 7
  • 12