Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android BuildConfig Issues while adding properties through Gradle

Tags:

android

gradle

I am adding few properties in BuildConfig through build.gradle as below

release_prod{
       buildConfig "public static final String ENVIRONMENT = \"prod\";"
    }
    release_dev{
           buildConfig "public static final String ENVIRONMENT = \"dev\";"
    }

The problem is when I build from gradle, it works fine but when I compile project in eclipse I get errors because this variable is NOT present in gen BuildConfig.

My question is

  1. Is there a way to add few variables in BuildConfig, so that its generated from eclipse at buildtime
  2. If NOT, Is there anyway I can generate properties from gradle in a separate file other than BuildConfig.
like image 401
CommonMan Avatar asked Sep 19 '13 01:09

CommonMan


1 Answers

  1. I didn't find a simple way to make Eclipse generate the BuildConfig class.

  2. I develop mainly with Eclipse ADT and then release different flavors with Gradle so I came up with the following solution:

    Create my own CustomBuildConfig class, add it to Eclipse build path and use Gradle sourceSets to select different version of this class.


In Project/build.gradle, define an additional source folder, specific to a flavor:

android {
  sourceSets {
    release_prod {
        java.srcDirs = ['src', 'flavors/prod/src' ]
    }
    release_dev {
        java.srcDirs = ['src', 'flavors/dev/src' ]
    }
}

The production env is defined in Project/flavors/prod/src/CustomBuildConfig.java:

package com.example.project;

public class CustomBuildConfig {
    public static final String ENVIRONMENT = "prod";
}

The development env is defined in Project/flavors/dev/src/CustomBuildConfig.java:

package com.example.project;

public class CustomBuildConfig {
    public static final String ENVIRONMENT = "dev";
}

And finally in Eclipse I add only the Project/flavors/dev/src/ to the build path.

like image 128
louisbl Avatar answered Oct 25 '22 20:10

louisbl