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 does Android Studio package the code into jar packages

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 Android Studio packages the code into jar packages". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how Android Studio packages the code into jar packages".

First, create a new as project and then a new model module

Then add the dependency of model to the build.gradle in app. Then compile the project.

After the compilation is completed, open the build--intermediates--bundles directory under model. There are two folders under the directory, debug,default, and there is a classess.jar under the default folder, which is the compiled jar package.

The main thing we need here is: because the version of as we use is different, the directory page of the classess.jar package will be different, but the final directory is still in the build--intermediates--bundles directory, and friends can look for the classess.jar file in this directory to make sure that the file can be found before performing the third step.

Third, under the condition of ensuring the existence of the classess.jar file, open the build.gradle file of the model module, add the following code to the file and the android module, and compile:

/ / Copy type task makeJar (type: Copy) {/ / Delete the existing delete 'build/libs/mysdk.jar' / / copy of the file from (' build/intermediates/bundles/default/') / / 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 the filter / / (we only care about the file classes.jar) include ('classes.jar') / / rename rename (' classes.jar', 'mysdk.jar')} makeJar.dependsOn (build)

It should be noted here: the above code in from brackets is your classess.jar file directory, here is posted my own directory, different as versions may lead to different directories, so partners in the use of directly to your own classess.jar path.

4. After the compilation is completed, find the Terminal command line of as, enter the following command and enter:

Gradlew makeJar

When the following text appears, it indicates that the compilation is successful.

BUILD SUCCESSFUL

The exported jar package is in the build--libs folder under the model module. It is copied out for us to use later.

In this way, we can package our own code modules into jar packages and import them directly into jar packages when we use them.

Supplementary knowledge: Android.mk compiles jar

Android.mk compiles jar

Android.mk file configuration

LOCAL_PATH:= $(call my-dir) # make jarinclude $(CLEAR_VARS) LOCAL_SRC_FILES: = $(call all-subdir-java-files) LOCAL_MODULE: = mytestjarinclude $(BUILD_JAVA_LIBRARY) # include $(BUILD_STATIC_JAVA_LIBRARY)

Execute the mm command in the current project (sourse build/envsetup.sh before that)

The difference between BUILD_JAVA_LIBRARY and BUILD_STATIC_JAVA_LIBRARY

The jar package compiled by BUILD_JAVA_LIBRARY contains files in DEX format. If you want to use this jar package to Eclipse for Android APP development, Eclipse does not recognize the files in this format and will usually report an error: Conversion to Dalvik format failed with error 1

The jar package compiled by BUILD_STATIC_JAVA_LIBRARY, in which the class file corresponding to each java file exists separately. As the name implies, the variables used in each java file are statically compiled into the class. The jar package in this format can be imported into Eclipse and used normally, but there may be some compatibility risks.

At this point, I believe you have a deeper understanding of "how Android Studio packages the code into jar packages". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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