8

I have my own custom library apk file (say lib.apk) & i want make it available to other applications. How to provide the uses-library in the android manifest.xml file in other apps so as to use my custom library.

Arutha
  • 26,088
  • 26
  • 67
  • 80
  • Why do you need an APK for that? Just roll a JAR and you're fine. – mxk Sep 07 '09 at 15:35
  • I have the same problem. I have a modular software that will work as multiples activities, and I didn't want to repeat the interfaces and common code in all modules. That would be the case if I opted to use a jar lib. – Spidey Jul 29 '10 at 19:47
  • 4
    It is difficult to include resources in jar files, which a lib.apk handles. – phreed Dec 28 '10 at 16:55

5 Answers5

7

The <uses-library> element is for add-ons supplied as extensions to the firmware. AFAIK, it will not be usable for your scenario.

Most likely, you will need to implement a service that exposes an API via AIDL, or uses a set of documented Intent actions to exchange data with other applications, or exposes a ContentProvider.

Otherwise, package your code as a JAR, not an APK. You can see many examples of this in my github repositories (all of the cwac- ones follow this pattern).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
3

When converting from a jar library to and apk library make sure that the reference to the jar is removed from "Java Build Path" -> "Projects" and the library is added to "Android" -> "Library".

phreed
  • 1,759
  • 1
  • 15
  • 30
  • 2
    Thanks, this worked for my case where the application linked to my custom library but when installing the APK on the device I got a warning saying that the library "APK" could not be found. Removing it from the Java Build Path but keeping it as Library reference solved it. – fejd Jun 08 '11 at 07:45
  • 1
    @fejd Exactly my experience as described [here](http://stackoverflow.com/a/20320105/2930186) – ripopenid Dec 04 '13 at 03:15
0

I try to use a jar library in eclipse (I added an external jar library in the Java Build Path options of my project). The application compiles but I have a "Could not find class..." exception at runtime.

Arutha
  • 26,088
  • 26
  • 67
  • 80
0

You can actually make a project that links to another project.

You have to do two things in Eclipse:

  • add uses-library = package name in the manifest.xml
  • open project properties -> Java Build Path -> Projects and add the project of the used library

But i haven't managed to make it run. The sdk correcly uploads both packages, but I get an link error

WARN/dalvikvm(444): Link of class 'Lme/guillaumin/android/osmtracker/activity/DisplayTrackMap;' failed
ERROR/dalvikvm(444): Could not find class 'me.guillaumin.android.osmtracker.activity.DisplayTrackMap', referenced from method me.guillaumin.android.osmtracker.activity.TrackLogger.onOptionsItemSelected
WARN/dalvikvm(444): VFY: unable to resolve const-class 154 (Lme/guillaumin/android/osmtracker/activity/DisplayTrackMap;) in Lme/guillaumin/android/osmtracker/activity/TrackLogger;

The VM cannot load the class in my app that links into the libs app.

Viesturs
  • 1,691
  • 4
  • 21
  • 25
-1

The lib.apk "application" should publish intents using Intent-Filters in the AndroidManifest.xml file.

The activity which requires to use the activity within the lib.apk, just needs to start an intent like below :

  Intent i = new Intent();
  i.setClass(this, libAPKActivity.class);
Lucifer
  • 29,392
  • 25
  • 90
  • 143
Roy Samuel
  • 790
  • 9
  • 24