nenick / AndroidStudioAndRobolectric

Minimal Robolectric and Android Studio example
139 stars 29 forks source link

Your work around is not working with apply plugin: 'com.android.library' #23

Closed bskania closed 9 years ago

bskania commented 9 years ago

We have downloaded AndroidStudioAndRobolectric-library-with-aar.zip and try to run testcase for core package and we were getting issue for same android/support/v7/appcompat/R$styleable java.lang.NoClassDefFoundError: android/support/v7/appcompat/R$styleable

Is it a open issue ?

nenick commented 9 years ago

This shouldn't be an issue current. When do you get this message (command line, Android Studio)?

I will check this.

bskania commented 9 years ago

When I try to run my test cases of Core library this error message is coming and that is through android studio

nenick commented 9 years ago

Ok now I got the same error. The base issue is that the app module don't get compiled if you only start tests for the core module. With Android Studio all works fine. As workaround be sure to compile the main module when you start tests.

./gradlew assembleDebug core:test

I have added a notice to the readme.

bskania commented 9 years ago

if I try this command it is giving me error stated below

java.lang.UnsupportedOperationException: Robolectric does not support API level 1.

nenick commented 9 years ago

Which OS do you use? Do you see a Warning that the AndroidManifest.xml could not be found? This is most the base issue.

bskania commented 9 years ago

I am using windows OS. Android Studio 1.2.1.1 If I change your file CustomRobolectricRunner with below code then It is giving me Warning of AndroidManifest.xml not found

public class CustomRobolectricRunner extends RobolectricGradleTestRunner {

public CustomRobolectricRunner(Class<?> klass) throws InitializationError {
    super(klass);
}

protected AndroidManifest getAppManifest(Config config) {
    AndroidManifest appManifest = super.getAppManifest(config);
    FsFile androidManifestFile = appManifest.getAndroidManifestFile();

    // Workaround also wrong paths for res and assets.
    // This will be fixed with next robolectric release https://github.com/robolectric/robolectric/issues/1709
    if (androidManifestFile.exists()) {
        FsFile resDirectory = FileFsFile.from(appManifest.getAndroidManifestFile().getPath().replace("AndroidManifest.xml", "res"));
        FsFile assetsDirectory = FileFsFile.from(appManifest.getAndroidManifestFile().getPath().replace("AndroidManifest.xml", "assets"));
        return new AndroidManifest(androidManifestFile, resDirectory, assetsDirectory);
    } else {
        String moduleRoot = getModuleRootPath(config);
        androidManifestFile = FileFsFile.from(moduleRoot, appManifest.getAndroidManifestFile().getPath());
        FsFile resDirectory = FileFsFile.from(moduleRoot, appManifest.getAndroidManifestFile().getPath().replace("AndroidManifest.xml", "res"));
        FsFile assetsDirectory = FileFsFile.from(moduleRoot, appManifest.getAndroidManifestFile().getPath().replace("AndroidManifest.xml", "assets"));
        return new AndroidManifest(androidManifestFile, resDirectory, assetsDirectory){
            @Override
        public int getTargetSdkVersion(){
                return 19;
            }
        };
    }
}

private String getModuleRootPath(Config config) {
    String moduleRoot = config.constants().getResource("").toString().replace("file:", "").replace("jar:", "");
    return moduleRoot.substring(0, moduleRoot.indexOf("/build"));
}

}

bskania commented 9 years ago

Hi ..Thanks for your support. We are able to execute testcase with application having appCompactActivity works fine.

Now I am working on UI library which is having appCompactActivity dependency [It is a library project].

With that still it is having

android/support/v7/appcompat/R$styleable java.lang.NoClassDefFoundError: android/support/v7/appcompat/R$styleable at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:148)

nenick commented 9 years ago

With robolectric 3.0-rc3 the workaround for "wrong paths" should not necessary anymore. I updated the examples.

bskania commented 9 years ago

You are right, your work around is not required for robolectric 3.0-rc3.

But this version is having still issue as with library project testcase (library is having android.support.v7.appcompat dependency) Is it still open issue ?

nenick commented 9 years ago

I think I found the difference between us. I tried it on Windows and did not get this error when I use a command line tool like CygWin. Only when I use the default command line tool from Windows I get the missing R class message.

I have updated the example so that the correct sperator is used at any environment.

bskania commented 9 years ago

Thanks nenick for doing this update. But after getting your updated project I am getting error of PNG files missing from support library.

nenick commented 9 years ago

Do you mean this?

AAPT err(532696699): D:\Workspace\AndroidStudioAndRobolectric\app\build\intermed
iates\exploded-aar\com.android.support\appcompat-v7\22.0.0\res\drawable-hdpi-v4\
abc_spinner_mtrl_am_alpha.9.png: libpng warning: iCCP: Not recognizing known sRG
B profile that has been edited

This is not a real error.

bskania commented 9 years ago

Yes.

I ignored that error and Try to test Core library project with command gradlew assembleDebug core:test

It is giving error You need to use a Theme.AppCompat theme (or descendant) with this activity. java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

I double check with the CoreActivity manifest declaration. It is using Theme as AppTheme. Which is of Theme.AppCompact.

nenick commented 9 years ago

The message is clear but I don't know why you get it. I can't reproduce this error.

I downloaded the zip file from github, using the command line from windows and use the same command to run the tests from core module. No error.

I have add a button which open the core activity when you install the app. When it is a true error, then this should fail.

bskania commented 9 years ago

I am not able to run application because of below error.

:app:mergeDebugResources FAILED Error:Execution failed for task ':app:mergeDebugResources'. Crunching Cruncher abc_ic_commit_search_api_mtrl_alpha.png failed, see logs

nenick commented 9 years ago

Again I don't have this issue. Please try if a simple app created by Android Studio works for you.

bskania commented 9 years ago

@nenick Thanks It works for me too.

nenick commented 9 years ago

You switch the plugin from application to library at the app module?

Yes then you will have some problems. If you want a second library module you should copy the core module. On this way you will copy necessary workarounds.