Tuesday, 12 November 2013

Tuesday, 5 November 2013

Do you android.content.res.Resources$NotFoundException: unknown resource ? (aka Testing with Robolectric (II))

I assumed that one only needed to specify the location of the manifest if, like here, you were extending the Application class. After reading and failing miserably to get the most stupid test up and running, turns out that when you have BuildTypes and/or Flavours you have to explicit about your manifest too.

So if you are getting one of these pesky exceptions:
android.content.res.Resources$NotFoundException: unknown resource XXXXXX
Double check that you specify the location of your AndroidManifest file in the @Config anotation.

Debugging Gradle

Always wanted to find out how does Gradle the thing it does? Is it driving you crazy not knowing why that script is not working?

Attach a remote debugger


TL;DR
  • Add a new system variable:
    GRADLE_OPTIONS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5006
  •  Start your Gradle task
  • Run your favourite IDE's debugger

Monday, 28 October 2013

Testing with Robolectric (I)

If you have your own Application class for your Android app, you might have found your tests failing because of a class cast exception:
java.lang.ClassCastException: android.app.Application cannot be cast to BLALBLA class
And if you are using Robolectric 2.+ the way to solve this is to annotate your test with
@Config
like so:
@RunWith(RobolectricTestRunner.class)
public class MyActivityTest {
@Test
@Config(manifest="src/main/AndroidManifest.xml")
public void shouldTestSomethingAmazing() throws Exception {
// Awesome code here
}
}

You can annotate also annotate the whole suite.

Wednesday, 11 September 2013

Get Google Play services in your app (Intellij Android Studio)

You can find an easy way to get Google Play services in your project here

But for an even easier way, this is your safest bet

UPDATE:
If you change update your Studio, make sure to run a clean and build. Wasted morning N+1

Friday, 5 July 2013

Running tests Test running startedTest running failed: Unable to find instrumentation info for: ComponentInfo{ [package.name]/android.test.InstrumentationTestRunner} Empty test suite.

I have been trying to run tests using Intellij's Android Studio. So I started here and used the IDE to help me create everything I needed. Failed horribly. Then I followed this and kept receiving this HELPFUL message:

Running tests
Test running started
Test running failed: Unable to find instrumentation info for: ComponentInfo{pacakge.name/android.test.InstrumentationTestRunner}
Empty test suite.

like a billion times!

 These are the things I have learnt:

  • Android/Studio use JUnit 3 so the constructor for your test suite MUST NOT have any parameters (aka don't let Studio create a constructor for you because it will add a parameter and JUnit will not be able to find your test suite):
  • public class TestMainActivity extends ActivityInstrumentationTestCase2<MainActivity> {
    public TestMainActivity() {
    super(MainActivity.class);
    }
    }


  • Your test needs to be in:
    yourModuleDir/src/instrumentTest/java/
    More info
  • The package of your test needs to be different from the package of the class you want to test, i.e: if the package for your activity is
    com.mypacakge.tortoise
    view raw tortoise.java hosted with ❤ by GitHub

    then the test should be in
    com.mypacakge.tortoise.test
  • Don't bother trying to run it from the IDE (don't believe this)
    • Start your emulator
    • Go to <yourProjectDir> (or where your gradlew is)
    • Run ./gradlew connectedInstrumentTest
    • The results can be found at:
      yourProjectDir/yourModuleDir/build/reports/instrumentTests/connected/index.html

Monday, 3 June 2013

Android Studio (Intellij) can't find aapt...but it's there!!! (UPDATED)

UPDATE: I have also encounter this with the following error message (it can be fixed the same way or following this):

db: error while loading shared libraries: libstdc++.so.6: cannot open shared object file: No such file or directory


If you are having problems with Android Studio (Intellij) when trying to run it for the first time with a message like:

android-apt-compiler: Cannot run program "PATH/TO/android-sdk-linux/platform-tools/aapt": java.io.IOException: error=2, No such file or directory


I found this (basically create symbolics links to the actual place where the files are. Didn't do anything for me) and THIS which actually fix it for me.

Turns out, as you can clearly read from the error message, it was a architecture problem. Aapt expects 32 bit binaries and my desktop is 64 bits.

With a nice:
apt-get install ia32-libs
it should work