Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to package an Android Studio project into jar

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to package an Android Studio project into a jar". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to package an Android Studio project into jar".

How to pack JAR package for Android Studio

In eclipse we know how to export one project as a jar package for use by other projects. In AS, you can only deal with it by modifying gradle.

Let's create a new project MakeJar and create a new modle- with the type android-library in the project.

As shown in the figure: app is our main project, and librarydemo is the model that we want to generate the jar package. Add the following code to the build.gradle in library demo at the same level as the android structure:

Project structure

Step 1-dependence

The dependent model of librarydemo should be added to the build of the main project App:

Dependencies {compile fileTree (dir: 'libs', include: [' * .jar']) compile 'com.android.support:appcompat-v7:22.2.0' compile project (': librarydemo')}

Step 2-load library and write task

When you finish compiling the project, all the java files of library have already generated the classes.jar package containing class, as shown in the following figure:

For the convenience of giving a task, export the changed classes.jar to library's build/libs/, and change the name to mysdk.jar (random name).

/ / Copy type task makeJar (type: Copy) {/ / Delete the existing delete 'build/libs/mysdk.jar' / / copy of the file from (' build/intermediates/bundles/release/') / / the file directory after entering the jar package into ('build/libs/') / / put the classes.jar into the build/libs/ directory / / include Exclude parameter to set filtering / / (we only care about the file classes.jar) include ('classes.jar') / / rename rename (' classes.jar', 'mysdk.jar')} makeJar.dependsOn (build) / / generate JAR package / / gradlew makeJar on the terminal

In the Terminal provided by android studio (the directory defaults to Wei's current project), type. / gradlew makeJar and enter to OK as shown below:

The generated jar package is under libs under the build of your library, and then it is ready to use.

Disclaimer: the typed jar only contains .class files of the source code and does not contain resource files.

See so many people step on, so many people encounter problems, explain here and solve

Great.

common problem

Someone asked:

How to put resources (pictures, layouts, string, etc.) into the jar package?

The answer is: since it is not included, let's put the resources used in the jar package into the project where you use the jar, and then use reflection.

Here is the reflection class:

Public class MResource {public static int getIdByName (Context context, String className, String resName) {String packageName = context.getPackageName (); int id = 0; try {Class r = Class.forName (packageName + ".R"); Class [] classes = r.getClasses (); Class desireClass = null; for (Class cls: classes) {if (cls.getName (). Split ("\ $") [1] .equals (className)) {desireClass = cls; break }} if (desireClass! = null) {id = desireClass.getField (resName) .getInt (desireClass);}} catch (Exception e) {e.printStackTrace ();} return id }} Thank you for your reading. The above is the content of "how to package Android Studio projects into jar". After the study of this article, I believe you have a deeper understanding of how to package Android Studio projects into jar, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report