使用 AndroidAnnotations 和 Fragments 编译库项目

Compile Library project with AndroidAnnotations and Fragments

我目前正在开发一个 Android 库,该库使用 Android 注释开发,并使用了一些片段。当它被编译为 Android 应用程序时,代码没有问题,一切正常。当它被编译为 Android 库时,它会中断,因为它找不到任何 Android 生成的 class 注释。

我用 @EActivity(resName="activity_name") 而不是 @EActivity(R.layout.activity_name) 对每个 activity 进行了注释,这更正了一些错误。

我遇到的问题是,在其中一些活动中,我动态创建了一些像这样的片段(例如):

PhotoFragment fragment = PhotoFragment_.builder().someParams("a string param").build();

当我尝试编译为 Android 库时,此调用失败,因为它找不到动态生成的 PhotoFragment_ class。有没有办法让它工作?是通过改变我创建片段的方式,还是通过配置 AndroidAnnotations?

编辑 2016 年 4 月 20 日

我的build.gradle(模块级别):

apply plugin: 'com.android.library'
apply plugin: 'android-apt'
def AAVersion = '4.0.0'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'

    // Barcode library (ZXing)
    compile 'com.journeyapps:zxing-android-embedded:3.0.2'
    compile 'com.google.zxing:core:3.2.0'

    // Android Annotations
    apt "org.androidannotations:androidannotations:$AAVersion"
    compile "org.androidannotations:androidannotations-api:$AAVersion"

    // Android Bootstrap
    compile 'com.beardedhen:androidbootstrap:2.1.0'

    // Gson
    compile 'com.google.code.gson:gson:2.4'

    // Some auto-generated BS
    compile 'com.android.support:design:23.2.1'
    compile 'com.android.support:support-v4:23.2.1'

    // Robotium --> Emulate User Interaction on tests
    compile 'com.jayway.android.robotium:robotium-solo:5.5.4'

    // Android Testing
    androidTestCompile 'com.android.support:support-annotations:23.2.1'
    androidTestCompile 'com.android.support.test:runner:0.5'
    androidTestCompile 'com.android.support.test:rules:0.5'

    // OkHTTP (HTTP Client Library)
    compile 'com.squareup.okhttp3:okhttp:3.2.0'
}

apt {
    arguments {
        library 'true'
    }
}

非常感谢!

经过讨论,原来是代码中存在编译错误(与注解处理器无关)。这是甚至没有调用 AndroidAnnotations 处理器,也没有生成 classes。

解决方案是修复 "normal" 编译错误,然后注释处理器将 运行。这很难,因为错误会隐藏在大量生成的 class not found 错误中,因此需要彻底阅读。