Android Gradle 构建系统:创建 Jar 而不是库

编程入门 行业动态 更新时间:2024-10-12 08:19:05
本文介绍了Android Gradle 构建系统:创建 Jar 而不是库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

目前我在 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-15 05:22:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/857542.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而不是   系统   Android   Gradle   Jar

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!