Skip to content

klarna/gradle-react-native

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

69 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Gradle React Native Plugin

CircleCI codecov gradlePluginPortal GitHub

Setup

Gradle's kotlin-dsl:

// build.gradle.kts
plugins {
    id "com.klarna.gradle.reactnative" version "$version" apply false
}
// project build.gradle.kts
plugins {
    id("com.android.application")
    id("com.klarna.gradle.reactnative")
}

Or via the buildscript block:

// rootProject build.gradle
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath "com.klarna.gradle.reactnative:plugin:$version"
    }
}
// project build.gradle
apply plugin: "com.android.application"
apply plugin: "com.klarna.gradle.reactnative"

Usage

Minimalistic

// project build.gradle
apply plugin: "com.android.application"
apply plugin: "com.klarna.gradle.reactnative"

After applying plugin all tasks will be created automatically and attached to the build graph.

Extended

// project build.gradle
apply plugin: "com.android.application"
apply plugin: "com.klarna.gradle.reactnative"

/* https://developer.android.com/studio/build/index.html */
android {
    /* ... Configuration of the Android app ... */
}

react {
    /* declare custom configuration options for each build type */
    buildTypes {
        debug {
            /* ... */
        }
        release {
            /* ... */
        }
        /* ... more build types ... */
    }
    
    /* declare custom configuration options for each flavor */
    productFlavors {
        local {
            /* ... */
        }
        /* ... more flavors ... */
    }
    
    /* Reconfigure `android.packagingOptions{...}` for supporting well JSC integration. */
    applyJscPackagingOptions()
}

Build types and Flavors are corresponding the Android Build Plugin - android_build_variants

Compatibility

Our plugin is designed for replacing the old React Native react.gradle code by properly maintained and heavily tested code. Just for that purpose everything in plugin configuration is backward compatible with old approaches.

We are targeting to replace RN 0.61 build scripts.

Those lines of code below are equal:

project.ext.react = [
    entryFile   : "index.js",
    enableHermes: false,  // clean and rebuild if changing
]

// replaced by

react {
    entryFile = "index.js"
    enableHermes = false
}

When you use the plugin DSL for configuration, plugin will take care for reflecting any defined configuration in project.ext.react array/collection. In other words you can combine old and approach and current plugin in one project at the same time.

The biggest advantage of the plugin that all the variables definitions are type safe and validated in the moment of gradle script running/editing (depends on IDE you use).

Contribute

Enable Git Hooks

In the root folder of the project you can find folder .githooks inside it you can find pre-configured hooks. To enable them just copy all files from .githooks to .git/hooks.

OR which is a bette approach create a symbolic links to hooks:

cd .git/hooks
# drop all old hooks
rm *
# create all symbolic links
find ../../.githooks -type f -exec basename {} \; | xargs -I {} ln -s ../../.githooks/{} {}

Publishing

Do login:

./gradlew login

Output:

# Task ':login' is not up-to-date because:
#  Task has not declared any outputs despite executing actions.
#
# To add your publishing API keys to /usr/local/opt/gradle/gradle.properties, open the following URL in your browser:
#
#   https://plugins.gradle.org/api/v1/login/auth/{token}
#
# Your publishing API keys have been written to: $GRADLE_USER_HOME/gradle.properties

Copy gradle.publish.key and gradle.publish.secret keys:

cat $GRADLE_HOME/gradle.properties
#Updated secret and key with server message: Using key 'C02WW29ZHTDD' for OlKu
#Thu, 26 Sep 2019 09:41:39 +0200
gradle.publish.key={secret}
gradle.publish.secret={secret2}

Create file in root of the project credentials.properties and place those keys into it.

# increase version MINOR part, switch to RELEASE from any (alpha, beta, rc) and force tag apply
gradle/version-up.sh --minor --release --apply

# take version from properties file
export VER=$(cat version.properties | grep snapshot.version | cut -d'=' -f2)

# switch to release TAG as a branch
git checkout -b $VER $VER

# force full rebuild
./gradlew clean build assembleRelease -Pversion=$VER

# now we can publish the version
./gradlew publishPlugins -Pversion=$VER

Why Not A Standard Approach

as a developer I work with multiple projects, companies, repositories. Main rule for me is to keep projects as much as possible isolated from local environment of the laptop. That is why credentials.properties approach used - isolate project from global system settings.

References:

Run CircleCI locally

based on: https://circleci.com/docs/2.0/local-cli/

brew install circleci

# validate config
circleci config validate

# run specific job
circleci local execute --job [dependencies|debug|release|test|lint|deploy] -e VAR1=FOO

## run tests 1/4 - specifically second slice: 2
circleci local execute --job test -e TEST_START=2 -e TEST_TOTAL=4

# cleanup
brew uninstall circleci

Snapshots development

If you want to use the latest development SNAPSHOT version on a regular basis, you will need to also need to add the following to the buildscript block to ensure Gradle checks more frequently for updates:

buildscript {
  // ...
  /* Since the files in the repository change with each build, we need to recheck for changes */
  configurations.all {
    resolutionStrategy {
      cacheChangingModulesFor 0, 'seconds'
      cacheDynamicVersionsFor 0, 'seconds'
    }
  }
}

// `-SNAPSHOT` suffix automatically recognized as `changing` dependency
dependencies {
    compile group: "groupId", name: "artifactId", version: "1+", changing: true
    compile group: "groupId", name: "artifactId", version: "1.0-SNAPSHOT"
}

References:

References

Legal

This project is available under the Apache 2.0 License.

Copyright 2013-2019 Klarna AB.