简体   繁体   English

如何在 Android Studio 中从库项目生成 Jar

[英]How to Generate Jar from Library Project in Android Studio

I had library project in Eclipse and the library project also some libraries dependencies inside it.我在 Eclipse 中有库项目,库项目中还有一些库依赖项。

Now I migrate Eclipse to Android studio and I would like to build jar from library project.现在我将 Eclipse 迁移到 Android Studio,我想从库项目构建 jar。

I successfully generated jar in Android studio but the problem is that inside jar there is not included reference library.我在 Android Studio 中成功生成了 jar,但问题是在 jar 中没有包含参考库。

Can you please help How to do reference Some other library inside main library so it will include in Android Studio Generated jar.你能帮忙如何参考主库中的一些其他库,以便它将包含在 Android Studio 生成的 jar 中。

I reviewed all link but it is only related to single library.我查看了所有链接,但它仅与单个库相关。 But my question is different.但我的问题是不同的。

I have tried following code but not getting classes of dependent library in my jar我试过下面的代码,但没有在我的 jar 中获取依赖库的类

task jarTask(type: Jar) {
    baseName="my-sdk-android"
    from 'src/main/java'
}

task createJarWithDependencies(type: Jar) {
    baseName = "my-sdk-android-jar-with-dependencies"

    from {
        configurations.compile.collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }

    with jarTask
}

configurations {
    jarConfiguration
}

artifacts {
    jarConfiguration jarTask
}

Such an idea reffers to the gradle build system.这样的想法是指 gradle 构建系统。 You should not include an dependencie library in your actual library project.您不应在实际库项目中包含依赖库。 You should ship it seperately.您应该单独运送。

As this should be a library module, the end-project has to have a lits of its dependencies.由于这应该是一个库模块,因此最终项目必须有一些依赖项。

Anyway...反正...

Add to build.gradle of library project:添加到库项目的 build.gradle 中:

// Exec Jar
task bundleJar(type: Jar) {
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    with jar
}

So it adds needed classes to the jar file.因此它将所需的类添加到 jar 文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM