Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Gradle Build System: Create Jar Not Library

Tags:

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
}
like image 721
Sababado Avatar asked Jun 20 '13 15:06

Sababado


People also ask

How do I create an executable jar in gradle?

Method 1 (no need for application or other plugins)Copy library JARs in libs sub-directory of where you put your result JAR. Make sure your library JAR files do not contain space in their file name. Note that Java requires us to use relative URLs for the Class-Path attribute.

How do I manually sync gradle?

Open your gradle. properties file in Android Studio. Restart Android Studio for your changes to take effect. Click Sync Project with Gradle Files to sync your project.

How do I change the gradle folder in Android Studio?

To change its path go to this path File > Settings... > Build, Execution, Deployment > Gradle In the Global Gradle settings Change Service directory path to what you want.


1 Answers

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
}
like image 178
vinc3m1 Avatar answered Oct 19 '22 08:10

vinc3m1