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.

2 Answers 2

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"'
        }
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

No. I have given that value as an example. I have some other data I need to access.
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?
@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.
@AdrianIvasku: See the updated answer for an example of a dynamically-generated value.
It works! Maybe Gavriels solution is correct too, but I have a confirmation for this one. My fault, I havent 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 !
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

1 Comment

I think I understand it now. I`l try this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.