ActivityInstrumentationTestCase2 and android library project: resources fail to load - android

I have the following setup:
LibProjectWithActivity - an android library project which contains PreviewActivity
AppProject - usual Android Project which uses LibProjectWithActivity
AppProjectTest - an Android JUnit Test Project which is an ActivityInstumentationTestCase2
First of all: all basic dependencies are set up correctly, because I can perfectly launch my app in emulator, it shows PreviewActivity just ok, things work - no problems here.
But I fail to launch the test... Here's the setup:
public class PreviewActivityTest
extends ActivityInstrumentationTestCase2<PreviewActivity> {
public PreviewActivityTest() {
// note: if i put "com.the7art.libprojectwithactivity" instead it wont find it
// and will fail with "activity not found"
super("com.the7art.appproject", PreviewActivity.class);
}
public void testDummy() {
getActivity();
}
}
This throws a NullPointerException like this:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.the7art.appproject/com.the7art.libprojectwithactivity.PreviewActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2401)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2417)
.......
Caused by: java.lang.NullPointerException
at com.the7art.libprojectwithactivity.PreviewActivity.onCreate(PreviewActivity.java:37)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
... 11 more
Here are lines PreviewActivity.java:36,37:
mButtonAdd = (Button) findViewById(R.id.button_add);
mButtonAdd.setOnClickListener(this);
So what this exception means is that R.id.button_add (which is in a libproject) is perfectly found, but the view by this id can't be found. If I comment out this code, it'll crash similarly when trying to obtain another resource.
Looks like test uses right R.java, but wrong Context object. Or something like this.
Again, the app code is ok, because it works in emulator, something is wrong with test setup...
Any hints on what is wrong and how to fix this?

Yes the test can be only use the Activity project as a target, look at the projectTarget in your Test manifest file.
This means that your test can use the same context as your application does. Accessing only the application's project resources. However, you can't access Library's project resources, you can use the R.string.XX from the library's project because this was first built it, and it's only a generated identifier that will be used in the application's context to find the resource. So the result will be an mistake in the resource found or a resource not found exception.
There are 2 ways to test a library project, here they are

Related

Shadow PackageParser.call ParsePackage PackageParser$Packageparserexception

I am trying to move my code to dynamic feature and while moving the test cases i created a test package inside dynamic feature module and moved related test classes there.
The test cases are not running and failing with below exception
java.lang.RuntimeException: android.content.pm.PackageParser$PackageParserException: Failed to parse /Users/anujjha/../build/intermediates/apk_for_local_test/debugUnitTest/packageDebugUnitTestForUnitTest/apk-for-local-test.ap_
at org.robolectric.shadows.ShadowPackageParser.callParsePackage(ShadowPackageParser.java:69)
at org.robolectric.android.internal.AndroidTestEnvironment.loadAppPackage_measured(AndroidTestEnvironment.java:314)
at org.robolectric.android.internal.AndroidTestEnvironment.lambda$loadAppPackage$1(AndroidTestEnvironment.java:284)
at org.robolectric.util.PerfStatsCollector.measure(PerfStatsCollector.java:53)
at org.robolectric.android.internal.AndroidTestEnvironment.loadAppPackage(AndroidTestEnvironment.java:282)
at org.robolectric.android.internal.AndroidTestEnvironment.installAndCreateApplication(AndroidTestEnvironment.java:178)
at org.robolectric.android.internal.AndroidTestEnvironment.setUpApplicationState(AndroidTestEnvironment.java:169)
at org.robolectric.RobolectricTestRunner.beforeTest(RobolectricTestRunner.java:301)
There are all junit test cases. Any idea how to make it work.and what can i check in code or gradle.

Android NoClassDefFound when referencing class from external libraries (IntelliJ IDEA 12)

