3

I'm using Mac, Android Studio(0.3.6), Robolectric 2.2, Android Annotations 2.7.1, Junit 4.10.

I just built up my project with Android Annotations, so I can see the activity that is using @EActivity(R.layout.activity_some) like

@EActivity(R.layout.activity_some)
public class SomeActivity extends ActionBarActivity implements View.OnClickListener{
    // ...
}

on the device.

However, something happened in my Robolectric tests. All tests passed before using Android Annotations.

I always test with the command ../gradlew clean check in Terminal, and it shows

:MyProject:clean
:MyProject:preBuild UP-TO-DATE
:MyProject:preDebugBuild UP-TO-DATE
:MyProject:preReleaseBuild UP-TO-DATE
:MyProject:prepareComAndroidSupportAppcompatV71900Library
:MyProject:prepareDebugDependencies
:MyProject:compileDebugAidl
:MyProject:compileDebugRenderscript
:MyProject:generateDebugBuildConfig
:MyProject:mergeDebugAssets
:MyProject:mergeDebugResources
:MyProject:processDebugManifest
:MyProject:processDebugResources
:MyProject:generateDebugSources
:MyProject:compileDebug
/Users/StevenKim/AndroidStudio/MyProject/MyProject/src/main/java/com/sk/myproject/alarm/listview/item/MNAlarmItemClickListener.java:8: cannot find symbol
symbol  : class SomeActivity_
location: package com.sk.myproject.alarm.pref
import com.sk.myproject.alarm.pref.SomeActivity_;
                                         ^
Note: Starting AndroidAnnotations annotation processing
Note: AndroidManifest.xml file found: /Users/StevenKim/AndroidStudio/MyProject/MyProject/build/manifests/debug/AndroidManifest.xml
Note: Number of files generated by AndroidAnnotations: 1
Note: Generating source file: com.sk.myproject.alarm.pref.SomeActivity_
Note: Time measurements: [Whole Processing = 453 ms], [Extract Manifest = 158 ms], [Process Annotations = 116 ms], [Extract Annotations = 51 ms], [Find R Classes = 50 ms], [Generate Sources = 38 ms], [Validate Annotations = 37 ms], 
Note: Time measurements: [Whole Processing = 0 ms], 
Note: Time measurements: [Whole Processing = 0 ms], 
warning: The following options were not recognized by any processor: '[androidManifestFile]'
Note:     /Users/StevenKim/AndroidStudio/MyProject/MyProject/src/main/java/com/sk/myproject/common/size/MNViewSizeMeasure.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
:MorningKit:compileTestDebugJava
Note: Starting AndroidAnnotations annotation processing
:MyProject:copyDebugTestResources
:MyProject:processTestDebugResources UP-TO-DATE
:MyProject:testDebugClasses
:MyProject:testDebug

com.sk.myproject.alarm.listview.MNAlarmListViewTest > mainAlarmListAdaptorTest STARTED

com.sk.myproject.alarm.listview.MNAlarmListViewTest >         mainAlarmListAdaptorTest FAILED

...

One of the tests is this.

@RunWith(RobolectricGradleTestRunner.class)
public class SomeActivityTest {

    SomeActivity someActivity;

    @Before
    public void setUp() {
        ShadowLog.stream = System.out;

        someActivity = Robolectric.buildActivity(SomeActivityTest.class).create().visible().get();

// ...

Like SomeActivityTest, all tests containing Robolectric stuff failed because of NullPointException at onCreate() of Activities.

My main code worked on Android Studio, but Robolectric test code using command in Terminal failed. I think it's about compile timing with my build.gradle file.

in my build.gradle file I copy & paste the Android Annotations part

// ... etc

ext.androidAnnotationsVersion = '2.7.1';

configurations {
    apt
}

dependencies {

    // ... etc

    // Android Annotations part
    apt "com.googlecode.androidannotations:androidannotations:${androidAnnotationsVersion}"
    compile "com.googlecode.androidannotations:androidannotations-api:${androidAnnotationsVersion}"

}

def getSourceSetName(variant) {
    return new File((String)variant.dirName).getName();
}

android.applicationVariants.all { variant ->
    def aptOutputDir = project.file("build/source/apt")
    def aptOutput = new File(aptOutputDir, variant.dirName)
    println "****************************"
    println "variant: ${variant.name}"
    println "manifest:  ${variant.processResources.manifestFile}"
    println "aptOutput:  ${aptOutput}"
    println "****************************"

    android.sourceSets[getSourceSetName(variant)].java.srcDirs+= aptOutput.getPath()

    variant.javaCompile.options.compilerArgs += [
            '-processorpath', configurations.apt.getAsPath(),
            '-AandroidManifestFile=' + variant.processResources.manifestFile,
            '-s', aptOutput
    ]

    variant.javaCompile.source = variant.javaCompile.source.filter { p ->
        return !p.getPath().startsWith(aptOutputDir.getPath())
    }

    variant.javaCompile.doFirst {
        aptOutput.mkdirs()
    }
}

Do you guys have an idea for this? Please help me :)

Wooseong Kim
  • 1,871
  • 2
  • 21
  • 19
  • You have higher chances of fixing this issue on [AndroidAnnotations forum](https://groups.google.com/forum/#!forum/androidannotations). – gunar Dec 04 '13 at 15:36
  • I tried AndroidAnnotations:3.0-SNAPSHOT and it worked now(still with 'cannot find symbol' but anyway tests can pass), but now I can't use ButterKnife(4.0.1) in my project. I modify all sources using ButterKnife. I think it crashes somewhere(Because they both use Annotation Processing) – Wooseong Kim Dec 04 '13 at 17:05
  • why do you need Android annotations and Butter knife both in your application? – Eugen Martynov Dec 04 '13 at 22:48
  • I was using ButterKnife for ViewHolder Pattern in List Adapter. I'm digging how I can change it to Androud Annotation :) – Wooseong Kim Dec 05 '13 at 01:57
  • Easy peasy: [link](https://github.com/excilys/androidannotations/wiki/Adapters-and-lists) – khose Jan 29 '14 at 15:52
  • @WooseongKim Have you found out how to make androidannotations work for viewholder? – Chris.Zou Jun 02 '15 at 01:33
  • @Chris.Zou I'm not using AndroidAnnotations anymore. It feels like it's annoying to me a little. – Wooseong Kim Jun 02 '15 at 04:42
  • @WooseongKim I feel uncomfortable having to use the generated "_" classes too, thinking about a transition to ButterKnife totally. – Chris.Zou Jun 03 '15 at 09:51
  • I think you are not pointing to your activity. Robolectric.buildActivity(SomeActivity_.class), same goes with having the underscore in your manifest file. I have used AndroidAnnotations and yes in the beginning it was annoying to remember that, but then it became second nature. – Juan Mendez Mar 09 '17 at 01:03

0 Answers0