Gradle

From bibbleWiki
Jump to navigation Jump to search

Setup

  • Ubuntu 20.10
  • Java 8
  • Gradle 6.8.1

Get Software

wget https://services.gradle.org/distributions/gradle-${VERSION}-bin.zip -P /tmp
sudo unzip -d /opt/gradle /tmp/gradle-${VERSION}-bin.zip
sudo ln -s /opt/gradle/gradle-${VERSION} /opt/gradle/latest

Create Profile

touch /etc/profile.d/gradle.sh
chmod 777 /etc/profile.d/gradle.sh

Edit Profile

export GRADLE_HOME=/opt/gradle/latest
export PATH=${GRADLE_HOME}/bin:${PATH}

Kotlin (JVM)

Setup was very simple

gradle init --type kotlin-application

build.gradle.kts

This was the output from the above command

/*
 * This file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Kotlin application project to get you started.
 */

plugins {
    // Apply the Kotlin JVM plugin to add support for Kotlin.
    id("org.jetbrains.kotlin.jvm") version "1.3.72"

    // Apply the application plugin to add support for building a CLI application.
    application
}

repositories {
    // Use jcenter for resolving dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

dependencies {
    // Align versions of all Kotlin components
    implementation(platform("org.jetbrains.kotlin:kotlin-bom"))

    // Use the Kotlin JDK 8 standard library.
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")

    // Use the Kotlin test library.
    testImplementation("org.jetbrains.kotlin:kotlin-test")

    // Use the Kotlin JUnit integration.
    testImplementation("org.jetbrains.kotlin:kotlin-test-junit")
}

application {
    // Define the main class for the application.
    mainClassName = "test7.AppKt"
}

File Structure

src
├── main
│   ├── kotlin
│   │   └── test7
│   │       └── App.kt
│   └── resources
└── test
    ├── kotlin
    │   └── test7
    │       └── AppTest.kt
    └── resources

Java

Project Setup

mkdir -p test/src/main/java/hello
cd test
touch src/main/java/hello/HelloWorld.java
touch src/main/java/hello/Greeter.java

Source Files

HelloWorld.java

package hello;

import org.joda.time.LocalTime;

public class HelloWorld {
  public static void main(String[] args) {
    LocalTime currentTime = new LocalTime();
    System.out.println("The current local time is: " + currentTime);

    Greeter greeter = new Greeter();
    System.out.println(greeter.sayHello());
  }
}

Greeter.java

package hello;

public class Greeter {
  public String sayHello() {
  return "Hello world!";
  }
}

Build File

Create a build.gradle in the project root

apply plugin: 'java'

// Repositories for 3rd parties (nuget repos)
repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

// Dependicies (nuget)
dependencies {
    compile "joda-time:joda-time:2.2"
    testCompile "junit:junit:4.12"
}

// Name of jar to create
jar {
    baseName = 'gs-gradle'
    version =  '0.1.0'
}

Create Gradle Wrapper

This allows others to build it too maybe on winding

gradle wrapper --gradle-version=5.1.1

Update Gradle Wrapper

gradle wrapper --gradle-version 6.7.1

Run build

./gradlew build

Migration of Old Projects

Use Java 8

Sometimes when using older projects with gradle you might see

Couldn't determine java version from '11.0.1'

This is because of an old version of gradle. The easiest way to solve it is to go to the command line and change your JAVA_HOME is 8 e.g.

export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64

Gradle 3.3 to 4.10

To achieve this I change the build.gradle to update the plugin and specify the maven and google repositories.

buildscript {
    repositories {
        google()
        jcenter()
        maven { url "https://plugins.gradle.org/m2/" }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

And ran gradlew not gradle and also updated the gradle.warapper.properties

distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip

From there I could use the standard update to 4.10

./gradlew wrapper --gradle-version 4.10

Gradle 4.10 to 5.4.1

For this I needed to change the plugin in build.gradle to be

   classpath 'com.android.tools.build:gradle:3.5.1'

And of course the gradle.warapper.properties

distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip

I also need to change the SDK build toools in the app to be 26

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.2"
    defaultConfig {
        applicationId "com.example.alexr.ideamanager"
        minSdkVersion 21
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

Next I need to change the following from the 25 version

    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:support-v4:25.3.1'
    compile 'com.android.support:recyclerview-v7:25.3.1'
    compile 'com.android.support:design:25.3.1'

To

    compile 'com.android.support:appcompat-v7:26.0.1'
    compile 'com.android.support:support-v4:26.0.1'
    compile 'com.android.support:recyclerview-v7:26.0.1'
    compile 'com.android.support:design:26.0.1'

Gradle 5.4.1 to 6.x

To do this I started Android Studio where I was prompted to upgrade to 6.x