androidannotations / androidannotations

Fast Android Development. Easy maintainance.
Other
11.06k stars 2.35k forks source link

AndroidAnnotations Gradle packagNameSuffix workaround doesn't work #690

Closed jameswyly closed 11 years ago

jameswyly commented 11 years ago

According to disucssion here and the stackoverflow comment posted by tbruyelle here , one must simply add the '-AresourcePackageName=yourPackage' argument to your gradle build in order to successfully build gradle with android annotations and the packageNameSuffix set.

However, this does not work. In fact I see no difference between using this argument and not using it.

My current build.gradle is an exact copy of the recommended style here, with the addition of a few libraries and the new argument above.

Specifically it looks something like:

... variant.javaCompile.options.compilerArgs += [ '-processorpath', configurations.apt.getAsPath(), '-AandroidManifestFile=' + variant.processResources.manifestFile, '-AresourcePackageName=HARDCODED_PACKAGE_NAME', '-s', aptOutput ] ... We know the libraries work on our project because we are able to successfully build the gradle project without the packageNameSuffix flag.

Is there an obvious error I'm making, or does the proposed fix just not work at all? Basically we get the normal errors of Note: Starting AndroidAnnotations annotation processing Note: AndroidManifest.xml file found: {redacted}\AndroidManifest.xml warning: The AndroidManifest.xml file was found, but not the compiled R class: {redacted}.debug.R {redacted}\MainActivity.java:41: Resource id value not found in R.layout: 2130903040 @EActivity(R.layout.activity_main) ^

What is interesting though is that I do get warnings stating

warning: The following options were not recognized by any processor: '[androidManifestFile, resourcePackageName]'

Which would seem to indicate there is some kind of error somewhere, or maybe that's an unrelated bug... But we seem to be unable to get the packageNameSuffix to work properly regardless.

DayS commented 11 years ago

These two annotation processing options are only available since AA 3.0. As we don't release the version yet, you can fork the project, put a tag on your local copy (to keep some trace of what you're doing) and work with this version.

tbruyelle commented 11 years ago

Moreover, the latest version 0.5.5 of the android plugin requires now to use android.applicationVariants.all rather than android.applicationVariants.each. So if you use a dependency like that "com.android.tools.build:gradle:0.5.+", then you use version 0.5.5 since the 1st august and you have to apply this change in your build script.

jameswyly commented 11 years ago

@DayS Sorry I didn't give enough information in my base post... We're actually using the 3.0-SNAPSHOT jars from the sonatype repository. I considered building locally as you say but had not yet got around to attempting that, and I didn't see any instructions anywhere on how to do so.

@tbruyelle Is that true? Wouldn't the change for that then imply that a normal build without the packageNameSuffix set also fail? I will try as you suggest anyway

I will try today to generate the smallest possible project to reproduce this but I may not get around to it. Feel free to let me know if you have any other suggestions

tbruyelle commented 11 years ago

@jameswyly yes, it seems it's a breaking change for all scripts that uses android.[application|Library|Test]Variants

jameswyly commented 11 years ago

@tbruyelle Sorry maybe I'm not being clear... In the OP I stated that we were able to build the project just fine in cases where we didnt use packageNameSuffix, so in my last post I was just expressing skepticism that the specific fix you stated would change anything for this specific issue. However I will try it later today when I get a moment, thank you for your quick reply

tbruyelle commented 11 years ago

@jameswyly Your issue is probably because you didn't use the latest 3.0-SNAPSHOT of AA. But moreover if you use the version 0.5.5 of the android plugin you have to use android.appliationVariants.all to make things work.

jameswyly commented 11 years ago

I've still been unable to get this to work... Here's my entire build.gradle, which once again works just fine with annotations when i'm not using the packageNameSuffix variable (Release build below works, debug does not)

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}
apply plugin: 'android'

repositories {
    mavenCentral()
    maven {
        url 'https://oss.sonatype.org/content/repositories/snapshots/'
    }
}

ext.androidAnnotationsVersion = '3.0-SNAPSHOT';

configurations {
    apt
}
dependencies {
    compile 'com.google.android.gms:play-services:3.1.36'
    compile files(fileTree(dir: '/Android/libs', includes: ['*.jar']))
    compile 'com.android.support:support-v4:13.0.+'
    compile project(':libs:facebook-android-sdk')
    compile project(':libs:ptr-library')
    compile project(':libs:PagerSlidingTabStrip')
    compile project(':libs:PhotoView')

    apt "com.googlecode.androidannotations:androidannotations:${androidAnnotationsVersion}"
    compile "com.googlecode.androidannotations:androidannotations-api:${androidAnnotationsVersion}"

}
android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    sourceSets {
        main {
            java.srcDirs = ['/Android/src']
            manifest.srcFile 'AndroidManifest.xml'
            resources.srcDirs = ['/Android/src']
            assets.srcDirs = ['/Android/assets']
            res.srcDirs = ['Android/res']
        }

    }

    signingConfigs {
        release {

                 [RELEASE KEY DATA]
        }
    }

    buildTypes {

        debug {
            packageNameSuffix "debug"
        }

        release {
            signingConfig signingConfigs.release
        }

    }

... 
[PRODUCT FLAVOR FOR DIFFERENCE RESOURCE FOLDERS HERE]
...

}

def getSourceSetName(variant) {
    return new File(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,
        '-AresourcePackageName=[PACKAGE_NAME_STRING]',
        '-Atrace',
        '-s', aptOutput
    ]

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

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

}
tbruyelle commented 11 years ago

I didn't test the build config with product flavors, maybe it is why it fails. Please post the full build.gradle file in a gist.

jameswyly commented 11 years ago

@tbruyelle Apologies, that's a bit of a red herring, there are flavors there but they are currently empty. I've removed them completely and still see the same results, so the current entire gradle file is shown above.

I've also tried using the (resName="layout|id|drawable|view name") paradigm but it doesn't seem to change anything either.

I also triple checked to make sure that the gradle version I'm using is 1.6 and made sure that it's the only version available on my system.

I had suspicions that there may have been isuses related to implicit annotations (ViewById, etc), but now the first error I show is from an @EActivity / @EFragment (resName="...") line.... Identical above just with the change to resName instead of R.layout.zzzz

jameswyly commented 11 years ago

@tbruyelle ok so I've solved the problem... It seems that the jars at the com.googlecode.androidannotations are out of date, I've confirmed this on the https://oss.sonatype.org/content/repositories/snapshots/ repository as well. The com.googlecode version of the annotations jars stopped updating in November of this past year (but still has a 3.0-SNAPSHOT) version.

Switching over to the one from the org.androidannotations maven actually gives a release from only a few days or weeks ago.

Sorry for all of the confusion, but this may want to be added to the knowledge base if it wasn't already common knowldege.

To clarify : Changing the above file to use

org.androidannotations:androidannotations:${androidAnnotationsVersion}

instead of

com.googlecode.androidannotations:androidannotations:${androidAnnotationsVersion}

caused everything to now work as expected. I noticed the difference related to the new post here when comparing build files.

Thanks for all of the assistance

tbruyelle commented 11 years ago

Yes the package has changed in 3.0, sorry I didn't notice it in your script. Happy this works now for you :)