I'm having an issue with IntelliJ's dependency handling in regards to external modules. Here's some context:
I'm writing an Android app which implements Google Maps. For my current feature set, I require two external libraries-- Google's Play Services Library and mapex (A third party google map extension library located here https://code.google.com/p/mapex/). I actually built most of this project in Android Studio before I was recommended to move to IntelliJ due to the easier dependency handling. But now I'm here and still having problems.
My Error
When trying to build an object from a class located in the mapex package (com.androidnatic.maps), I get this error when starting the activity the view is contained in (object has not been created yet):
07-03 11:40:35.837: ERROR/dalvikvm(3585): Could not find class 'com.androidnatic.maps.SimpleMapView', referenced from method com.example.myproject.MapActivity.showHeatMap
And then, upon creation, my app force closes and leaves this behind in logcat:
7-03 11:40:45.467: ERROR/AndroidRuntime(3585): FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: com.androidnatic.maps.SimpleMapView
at com.example.myproject.MapActivity.showHeatMap(MapActivity.java:492)
Yet I'm completely stumped because my IDE gives me no sign that anything is wrong! Classes are auto-filled in when trying to access them from MyProject.
ide-fill-in http://www.tonyandrys.com/img/fillin.jpg
No build errors about missing classes, missing modules, class definitions, or anything related.
I get the same results if I try to access the class via its full package name as well.
// No dice.
final SimpleMapView mapView = new SimpleMapView(this, "apikey");
final com.androidnatic.maps.SimpleMapView mapView = new com.androidnatic.maps.SimpleMapView(this, "apikey");
It seems that there's only an issue when the application is running on my phone, as far as my IDE is concerned.
Here's the structure of MyProject:
main-project-structure http://www.tonyandrys.com/img/mainstructure.jpg
Here's my main project's dependency settings (where I assume I screwed up somewhere):
project-structure http://www.tonyandrys.com/img/projectstructure.jpg
And here is the structure of the mapex library module:
mapex-lib http://www.tonyandrys.com/img/mapexstructure.jpg
The class files that I'm trying to access live in MapExLib/gen and MapExLib/src, which are currently marked as source directories (blue).
Does anyone have any ideas on how to proceed from here?
Try to do this :
Android, IntelliJ Idea, changing module type
I had the same error "Could not find class XXX from referenced method XXX" with Intellij, involving an Android Test Module and a test which could not find the Activity class anywhere.
I set inside the Project Structure the main Module's Facet Android to Library Module (checkbox) and it worked...
Let us know

Android Application class not found

In my application I use RoboGuice and the configuration for RoboGuice requires to add an Application-class and specify it in the AndroidManifest.xml file in the application-tag using the 'android:name' attribute.
So this is how my applicaiton-tag in the manifest looks like:
<application android:label="Worktime" android:icon="#drawable/logo" android:name=".guice.Application">
This always works and still does when I compile inside my IDE (IntelliJ) and deploy it to my device.
However when I want to run my tests using Ant (and only via Ant, this also still works in the IDE) I have this error on the console:
[exec] android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests:INSTRUMENTATION_RESULT: shortMsg=Unable to instantiate application eu.vranckaert.worktime.guice.Application: java.lang.ClassNotFoundException: eu.vranckaert.worktime.guice.Application in loader dalvik.system.PathClassLoader#44e88928
[exec] INSTRUMENTATION_RESULT: longMsg=java.lang.RuntimeException: Unable to instantiate application eu.vranckaert.worktime.guice.Application: java.lang.ClassNotFoundException: eu.vranckaert.worktime.guice.Application in loader dalvik.system.PathClassLoader#44e88928
This has worked before but started failing since I upgraded my 'Android SDK Tools' to revision 17 and the 'Android SDK Platform-tools' to revision 11.
Anyone who had this issue also or who knows how to fix it?
I think that the error saying
java.lang.ClassNotFoundException:
is occur only when you have create new class Activity and not declare(register) that in manifest file
please check if you have any new activity and not declare(register) that activity in manifest file
Thanks.
we might have encountered the same error - In my case the solution was to separate the Ant targets to two calls (i.e. "ant myParameters myTarget1 myTarget2", and "ant myParameters debug delivery"). Hope this works for you, anyhow - Google need to add better support for their undocumented/faulty changes (and stackoverflow isn't the best place for questions that are not originated in stupidity, as the lifetime of a complex question isn't too long here).

Robotium test cannot find activity class

I have been using robotium to test my android application. I found it very useful tool so far. Recently we have done a refactoring that would use only one activity in the entire application, each page will be replaced by a fragment.
However, After we start using that activity to run the unit tests, the test complains NoClassDefound error -- it couldn't find the activity class. I don't see anywhere I have change the configuration whatsoever.
Can anybody give a clue what might be wrong , where to check and so on ?
[INFO] java.lang.RuntimeException: Exception during suite construction
at android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests.testSuiteConstructionFailed(T estSuiteBuilder.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:529)
....
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoClassDefFoundError: com.xxx.wallet.HaloActivity
at com.xxx.wallet.HaloActivityTest.<init>(HaloActivityTest.java:12)
... 18 more
The app apk is loaded, AndroidManifest.xml should be ok too.
Make sureafter refactoring:
The AndroidManifest.xml of the test project is still accurate:
<instrumentation android:targetPackage="package.of.the.app.under.test">
The Test class is still accurate:
public class YourTest extends ActivityInstrumentationTestCase2<SplashScreenActivity> {
protected static final String TARGET_PACKAGE_ID = "package.of.the.app.under.test";
protected Solo solo;
public Test() {
super(TARGET_PACKAGE_ID, StartingActivityOfYourAppUnderTest.class);
}
//..
}
All libraries of the app under test can only (!) be found in libs/yourlibrary.jar and are referenced in Project->Properties->Java Build Path->Libraries

Error NullPointerException when running an altered version of an Android Project

I have this Android program running well, called TestePHP2 (name of main class), package com.testedeacesso.www. It is stable and runs perfectly.
I've cloned this project's folder to start developing an upgrade to it. I called it TesteTempo01, after importing it to Eclipse - option 'from existing project' (yes, I used the refractor to change the class name and I checked 'update references'). I modified this program's main class as I mentioned before and added some code to it, but I kept the package name the same, as well as other classes from the program.
Whe I try to run this program, though, I got a 'can't find main activity' error. After searching through my code, I found out that on the manifest there was a reference to the TestePHP2 that I hadn't changed before. for reference, this was the line on the Android Manifest XML:
> <activity android:name="com.testedeacesso2.www.TesteTempo01"
> android:label="#string/app_name">
After this, I keep getting a NullPointerException because the class can't find the main class! I've searched through the whole code but I found no other references to the old class. Have I forgotten something? Do I need to alter something else to change this class' name? Or the import will only work with the original class names?
You said the package was 'com.testedeacesso.www', but when you reference the class, its 'com.testedeacesso2.www'. I'm guessing you have a problem with class/package references.

Categories

Resources