8

After converting a perfectly working application to a library (including its Activity class!), I am trying to create an application that uses that entire library by simply superclassing the library's activity:

package com.example.baseapp.paid;

import android.os.Bundle;
import com.example.baseapp.LibActivity;


public class PaidActivity extends LibActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}

Eclipse builds this newly "re-architected" application without any errors, but when I try to run it, I get an exception:

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.baseapp.paid/com.example.baseapp.paid.PaidActivity}: java.lang.ClassNotFoundException: com.example.baseapp.paid.PaidActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.baseapp.paid-1.apk]
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4627)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: com.example.baseapp.paid.PaidActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.baseapp.paid-1.apk]
    at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
    ... 11 more

I have no idea why this is happening since the error for "Class Not Found" when the class is exactly the derived class built (without errors!) and now trying to run.

How do I troubleshoot this?

What am I missing?

EDIT (answering question by @CaspNZ):

This is the AndroidManifest.xml of the library project:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.baseapp"
      android:versionCode="5"
      android:versionName="1.1.2"> 
    <uses-sdk android:minSdkVersion="8" />

    <uses-permission android:name="android.permission.INTERNET"/>   
</manifest>

And this is the AndroidManifest.xml of the application, using the library project:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.baseapp.paid"
      android:versionCode="5"
      android:versionName="1.1.2"> 
    <uses-sdk android:minSdkVersion="8" />
    <uses-library android:name="AppLibrary" />

    <uses-permission android:name="android.permission.INTERNET"/>   

    <application android:icon="@drawable/icon">
        <activity android:name=".PaidActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.example.baseapp.LibActivity" android:label="com.example.baseapp.LibActivity:string/rx_label">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="EditPreferences" 
                android:label="com.example.baseapp.LibActivity:string/app_name">
        </activity>
    </application>
</manifest>

I would appreciate any hint trying to troubleshoot this as this is my first time ever trying to use a library project and I am not familiar with the "tricks" involved to get this working.

an00b
  • 11,338
  • 13
  • 64
  • 101
  • 1
    Do you use proguard ? Is your class inside you dex file ? (You can use dexdump from android distro to find out, classes.dex is in your apk). – Snicolas Jun 13 '11 at 01:21
  • @Snicolas This is in debug mode, so no ProGuard. I don't know (yet) what to look for in dexdump. However, I made some progress since I posted and discovered that Eclipse's re-factoring works for me most of the time, but also works against me every once in a while, just like in this case in which it injected some garbage into the Library's AndroidManifest.xml. I fixed this but I am still getting the error, though. – an00b Jun 13 '11 at 01:28
  • @Snicolas This is related to my attempt to implement your 1st suggestion in [the other thread](http://stackoverflow.com/questions/6324234/converting-an-existing-application-to-a-library). Do I have to duplicate the library's AndroidManifest.xml for the derived application? – an00b Jun 13 '11 at 01:32
  • 1
    Can you post your Manifest.xml file? The exception is almost certainly caused by a problem in this file. – Caspar Harmer Jun 13 '11 at 04:29
  • @CaspNZ Thanks for your guidance. I just posted the two AndroidManifest.xml files. I am still groping in the dark. Any help would be very much appreciated. – an00b Jun 13 '11 at 13:02
  • Sorry was asleep over here - Glad to hear you've solved it. – Caspar Harmer Jun 13 '11 at 19:17

4 Answers4

10

For anyone who updated to ADT 22, make sure that you have checked Android Private Libraries in the Java Build Path > Order and Export tab. For further information, read this. This works for me. Maybe little force to move users to Android Studio instead of Eclipse:))

nlt
  • 555
  • 7
  • 11
  • 1
    Oh dear god, 4 hours of battling later and I realise I'd missed the "even on the final app project" part. Thanks for this, made me go re-read it properly and fix my issue. – Ruddles May 17 '13 at 09:28
  • I had the same error: **Unable to instantiate activity ComponentInfo** and **java.lang.ClassNotFoundException: Didn't find class**. I was extending FragmentActivity. This worked for me. – vovahost May 23 '13 at 14:38
  • Thanks - that fixed my issue. There's nothing like a good "upgrade" to blow all your projects out of the water! – ccbunney Jul 10 '13 at 20:05
1

This can happen if you list the activities in the library manifest instead of in the application manifest. There's no need to have <activity> tags in a library manifest. (See, for instance, this thread.)

Community
  • 1
  • 1
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • Thanks for this information (I didn't know this). I moved all `` and `` from the library to the derived application's manifest but still no luck. I am groping in the dark. :( – an00b Jun 13 '11 at 02:31
1

I finally solved the problem. It turns out that I had 2 critical settings in the Properties of both projects not set correctly:

  1. In the library project, "Is Library" was not checked for some reason. I could swear that I checked it, but knowing how whimsical the Android development environment under Eclipse can be, I suspect that it was unchecked by Eclipse (or the ADT plugin) as a result of some glitch.
  2. In the application project, I neglected to add my library project as a reference via the Add... button. (how dumb could I be?)

I hope that other newbies like me will find this information helpful. Sometimes, an extremely difficult problem to debug, hides a configuration error when the innocent unsuspecting stressed programmer assumes that this is already has been taken care of...

an00b
  • 11,338
  • 13
  • 64
  • 101
0

I had the same error in one of my projects and thought that I can share how I fixed mine.

At some point of my project I had to change one of the package names I am using where all my Activity classes are situated. When I did, I forgot to update the AndroidManifest which caused me the same error. Updating this fixed it.

Mark Pazon
  • 6,167
  • 2
  • 34
  • 50