Android Gradle 构建系统:创建 Jar 而不是库 [英] Android Gradle Build System: Create Jar Not Library

查看:22
本文介绍了Android Gradle 构建系统:创建 Jar 而不是库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我在 eclipse 中工作.我想迁移到 Android Studio,但我需要先弄清楚这一点:如何使用新的 android 构建系统为我的项目创建 jar?

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?

我的项目被设置为一个库,但项目中只有 java 文件.我不需要或不想将其导出为库.我想将文件导出为 .jar,以便可以轻松地将其放入另一个项目中.

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.

更新这是我的 gradle 文件.我无法添加行 apply plugin java 因为它与 android 插件不兼容.jar 任务已经包含在 android 插件中.

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
}

我正在运行脚本:gradle clean jar

当我运行任务时,没有任何反应...为什么?我错过了什么?

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

更新 2

下面是我正在使用的新 gradle 构建文件.请注意由于 android studio 的最新更新,gradle 版本发生了变化.即使使用简单的 clean build 我也会收到此错误:Project directory '<my_workspace_path>\Core2Project\build.gradle' is not a directory. 仅此错误发生在构建工作室.当我从 IDE 运行时不是.我在另一个项目中也遇到了同样的问题.事实证明,当我指定要在构建工作室中使用的文件名时,我会收到此错误.

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
}

推荐答案

使用较新版本的 gradle 和 android gradle 插件,您只需要在 build.gradle 中添加以下内容:

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
}

并运行gradle clean compileReleaseJava jar

在旧版本的 gradle 插件上,你的 sourceSets 元素需要在 android 中:

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'
            }
        }
    }
}

然后,你的 jar 任务需要引用 android.sourceSets:

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

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

这篇关于Android Gradle 构建系统:创建 Jar 而不是库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