简体   繁体   English

将android库导出为.jar文件

[英]export android library as a .jar file

I've just created an android library module, and now I'd like to export it as a .jar file (Like I used to do in Eclipse), so I can use it in others project just link it as an external library file. 我刚刚创建了一个android库模块,现在我想将其导出为.jar文件(就像我以前在Eclipse中所做的那样),因此我可以在其他项目中使用它,只需将其链接为外部库文件即可。

The problem is that I've just read from the official site that "You cannot export a library module to a JAR file": https://developer.android.com/tools/projects/index.html 问题是我刚刚从官方网站上读到“您不能将库模块导出到JAR文件”: https : //developer.android.com/tools/projects/index.html

So how can I do that? 那我该怎么办呢? Is there something similare that I can do to obtain the same result? 是否可以做类似的事情以获得相同的结果?

Thanks 谢谢

You must export it as .AAR file. 您必须将其导出为.AAR文件。 It is not possible to export an Android Studio library as a .jar file. 无法将Android Studio库导出为.jar文件。

Android .AAR Format Android .AAR格式

Check this out: https://androidbycode.wordpress.com/2015/02/23/building-an-aar-library-in-android-studio/ 看看这个: https : //androidbycode.wordpress.com/2015/02/23/building-an-aar-library-in-android-studio/

Purpose of AAR: Library reuse has long been available in Java based applications through Java Archive (.jar) files. AAR的目的:通过Java Archive(.jar)文件,基于Java的应用程序中的库重用早已存在。 Android AAR files build upon this concept to allow you to package not only the source code but also the libraries self contained Android resources. Android AAR文件基于此概念构建,可让您不仅打包源代码,而且打包自包含的Android资源。 The library can have its own manifest and resources and can also optionally provide assets, NDK built libraries, proguard configuration and even its own custom lint filters. 该库可以具有其自己的清单和资源,还可以选择提供资产,NDK构建的库,proguard配置,甚至其自己的自定义棉绒过滤器。 This is all bundled into a zip archive that can be published to a repository for your team (or the world!) to get easy access from a Gradle build. 所有这些都捆绑在一个zip归档文件中,该归档文件可以发布到您的团队(或全世界!)的存储库中,以便从Gradle构建中轻松访问。

This is how to create the .AAR file by @alex18aa: 这是通过@ alex18aa创建.AAR文件的方法:

File -> New module -> Import aar/jar , and then add the dependencie in the module settings, and linking it. File > New module Import aar/jar > Import aar/jar ,然后在模块设置中添加依赖关系,并将其链接。

Look into AAR files. 查看AAR文件。 They are Android libraries that can contain not only code, but also resources! 它们是Android库,不仅可以包含代码,还可以包含资源!

AAR: Look up AAR files etc. The way I do it is: AAR:查找AAR文件等。我的方法是:

uploadArchives {
 repositories.mavenDeployer {
    repository(url: "someDirectory/")
}

} }

Then ./gradlew clean build uploadArchives 然后./gradlew clean build uploadArchives


JAR: If you don't have a resource file, just add this to your build.gradle: JAR:如果您没有资源文件,只需将其添加到build.gradle中:

task makeJarRelease(type: Copy) {
    from("build/intermediates/bundles/release/")
    into("some_directory/")
    include("classes.jar")
    rename("classes.jar", "yourlibname.jar")
}

Then run a ./gradlew clean build makeJarRelease 然后运行./gradlew clean build makeJarRelease

And that will do it. 这样就可以了。

Yeah, Android Studio exports .AAR files and that is the new standard. 是的,Android Studio导出.AAR文件,这是新标准。 However, if you need a jarfile, thats how you can do it. 但是,如果您需要一个jarfile,那就可以做到这一点。

Again, you cannot have a res folder (meaning no XML files or drawables) if you want to do it this way. 同样,你不能有res文件夹(意味着没有XML文件或绘图资源),如果你想要做这种方式。

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

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