Android的摇篮构建系统:创建罐不图书馆 [英] Android Gradle Build System: Create Jar Not Library
问题描述
目前我在Eclipse我的工作。我想,但是我需要先算出这个迁移到Android的工作室:我如何创建一个使用新的Android构建系统为我的项目一个jar
我的项目是设置为一个库但有在项目中唯一的java文件。我不需要或不想要这个导出为库。我要导出的文件作为一个.jar因此它很容易被丢弃到另一个项目。
更新
这是我的文件的gradle。的我无法添加行应用插件的java
,因为它与Android插件不兼容。 jar任务已经包含在Android插件。的
buildscript {
库{
mavenCentral()
}
依赖{
类路径'com.android.tools.build:gradle:0.4
}
}
应用插件:'机器人'依赖{
编译文件(库/ Android的支持 - v4.jar')
}安卓{
compileSdkVersion 17
buildToolsVersion17.0.0 defaultConfig {
7的minSdkVersion
targetSdkVersion 16
}
}sourceSets {
主要{
java的{
SRCDIR的src / main / java的'
}
}
}
任务jar包(类型:罐){
从sourceSets.main.java
}
我运行脚本:干净的gradle JAR
当我运行的任务,没有任何反应,为什么?我缺少什么?
更新2
下面是我使用的gradle新构建文件。注意版本的Gradle变化,由于到Android工作室的最新更新。即使是一个简单的干净构建
我得到这个错误:项目目录'< my_workspace_path> \\ Core2Project \\的build.gradle'是不是一个目录
该错误仅发生在构建工作室。不是当我从IDE中运行。我碰到的另一个项目同样的问题为好。原来,当我指定要在构建工作室使用的文件名,我会得到这个错误。
buildscript {
库{
mavenCentral()
}
依赖{
类路径'com.android.tools.build:gradle:0.5.+
}
}
应用插件:'机器人'依赖{
编译文件(库/ Android的支持 - v4.jar')
}安卓{
compileSdkVersion 17
buildToolsVersion17.0.0 defaultConfig {
7的minSdkVersion
targetSdkVersion 16
} sourceSets {
主要{
java的{
SRCDIR的src / main / java的'
}
}
}
}
任务jar包(类型:罐){
从android.sourceSets.main.java
}
使用gradle这个较新版本和android插件的gradle,你只需要添加以下到你的的build.gradle
:
任务jar包(类型:罐){
从android.sourceSets.main.java.srcDirs
}
和运行干净的gradle罐子compileReleaseJava
在旧版本的插件的gradle,你的 sourceSets
元素的需求在里面机器人
:
安卓{
compileSdkVersion 17
buildToolsVersion17.0.0 defaultConfig {
7的minSdkVersion
targetSdkVersion 16
} sourceSets {
主要{
java的{
SRCDIR的src / main / java的'
}
}
}
}
然后,你的 JAR
任务将需要引用 android.sourceSets
:
任务jar包(类型:罐){
从android.sourceSets.main.java
}
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
}
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
}
这篇关于Android的摇篮构建系统:创建罐不图书馆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!