3

I am struggling to get AutoValue working in an Android project. It seems to be working for others, so I must just be missing something. I've tried enabling the jack compiler, that didn't seem to help.

Here is a demo project that fails when I build it locally. The complaint is in this class, where it can't find the AutoValue_Repo constructor.

Here is my project level build.gradle:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.1'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

And there is my module level build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.2"
    defaultConfig {
        applicationId "org.cse390.githubhotness"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    provided 'com.google.auto.value:auto-value:1.2-rc1'
    compile "com.google.auto.value:auto-value:1.2-rc1"
    apt "com.google.auto.value:auto-value:1.2-rc1"

    compile 'com.jakewharton:butterknife:8.4.0'
    apt 'com.jakewharton:butterknife-compiler:8.4.0'

    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.android.support:design:24.2.1'

    testCompile 'junit:junit:4.12'
}

Here is the class that isn't being auto-valued:

@AutoValue
public abstract class Repo {
  public abstract int id();
  public abstract String name();
  public abstract String fullName();
  public abstract String description();
  public abstract int numStars();
  public abstract String language();
  public abstract String htmlUrl();

  static Repo create(int id, String name, String fullName, String description,
                     int numStars, String language, String htmlUrl) {
    // See "How do I...?" below for nested classes.
    return new AutoValue_Repo(id, name, fullName, description, numStars,
        language, htmlUrl);
  }

}

Any obvious mistakes here?

user1978019
  • 3,008
  • 1
  • 29
  • 38
  • Because the class is being generated access to the create method must be public autovalue can work. – Eoin Oct 19 '16 at 05:25
  • Of all the things I tried (including changing visibility of the class and the member variables), I apparently neglected to try that. But indeed it works. If you promote your comment to an answer I'll accept it. I should also note, in Android Studio if I fiddle with the visibility of the static method I have to manually invoke a make for it to pick up errors or fix errors. This may have hidden this fix from me previously, though I can't swear to it. Thank you! – user1978019 Oct 19 '16 at 05:36
  • Im glad I could help. Ive had similar problems before with dagger generating class. :) – Eoin Oct 19 '16 at 05:46

2 Answers2

3

Because we are generating a class here access to the method needs to be public so autovalue can work.

  public static Repo create(int id, String name, String fullName, String description,
                 int numStars, String language, String htmlUrl) {
        // See "How do I...?" below for nested classes.
        return new AutoValue_Repo(id, name, fullName, description, numStars,
                    language, htmlUrl);
   }
Eoin
  • 4,050
  • 2
  • 33
  • 43
0

In my case I forgot to set @AutoValue above class description. Also the class should be external, not inner.

CoolMind
  • 26,736
  • 15
  • 188
  • 224