ARNeto
ARNeto

Reputation: 451

Android libraries in ADT: type cannot be resolved error

In essence: a library project declares a Activity, a implementation project uses (subclass) this Activity and a test project exercises the implemented version. In this scenary we found "The type cannot be resolved. It is indirectly referenced from required .class files". If changed to ADT r13 no problems reported. Code to issue reproduction: In library:

package com.r14.error.lib;
import android.app.Activity;
import android.os.Bundle;

public class R14ErrorLibActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

In implementation:

package com.r14.error.imp;
import com.r14.error.lib.R14ErrorLibActivity;

public class R14ErrorImpActivity extends R14ErrorLibActivity {
}

In test:

package com.r14.error.imp.test;    
import com.r14.error.imp.R14ErrorImpActivity;    
import android.test.ActivityUnitTestCase;

public class R14ErrorLibImpActivityTest extends ActivityUnitTestCase<R14ErrorImpActivity> {    
    public R14ErrorLibImpActivityTest() {
        super(R14ErrorImpActivity.class);
    }       
    private static final int ANSWER_TO_THE_ULTIMATE_QUESTION_OF_LIFE_THE_UNIVERSE_AND_EVERYTHING = 42;    
    public void testTest() {
        assertEquals(ANSWER_TO_THE_ULTIMATE_QUESTION_OF_LIFE_THE_UNIVERSE_AND_EVERYTHING, 42);
    }       
}

Error:

The type com.r14.error.lib.R14ErrorLibActivity cannot be resolved. It is indirectly referenced from required .class files

If the lib is added manually 1 (action not necessary in older adt versions) the build error is not displayed but the test fail with:

java.lang.RuntimeException: Exception during suite construction
at android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests.testSuiteConstructionFailed(TestSuiteBuilder.java:239)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:430)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)
Caused by: java.lang.reflect.InvocationTargetException
at com.r14.error.imp.test.R14ErrorLibImpActivityTest.<init>(R14ErrorLibImpActivityTest.java:10)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:446)
at android.test.suitebuilder.TestMethod.instantiateTest(TestMethod.java:87)
at android.test.suitebuilder.TestMethod.createTest(TestMethod.java:73)
at android.test.suitebuilder.TestSuiteBuilder.addTest(TestSuiteBuilder.java:263)
at android.test.suitebuilder.TestSuiteBuilder.build(TestSuiteBuilder.java:185)
at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:336)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3982)
at android.app.ActivityThread.access$2900(ActivityThread.java:119)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1901)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4363)
at java.lang.reflect.Method.invokeNative(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoClassDefFoundError: com.r14.error.imp.R14ErrorImpActivity
... 19 more

I'm working with Eclipse Indigo and ADT 15.

Thanks for any help!

Upvotes: 1

Views: 4230

Answers (2)

ARNeto
ARNeto

Reputation: 451

This is already a issue (21343) on Android Open Handset Alliance Project. If you have the same problem, please star this issue. Note: Problem seems fixed with revision 17 of the Android SDK Tools and of the Eclipse ADT plug-in (my own projects compiled without hacks or errors).

Upvotes: 2

paulorcf
paulorcf

Reputation: 43

Same problem reported here:

http://code.google.com/p/android/issues/detail?id=21888

You can try this hack! (found inside the issue):

Hack eclipse to get this working by exporting library projects in the main project.

Upvotes: 1

Related Questions