React-native-config: Cannot get property 'args' on null object when running gradle build

Created on 7 Dec 2016  路  9Comments  路  Source: luggit/react-native-config

This happens when I attempt to set project.ext.envConfigFiles...any ideas?

Most helpful comment

I was able to fix this by changing the getCurrentFlavor method of dotenv.gradle to the following:

def getCurrentFlavor() {
    Gradle gradle = getGradle()
    String tskReqStr = gradle.getStartParameter().getTaskRequests().toString()

    Pattern pattern = Pattern.compile("(assemble|generate|install)(.*?)(Debug|Release)")
    Matcher matcher = pattern.matcher(tskReqStr)

    if( matcher.find() ) {
        String variant = matcher.group(2) + matcher.group(3)
        return variant.toLowerCase()
    } else {
        return "";
    }
}

The pattern variable above assumes that your variants are named debug, release, or end with debug or release. For example, my staging build variant is "betaRelease"

To be able to use the updated method without modifying node_modules, I did the following:

  1. Copy the dotenv.gradle file from https://github.com/luggit/react-native-config/blob/master/android/dotenv.gradle and place it in my project
  2. Paste in the updated getCurrentFlavor code
  3. In your build.gradle file, replace:
    apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"
    with:
    apply from: "../dotenv.gradle"

All 9 comments

Most suggestions online for how to get the current flavor suggest using:

String tskReqStr = gradle.getStartParameter().getTaskRequests().toString() vs. grabbing the first task and then getting the args.

Updating the dotenv.gradle file to do that check similar to this http://stackoverflow.com/questions/32657838/gradle-get-current-flavor-for-a-specific-task?answertab=votes#tab-top seems to work. I don't have time to do a PR right now as it needs more testing but figured I'd let you know

For me the orginal String tskReqStr = gradle.getStartParameter().getTaskRequests()[0].args works fine with compileSdkVersion 23 buildToolsVersion "23.0.1" and Android Studio, and it will fail the gradle sync and generate above error with compileSdkVersion 24 buildToolsVersion '24.0.0' in Android Studio.

@corymsmith 's String tskReqStr = gradle.getStartParameter().getTaskRequests().toString() fix the gradle sync error, however it's always going to read env from .env instead of alternatives under staging or release variant when using command-line.

Same here. Configuring project.ext.envConfigFiles makes me unable to sync the gradle files.

@mlanter So #57 resolves this?

@brianjd Yep it should. Let me know if you have any issues with it.

I was able to fix this by changing the getCurrentFlavor method of dotenv.gradle to the following:

def getCurrentFlavor() {
    Gradle gradle = getGradle()
    String tskReqStr = gradle.getStartParameter().getTaskRequests().toString()

    Pattern pattern = Pattern.compile("(assemble|generate|install)(.*?)(Debug|Release)")
    Matcher matcher = pattern.matcher(tskReqStr)

    if( matcher.find() ) {
        String variant = matcher.group(2) + matcher.group(3)
        return variant.toLowerCase()
    } else {
        return "";
    }
}

The pattern variable above assumes that your variants are named debug, release, or end with debug or release. For example, my staging build variant is "betaRelease"

To be able to use the updated method without modifying node_modules, I did the following:

  1. Copy the dotenv.gradle file from https://github.com/luggit/react-native-config/blob/master/android/dotenv.gradle and place it in my project
  2. Paste in the updated getCurrentFlavor code
  3. In your build.gradle file, replace:
    apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"
    with:
    apply from: "../dotenv.gradle"

I made some modifications to the above to work with my setup. Some of the various solutions would work in command line, but not build when pressing "Run" in Android Studio, or would cause an exception during gradle sync.

This tweaks the above so that it doesn't presume your variants end in debug or release. It does strip the word "Sources" of the end of any builds - this gets appended during gradle sync, but we can still detect the build flavor if we strip that word off.

def getCurrentFlavor() {
    Gradle gradle = getGradle()
    List<TaskExecutionRequest> taskRequests = gradle.getStartParameter().getTaskRequests();
    String tskReqStr = taskRequests.toString()

    //tskReqStr will be null or the requests array will be empty during first pass in gradle sync
    if( tskReqStr != null && !taskRequests.isEmpty() ) {
        String firstTaskRequestArgsString = gradle.getStartParameter().getTaskRequests()[0].args.toString();

        Pattern pattern = Pattern.compile("(assemble|generate|install)(\\w+)")
        Matcher matcher = pattern.matcher(firstTaskRequestArgsString)

        if (matcher.find()) {
            String variant = matcher.group(2);
            //Strip "Sources" from the end, means Android Studio gradle sync will be handled correctly
            if (variant.endsWith("Sources")) {
                variant = variant.substring(0,variant.length() - 7)
            }
            variant = variant.toLowerCase();
            return variant;
        } else {
            return "";
        }
    } else {
        return "";
    }
}

If https://github.com/luggit/react-native-config/pull/57 gets finished and merged, it will probably fix this - but for now I'm forking the project, adding this change, and adding it as a package.json dependency - you can do the same, or follow @SBShane's instructions above for adding the dotenv.gradle file into your project.

Should be working on the latest 0.11.1, let me know if you're still running into issues!

Thanks folks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Quadriphobs1 picture Quadriphobs1  路  3Comments

jemise111 picture jemise111  路  4Comments

timscott picture timscott  路  5Comments

jstlaurent picture jstlaurent  路  4Comments

NachtRitter picture NachtRitter  路  4Comments