2

I have in my gradle file something like :

String packageName = com.test.free

I want this String variable packageName to use in my java class. I use Android Studio 1.5.1.

Is it possible? How can I transfer this String from gradle to java ?

I have read similar questions here but none of the solutions worked.

Adrian Ivasku
  • 1,118
  • 3
  • 16
  • 31

2 Answers2

2

Create a gradle task that writes packageName into a java file like:

build.gradle:

task generateGradleValuesJava {
    def java = 
      'public class GradleValues {' +
      '  public static String packageName = "' + project.packageName+ '";' +
      '  }' +
      '}'
    def javaFile = new File('GradleValues.java')
    javaFile.write(java)
}

compileJava.dependsOn generateGradleValuesJava

Compile this file into your jar and use it:

GradleValues.packageName
Gavriel
  • 18,880
  • 12
  • 68
  • 105
2

If that value is really your applicationId, that is already available to you as BuildConfig.APPLICATION_ID.

Otherwise, you can add your own custom fields to BuildConfig. These can include dynamic values:

import static java.util.UUID.randomUUID

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.commonsware.myapplication"
        minSdkVersion 21
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
        debug {
            buildConfigField "String", "FOO", '"'+randomUUID()+'"'
        } 

        release {
            buildConfigField "String", "FOO", '"a49f05b4-f55a-4066-a107-9098c9350f43"'
        }
    }
}
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • No. I have given that value as an example. I have some other data I need to access. – Adrian Ivasku Jan 27 '16 at 18:53
  • BuildConfig I can`t use because the accessed value must be hardcoded. The Value I am reading is dynamic and I am reading it from a String. Is there something like when transferin java String value to native via jni bridge? – Adrian Ivasku Jan 27 '16 at 19:05
  • @AdrianIvasku: "because the accessed value must be hardcoded" -- I don't think so. It has to be properly formatted (e.g., wrap a string value in quotes), but AFAIK it can be generated dynamically via a Groovy expression. "Is there something like when transferin java String value to native via jni bridge?" -- no, for the simple reason that Gradle and Groovy are not on the Android device. – CommonsWare Jan 27 '16 at 19:11
  • @AdrianIvasku: See the updated answer for an example of a dynamically-generated value. – CommonsWare Jan 27 '16 at 19:18
  • It works! Maybe Gavriel`s solution is correct too, but I have a confirmation for this one. My fault, I haven`t mentioned that I use experimental gradle so my solution is like this : buildConfigFields.with { create() { type = "String" name = "packageName" value = '"'+packageName+'"' } } And then call it from java BuildConfig.packageName. Thank you ! – Adrian Ivasku Jan 27 '16 at 20:38