New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BuildConfig fields unable to reference code #50

Closed
psh opened this Issue Jun 21, 2015 · 5 comments

Comments

Projects
None yet
2 participants
@psh

psh commented Jun 21, 2015

I saw this issue in a much larger application, and have reduced it down to just the parts pertinent to the bug.

The project build.gradle file is pretty standard:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.3'
        classpath 'org.codehaus.groovy:gradle-groovy-android-plugin:0.3.6'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

and the "app" build has the custom build config fields:

apply plugin: 'com.android.application'
apply plugin: 'groovyx.grooid.groovy-android'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.example.groovybuildconfig"
        minSdkVersion 16
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        debug {
            buildConfigField "String", "IMAGE_SERVER_URL", "com.example.groovybuildconfig.AppConstants.DEBUG_IMAGE_SERVER_URL"
            buildConfigField "boolean", "DISABLE_CACHING", "true"
            buildConfigField "String", "GOOGLE_ANALYTICS_ID", "\"12345\""
            minifyEnabled false
        }
        release {
            buildConfigField "String", "IMAGE_SERVER_URL", "com.example.groovybuildconfig.AppConstants.PROD_IMAGE_SERVER_URL"
            buildConfigField "boolean", "DISABLE_CACHING", "false"
            buildConfigField "String", "GOOGLE_ANALYTICS_ID", "\"09876\""
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'org.codehaus.groovy:groovy:2.4.3:grooid'
}

The AppConstants class nothing special

package com.example.groovybuildconfig;

public class AppConstants {
    public static final String DEBUG_IMAGE_SERVER_URL = "foo";
    public static final String PROD_IMAGE_SERVER_URL = "bar";
}

Whether the file is a ".groovy" or a ".java" makes no difference, the build still fails

phawke-ml2:GroovyBuildConfig $ ./gradlew clean build
:app:clean
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72220Library
:app:prepareComAndroidSupportSupportV42220Library
:app:prepareDebugDependencies
:app:compileDebugAidl
:app:compileDebugRenderscript
:app:generateDebugBuildConfig
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources
:app:mergeDebugResources
:app:processDebugManifest
:app:processDebugResources
:app:generateDebugSources
:app:processDebugJavaRes UP-TO-DATE
:app:compileDebugJava
/Users/paul.hawke/AndroidProjects/GroovyBuildConfig/app/build/generated/source/buildConfig/debug/com/example/groovybuildconfig/BuildConfig.java:16: error: cannot find symbol
  public static final String IMAGE_SERVER_URL = com.example.groovybuildconfig.AppConstants.DEBUG_IMAGE_SERVER_URL;
                                                                             ^
  symbol:   class AppConstants
  location: package com.example.groovybuildconfig
1 error
:app:compileDebugJava FAILED
:app:compileDebugGroovy

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugJava'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED
@AndrewReitz

This comment has been minimized.

Collaborator

AndrewReitz commented Jul 30, 2015

Does this work without the groovy plugin?

@psh

This comment has been minimized.

psh commented Aug 19, 2015

I sanitized the examples a little, but it came from an existing Java Android project that I was attempting to convert over to using Groovy. So, yes, references from BuildConfig into existing Java source work.

@AndrewReitz

This comment has been minimized.

Collaborator

AndrewReitz commented Jan 15, 2016

@psh I don't this this will ever work since compileJava, will be called before compileGroovy. Unless there is a way to get the BuildConfig to be in the groovy source folder where it will be compile with the rest of the groovy sources.

Another option would be to force groovyCompile to run before javaCompile.

Having these values in the build.gradle seem like a better option overall so they don't get compiled into each version of your application, and instead just included the needed values.

@AndrewReitz AndrewReitz closed this Feb 2, 2016

@AndrewReitz

This comment has been minimized.

Collaborator

AndrewReitz commented Feb 18, 2016

@psh I think I have figured a way to get this to work in some of the newer pieces I have been working on. So open this issue back up.

I think I will just add a flag to the plugin that will enable build wide joint compilation, which will have the groovy compiler compile everything rather than having javac run, then groovyc. This should solve this problem, and be a lot easier to manage than adding the specific BuildConfig.java files to the groovy sourceset.

@AndrewReitz AndrewReitz reopened this Feb 18, 2016

@AndrewReitz

This comment has been minimized.

Collaborator

AndrewReitz commented Feb 24, 2016

Flag to enable joint compilation for all files add in #99

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment