Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up compile library in android studio. LOMBOK

Help me to set up comile dependencies in Android Studio in build.gradle. I mean that they not include into final APK.

this build.gradle works fine but i don't need

lombok

library in apk in runtime;

apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.0"

    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 16
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    } }

dependencies {
    compile 'com.android.support:appcompat-v7:+'
    compile 'org.projectlombok:lombok:1.12.2' }

And may be its possible to setup in global build.gradle file for all projects?

like image 461
Oleksandr Samsonov Avatar asked Jan 25 '14 00:01

Oleksandr Samsonov


People also ask

Can I use Lombok in Android Studio?

In addition to setting up your gradle project correctly, you need to add the Lombok IntelliJ plugin to add lombok support to Android Studio: Go to File > Settings > Plugins. Click on Browse repositories... Search for Lombok Plugin.


4 Answers

With Android Studio 1.1.0, I couldn't find a consolidated set of instructions for adding Lombok that worked for me. That includes Lombok's own setup page: http://projectlombok.org/setup/android.html

Here's everything I had to do to get it working (on OS X):

  • Install Lombok plugin in Android Studio
    • Android Studio > Preferences > Plugins
    • Click on Browse repositories...
    • Search for Lombok Plugin
    • Click on Install plugin
    • Restart Android Studio
  • Add to the android/dependencies block in app/build.gradle: provided 'org.projectlombok:lombok:1.16.2'
    • See search.maven.org for latest version, as lombok's instructions don't update as they release new versions
    • I assume the subprojects approach in build.gradle in the project root (from Олександр Самсонов's answer) works also, but it appears that requires moving the configuration from app/build.gradle (you aren't allowed to further extend the configuration in the subprojects). I didn't want to move the entire android configuration, so I kept it in the app/build.gradle instead.
  • Create a lombok.config file in the root of the project containing two lines: lombok.anyConstructor.suppressConstructorProperties = true lombok.addGeneratedAnnotation = false
like image 80
Stan Kurdziel Avatar answered Oct 03 '22 17:10

Stan Kurdziel


The ability to do this was just added to the v0.8 Android-Gradle plugin, which you can use if you're running Android Studio 0.4.3 (which is available on the canary update channel).

It's mentioned in the release notes and a thread on the adt-dev mailing list

You specify it like this:

dependencies {
    provided 'org.projectlombok:lombok:1.12.2'
}

I've filed bug https://code.google.com/p/android/issues/detail?id=65216 to request the ability to edit this through the Dependencies panel in the Project Structure dialog; for the moment you have to edit your build.gradle file by hand to use this.

As for whether you can put it in the top-level build.gradle file to have it apply to all modules, I'm not certain. I tried putting it in the allprojects block, but Gradle told me that the default dependency handler didn't know what to do with it. So I'm guessing no, but if I get other information, I'll update this answer.

like image 39
Scott Barta Avatar answered Oct 03 '22 18:10

Scott Barta


For Android Studio

  1. Go to File > Settings > Plugins
  2. Click on "Browse repositories..."
  3. Search for "Lombok Plugin"
  4. Click on "Install plugin"
  5. Restart Android Studio
like image 38
kaMChy Avatar answered Oct 03 '22 16:10

kaMChy


I resolve this issue by changing gradle-wrapper.properties file in gradle folder. Now it looks like this:

#Sat Jan 25 02:59:06 EET 2014
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=http\://services.gradle.org/distributions/gradle-1.10-all.zip

and in main build.gradle file i can wrote this for lombok

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.8.+'
    }
}

allprojects {
    repositories {
        mavenCentral()
    }
}

subprojects {
    apply plugin: 'android'

    buildscript {
        repositories {
            mavenCentral()
        }
    }

    android {
        compileSdkVersion 19
        buildToolsVersion "19.0.0"

        defaultConfig {
            minSdkVersion 10
            targetSdkVersion 16
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                runProguard false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            }
        }
    }

    dependencies {
        compile 'com.android.support:appcompat-v7:+'
        provided 'org.projectlombok:lombok:1.12.2'
    }
}

Now in sub projects i don't need write many params for plugins or repositories and provided method works pretty fine.

like image 31
Oleksandr Samsonov Avatar answered Oct 03 '22 17:10

Oleksandr Samsonov