首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AutoValue在Android项目中找不到生成的构造函数

AutoValue在Android项目中找不到生成的构造函数
EN

Stack Overflow用户
提问于 2016-10-19 05:02:50
回答 2查看 1.9K关注 0票数 3

我很难让AutoValue在一个安卓项目中工作。这似乎是为他人工作,所以我一定是错过了什么。我试过启用jack编译器,这似乎没什么用。

这里是一个演示项目,当我在本地构建它时会失败。抱怨发生在这个类中,在那里它找不到AutoValue_Repo构造函数。

这是我的项目级别build.gradle

代码语言:javascript
复制
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
}

这是我的模块级build.gradle

代码语言:javascript
复制
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'
}

下面是不被自动估值的类:

代码语言:javascript
复制
@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);
  }

}

这里有明显的错误吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-10-19 05:45:09

因为我们在这里生成一个类,所以对方法的访问需要是公共的,所以autovalue可以工作。

代码语言:javascript
复制
  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);
   }
票数 3
EN

Stack Overflow用户

发布于 2018-04-10 10:00:13

在我的例子中,我忘记了在类描述上设置@AutoValue。此外,类应该是外部的,而不是内部的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40122530

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档