Making Library for Android Application

Common bugs/problems with the Android SDK the Emulator and the ADT-Plugin.

Making Library for Android Application

Postby c3r13e12u5 » Thu Oct 09, 2008 3:28 am

Could anyone tell me how to make an API to be used in other Android application?
How can I Compile it?

Thank you.
c3r13e12u5
Developer
Developer
 
Posts: 28
Joined: Wed Oct 08, 2008 7:33 am

Top

Postby MrSnowflake » Thu Oct 09, 2008 3:48 pm

In eclipse, start a Java Project and start coding. Don't forget to add the adroid.jar if you need android functions. When you are done, you right-click the project and select export. Then java -> jar file. And in the new dialog enter the correct path and filename and you're set!
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby c3r13e12u5 » Mon Oct 13, 2008 11:09 am

Thanks for the reply MrSnowFlake,
will try it out Soon. :)
c3r13e12u5
Developer
Developer
 
Posts: 28
Joined: Wed Oct 08, 2008 7:33 am

Postby c3r13e12u5 » Tue Oct 14, 2008 9:04 am

I tried compiling the API into jar and use it in another Android application project.
I made a user Library pointing to the API I created and add it on my Android application project.
Finish Android application project nicely. able to use my API classes.
but when running I encounter an exception
Sorry! The application ADemo_Block (process com.ADemo_Block) has stopped unexpectedly. Please try again.
(Screen shot uploaded).

on the Log i see:
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.ADemo_Block/com.Ademo_Block.ADemo_Block}: java.lang.ClassNotFoundException: com.ADemo_Block.ADemo_Block in loader dalvik.system.PathClassLoader@433a71e8


Could anybody tell me why I get ClassNotFoundException?
does it means I didnt add the library properly? or I didnt compile the API properly?

Thank you.
Attachments
API_bug1.jpg
Exception when using my API.
API_bug1.jpg (43.11 KiB) Viewed 32541 times
c3r13e12u5
Developer
Developer
 
Posts: 28
Joined: Wed Oct 08, 2008 7:33 am

Postby MrSnowflake » Tue Oct 14, 2008 9:06 am

Did you include your lib jar in the android project?
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby c3r13e12u5 » Tue Oct 14, 2008 9:50 am

hi MrSnowFlake,

I did register the jar on my project libraries.

right click on my project folder -> properties -> Java Build Path -> libraries -> Add Library -> user Library -> select my API.


Is there anything I missed out?
Do I need to put the jar in the apk file?


Thanks.
JT
c3r13e12u5
Developer
Developer
 
Posts: 28
Joined: Wed Oct 08, 2008 7:33 am

Top

Postby MrSnowflake » Tue Oct 14, 2008 9:54 am

Post (some) of your code and a little more of the Log, so we can have a look.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby c3r13e12u5 » Tue Oct 14, 2008 11:00 am

here is some parts of my code:
the API override Activity class.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public abstract class myActivity extends Activity{
  2.  
  3.   public abstract void myAbstractMethod();
  4.  
  5.   public void onCreate(Bundle savedInstanceState) {
  6.     super.onCreate(savedInstanceState);
  7.     //...code
  8.   }
  9. }
  10.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


