20

Currently I am working in eclipse. I want to migrate to Android Studio however I need to figure this out first: How do I create a jar for my project using the new android build system?

My Project is setup as a library however there are only java files in the project. I don't need or want to export this as a library. I want to export the files as a .jar so it can easily be dropped into another project.

Update Here is my gradle file. I cannot add the line apply plugin java because it is incompatible with the android plugin. The jar task is already included in the android plugin.

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android'

dependencies {
    compile files('libs/android-support-v4.jar')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }
}

sourceSets {
    main  {
        java {
            srcDir 'src/main/java'
        }
    }
}
task jar(type: Jar) {
    from sourceSets.main.java
}

I'm running the script as: gradle clean jar

When I run the tasks, nothing happens... Why? What am I missing?

Update 2

Below is the new gradle build file that I'm using. Notice the gradle version change due to android studio's latest update. Even with a simple clean build I get this error: Project directory '<my_workspace_path>\Core2Project\build.gradle' is not a directory. This error only happens in the build studio. Not when I run from the IDE. I've run into this same issue with another project as well. Turns out i'll get this error when I specify the file name to use in the build studio.

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

dependencies {
    compile files('libs/android-support-v4.jar')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }

    sourceSets {
        main  {
            java {
                srcDir 'src/main/java'
            }
        }
    }
}


task jar(type: Jar) {
    from android.sourceSets.main.java
}
Sababado
  • 2,524
  • 5
  • 33
  • 51

2 Answers2

17

With newer versions of gradle and the android gradle plugin, you just need to add the following to your build.gradle:

task jar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
}

and run gradle clean compileReleaseJava jar

On older versions of the gradle plugin, your sourceSets element needs be inside android:

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }

    sourceSets {
        main  {
            java {
                srcDir 'src/main/java'
            }
        }
    }
}

Then, your jar task would need to reference android.sourceSets:

task jar(type: Jar) {
    from android.sourceSets.main.java
}
vinc3m1
  • 4,075
  • 1
  • 26
  • 23
  • I want to say you're right, but I can't build my project anymore. Just a simple "clean build" gives me an error. The `build.gradle` file in the error path is the one i'm pointing to in order to run the tasks. No changes here. `Project directory '\Core2Project\build.gradle' is not a directory.` – Sababado Jul 18 '13 at 22:05
  • are you on the latest gradle plugin version? you should be at `classpath 'com.android.tools.build:gradle:0.5.+'` now – vinc3m1 Jul 18 '13 at 22:08
  • Yes, I actually created a new project just now and copied my src directory, and the `android` and `jar` bits from the gradle. My build script is inside of the module's gradle file, not the project's gradle file – Sababado Jul 18 '13 at 22:19
  • why are you pointing to that error path to run the tasks? what are you trying to run? – vinc3m1 Jul 18 '13 at 23:58
  • I guess I left it that way because that's how it is set by default. The build targets the Project level gradle file (which has a settings file next to it that pulls in each module's gradle file. This case there is only one module) – Sababado Jul 19 '13 at 14:01
  • Even when I change it to point to the module's build file I see the same error (with the relevant path) – Sababado Jul 19 '13 at 14:02
  • 1
    With the latest Gradle plugin, I had to change the Jar task body from part to this: `from fileTree(dir: 'build/intermediates/classes/release')` I also run `gradle clean compileReleaseJava jar` – Lo-Tan Jun 23 '14 at 20:02
  • 1
    More recently: from android.sourceSets.main.java.srcDirs – dwbrito Apr 07 '15 at 01:33
  • Do we add the task to the project-level or the module-level `build.gradle`? – Kenny Worden Jul 24 '17 at 16:03
-2

the 'jar' task you mean here is introduced by the java plugin. you can apply the gradle java plugin by using

apply plugin:'java'

Have a look in the chapter about the java plugin in the gradle userguide at http://www.gradle.org/docs/current/userguide/tutorial_java_projects.html#N103C6 to get more information about the java plugin.

cheers,

René

Rene Groeschke
  • 27,999
  • 10
  • 69
  • 78
  • that's surprising as the java library definitely adds a jar task. can you provide the full rootproject build file. What do you mean with "using" the jar task? injecting it from the command line? – Rene Groeschke Jun 22 '13 at 05:34
  • Sorry for the delay, I've updated my question with the build script – Sababado Jun 28 '13 at 18:58
  • what do you mean by "nothing happens" when running "gradle clean jar" is the task executed and marked as up-to-date? Is no jar file created in build/libs? One thing I notice in your build script is the configuration of your main java source directory. This is the default I think so configuring this isn't necessary. Furthermore, your snippet just adds another directory to the default ones. – Rene Groeschke Jun 30 '13 at 19:44
  • 6
    The java plugin doesn't compile correctly against imported android classes. – vinc3m1 Jul 18 '13 at 20:12