my Android application code use overridden class from my API:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class demo extends myActivity{
  2.  
  3.   public void onCreate(Bundle savedInstanceState) {
  4.     super.onCreate(savedInstanceState);
  5.     //...code
  6.   }
  7.  
  8.   public void myAbstractMethod(){
  9.     //...code
  10.   }  
  11.  
  12. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4



The error code generated:
Code: Select all
10-14 09:54:48.639: DEBUG/AndroidRuntime(2676): CheckJNI is ON
10-14 09:54:48.898: INFO/jdwp(2676): received file descriptor 20 from ADB
10-14 09:54:48.909: DEBUG/AndroidRuntime(2676): --- registering native functions ---
10-14 09:54:49.229: ERROR/jdwp(2676): Failed sending reply to debugger: Broken pipe
10-14 09:54:49.229: INFO/dalvikvm(2676): Debugger has detached; object registry had 2 entries
10-14 09:54:49.789: INFO/ActivityManager(52): Starting activity: Intent { flags=0x10000000 comp={com.ADemo_Block/com.ADemo_Block.ADemo_Block} }
10-14 09:54:49.850: DEBUG/AndroidRuntime(2676): Shutting down VM
10-14 09:54:49.850: DEBUG/dalvikvm(2676): DestroyJavaVM waiting for non-daemon threads to exit
10-14 09:54:49.869: INFO/ActivityManager(52): Start proc com.ADemo_Block for activity com.ADemo_Block/.ADemo_Block: pid=2684 uid=10023 gids={3003}
10-14 09:54:49.908: INFO/dalvikvm(2676): DestroyJavaVM shutting VM down
10-14 09:54:49.908: DEBUG/dalvikvm(2676): HeapWorker thread shutting down
10-14 09:54:49.908: DEBUG/dalvikvm(2676): HeapWorker thread has shut down
10-14 09:54:49.908: DEBUG/jdwp(2676): JDWP shutting down net...
10-14 09:54:49.908: DEBUG/dalvikvm(2676): VM cleaning up
10-14 09:54:49.919: DEBUG/dalvikvm(2676): LinearAlloc 0x0 used 540764 of 4194304 (12%)
10-14 09:54:50.078: INFO/jdwp(2684): received file descriptor 10 from ADB
10-14 09:54:50.249: WARN/dalvikvm(2684): Unable to resolve superclass of Lcom/ADemo_Block/ADemo_Block; (30)
10-14 09:54:50.249: WARN/dalvikvm(2684): Link of class 'Lcom/ADemo_Block/ADemo_Block;' failed
10-14 09:54:50.259: DEBUG/AndroidRuntime(2684): Shutting down VM
10-14 09:54:50.259: WARN/dalvikvm(2684): threadid=3: thread exiting with uncaught exception (group=0x40010e28)
10-14 09:54:50.259: ERROR/AndroidRuntime(2684): Uncaught handler: thread main exiting due to uncaught exception
10-14 09:54:50.289: ERROR/AndroidRuntime(2684): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.ADemo_Block/com.ADemo_Block.ADemo_Block}: java.lang.ClassNotFoundException: com.ADemo_Block.ADemo_Block in loader dalvik.system.PathClassLoader@433e7058
10-14 09:54:50.289: ERROR/AndroidRuntime(2684):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2068)
10-14 09:54:50.289: ERROR/AndroidRuntime(2684):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2156)
10-14 09:54:50.289: ERROR/AndroidRuntime(2684):     at android.app.ActivityThread.access$1800(ActivityThread.java:112)
10-14 09:54:50.289: ERROR/AndroidRuntime(2684):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1580)
10-14 09:54:50.289: ERROR/AndroidRuntime(2684):     at android.os.Handler.dispatchMessage(Handler.java:88)
10-14 09:54:50.289: ERROR/AndroidRuntime(2684):     at android.os.Looper.loop(Looper.java:123)
10-14 09:54:50.289: ERROR/AndroidRuntime(2684):     at android.app.ActivityThread.main(ActivityThread.java:3742)
10-14 09:54:50.289: ERROR/AndroidRuntime(2684):     at java.lang.reflect.Method.invokeNative(Native Method)
10-14 09:54:50.289: ERROR/AndroidRuntime(2684):     at java.lang.reflect.Method.invoke(Method.java:515)
10-14 09:54:50.289: ERROR/AndroidRuntime(2684):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
10-14 09:54:50.289: ERROR/AndroidRuntime(2684):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:497)
10-14 09:54:50.289: ERROR/AndroidRuntime(2684):     at dalvik.system.NativeStart.main(Native Method)
10-14 09:54:50.289: ERROR/AndroidRuntime(2684): Caused by: java.lang.ClassNotFoundException: com.ADemo_Block.ADemo_Block in loader dalvik.system.PathClassLoader@433e7058
10-14 09:54:50.289: ERROR/AndroidRuntime(2684):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:215)
10-14 09:54:50.289: ERROR/AndroidRuntime(2684):     at java.lang.ClassLoader.loadClass(ClassLoader.java:453)
10-14 09:54:50.289: ERROR/AndroidRuntime(2684):     at java.lang.ClassLoader.loadClass(ClassLoader.java:421)
10-14 09:54:50.289: ERROR/AndroidRuntime(2684):     at android.app.Instrumentation.newActivity(Instrumentation.java:1096)
10-14 09:54:50.289: ERROR/AndroidRuntime(2684):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2060)
10-14 09:54:50.289: ERROR/AndroidRuntime(2684):     ... 11 more
10-14 09:54:50.330: INFO/Process(52): Sending signal. PID: 2684 SIG: 3
10-14 09:54:50.367: INFO/dalvikvm(2684): threadid=7: reacting to signal 3
10-14 09:54:50.438: INFO/dalvikvm(2684): Wrote stack trace to '/data/anr/traces.txt'
10-14 09:54:52.258: INFO/Process(2684): Sending signal. PID: 2684 SIG: 9
10-14 09:54:52.278: INFO/ActivityManager(52): Process com.ADemo_Block (pid 2684) has died.


There is a ClassNotFoundException. Do you know what cause this?
If I move the API classes into the same project as my Android application it works perfectly.



Thank you.
c3r13e12u5
Developer
Developer
 
Posts: 28
Joined: Wed Oct 08, 2008 7:33 am

Postby MrSnowflake » Tue Oct 14, 2008 11:34 am

I have no clue. The only thing for my to be able to help you would be to have the complete source. But maybe someone else can help you.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby c3r13e12u5 » Tue Oct 14, 2008 11:50 am

Is there any sample of API I can download and try?
A simple one like I made if possible. just overriding Activity class to be used in my current Android application.

Thank you.
c3r13e12u5
Developer
Developer
 
Posts: 28
Joined: Wed Oct 08, 2008 7:33 am

Postby MrSnowflake » Tue Oct 14, 2008 11:52 am

You could take a look at my DroidGamer, there's a jar download in the download section.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby c3r13e12u5 » Wed Oct 15, 2008 3:30 am

Ok, I manage to solve the problem.
Exception occur because the Library package is not included to the current project.
Instead of creating a "user Library" I should have directly add the jar file to the current project libraries list.
finally solved :lol:
c3r13e12u5
Developer
Developer
 
Posts: 28
Joined: Wed Oct 08, 2008 7:33 am

Postby Quetzalcoatl » Fri Jan 16, 2009 3:42 pm

Sorry to dig up an old thread but I have a similar/related problem I'd like to get to the bottom of if I may? :)

I have exported a JAR file containing everything I'd like in it and it includes some resources. The problem is that I don't think there's any reference to the correct resources (.txt/.png files etc) within the JAR.
I assume this has something to do with R.Java not being in place in the JAR file, do I need to force/replace it within the JAR file?

My project currently has a dummy UI over the top of all the workings within the JAR, the first thing my app complains about is the rubbish data it is getting from one of my txt files (tries to convert it to int and then dies). So I'm assuming it's reading some rubbish memory instead of the correct resource or something.

Any ideas?
Image
Quetzalcoatl
Senior Developer
Senior Developer
 
Posts: 129
Joined: Sat Oct 18, 2008 8:21 pm
Location: near Manchester, UK

Postby sudheer1437 » Mon Oct 05, 2009 3:59 pm

To MrSnowFlake,

A very heartful thanks to u MrSnowFlake. This thread helped me a lot.
But i got struck at 1point.

I am creating my own set of classes and as you mentioned, i exported into a jar,
and i m using that jar in another application.

My problem is,

in my class I m using ImageView and want to show a image in that view.
So my first question is where should i put my image in my java project..???
I kept in an images folder and wrote the below code in my class:


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Bitmap smallImage = BitmapFactory.decodeFile("/images/mcdo.png");
  2.  
  3. imageView.setImageBitmap(smallImage);
Parsed in 0.034 seconds, using GeSHi 1.0.8.4



But when i integrate it into my application, no image is shown.

where am i doing wrong...??

please help me...

Thanks in advance...!!

Sudheer
sudheer1437
Experienced Developer
Experienced Developer
 
Posts: 79
Joined: Sat Aug 08, 2009 7:42 am
Location: India

Postby Quetzalcoatl » Mon Oct 05, 2009 4:15 pm

I did this a while ago & it was something like this (if I remember correctly):

We added our images to the projects Assets folder and did the following:

Code: Select all
InputStream is =  myActivity.getAssets().open(filename);
bitmap = BitmapFactory.decodeStream(is);


I think getAssets is actually part of the "android.content.ContextWrapper" class from which Activity is derived.

The problem with this is that we had to force any resources on the application that wants to use our jar, the way we did this (if I remember correctly) was to export them to the assets folder in our modified Ant build files. We generated a build.xml from the command line as part of the android SDK and then butchered it a little :D
Image
Quetzalcoatl
Senior Developer
Senior Developer
 
Posts: 129
Joined: Sat Oct 18, 2008 8:21 pm
Location: near Manchester, UK

Top
Next

Return to SDK/ADT/Emulator Problems

Who is online

Users browsing this forum: No registered users and 4 guests