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 Module into Jar in Android studio

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, I would like to share with you how to package Module into Jar in Android studio. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

1. Automatically generated jar package by default

It is well known that android studio will automatically generate the jar package after the app run or build that library depends on, and the path is Module root / build/intermediates/bundles/debug or release/classes.jar, so the generated jar can be used, but it is not under our control, so we need to solve this problem in other ways.

Note:

If you find that there is no release directory under the bundle folder of the Android Studio project, you may not have added module to the main module dependency.

Mode 1: main module right-click-- > penultimate Open Module Setting-- > last option above Dependencies-- > right green plus sign-- > Module dependency. And then determine

Method 2: add compile project (': lib-zxing') to the dependencies of the gradle of the main Module (where the third-party library is usually added)

2. Custom generated jar package

First, you need to modify the build.gradld file in the module project to be packaged, as shown below:

Second, add the following code to the build.gradle file:

Method 1:

Def _ BASENAME = "TestJar"; def _ VERSION = "_ V1.0"; def _ DestinationPath = "build"; / / location where the jar package was generated def zipFile = file ('build/intermediates/bundles/default/classes.jar') / / location of files to be packaged: task deleteBuild (type:Delete) {delete _ DestinationPath + _ BASENAME + _ VERSION + ".jar"} task makeJar (type:Jar) {from zipTree (zipFile) from fileTree (dir:'src/main',includes: ['assets/**']) / / insert the assets directory into the jar package baseName = _ BASENAME + _ VERSION destinationDir = file (_ DestinationPath)} makeJar.dependsOn (deleteBuild, build)

Method 2: the packaged jar only contains .class files of the source code and does not contain resource files.

Task makeJar (type: Copy) {delete 'build/TestJar_V1.0.jar' / / remove the old jar package from (' build/intermediates/bundles/default/') / / remove the default jar package into ('build/') / / output the jar package to the specified directory include (' classes.jar') rename ('classes.jar',' TestJar_V1.0.jar') / / the name of the custom jar package} makeJar.dependsOn (build)

Method 3:

Task clearJar (type: Delete) {delete 'build/TestJar_V1.0.jar' / / name of the jar package Casually name} task makeJar (type:org.gradle.api.tasks.bundling.Jar) {/ / specify the generated jar name baseName 'TestJar_V1.0' / / where to package the class file from (' build/intermediates/bundles/default/') / / the directory structure after packaging to jar into ('build/') / / remove directories and files that do not need to be packaged exclude (' test/', 'BuildConfig.class' 'R.class') / / remove the file starting with R exclude {it.name.startsWith (' R') }} makeJar.dependsOn (clearJar, build)

Note:

The above configuration information should be in contact with android {. .} configured scripts are level relationships.

Where _ BASENAME = "TestJar"; _ VERSION = "_ V1.0"; is the name that defines the generated jar package as TestJar_V1.0.jar.

Once configured, you can compile the jar package in two ways:

Method A: click the Gradle panel on the right side of the Android Studio (usually in this location), find the Tasks-> other-> makeJar command in the project or the library directory, double-click the makeJar and then compile the jar package.

After successful packaging: generate the jar package under the specified directory of the configuration

Method B: through the cmd command line, in the project root directory, execute the gradlew makeJar command, see the compiled information after the OK, can also generate the same jar package, and directly use the way in ① is equivalent.

Note: configure the "environment variable" of gradle before using this method, otherwise you will not find this command directly in the project root directory. Using cmd is laborious and is not recommended.

Method C: enter the gradlew makeJar command in the Android Studio terminal window.

The package is successfully packaged as shown below:

After successful packaging: generate the jar package under the specified directory of the configuration

Note:

Question 1:

For the default jar package section of the above configuration form, the in-use directory is from ('build/intermediates/bundles/release/'). Pay attention to the bold part, which I wrote according to this directory at first, no problem. But later, that is, after Android Studio updated to 2.3, I found that this directory is gone, replaced by default and debug directory, debug directory before, there is no change, but the original release directory has been replaced by the current default directory. As a result, when I generated the jar package, I never found the jar package and reported the error shown in the figure above.

The reason is that there is no release directory, can not find the default classes.jar file, so there is no custom name of the jar package, changed to default or debug directory will be able to generate jar package normally, you should pay attention to this change.

Question 2

Execution failed for task': mylibrary:lint'.

> Lint found errors in the project; aborting build.

To solve the problem in the build.gradle file, add the following code block (note where the code block is located) and re-execute the gradlew makeJar command.

Android {lintOptions {abortOnError false}...}

Finally, note:

The embedded application is packaged into a jar package, but this approach has limitations and cannot package the resource files used by the embedded application. Here is how to package aar.

Third, use the aar package directly

It is also a new feature of Android Studio to package embedded applications into aar packages, which can package the resource files used by applications together. Compilation can be packaged to generate aar packages without worrying about missing resources.

If Module is an application in Project, the attribute is defined as apply plugin: 'com.android.application'; in the build.gradle file of Module, and if Mudule is a referenced dependent lib library, the attribute is defined as: apply plugin:' com.android.library; in the build.gradle file of Module, so when we package the application Module out of the aar file, we need to modify the type property. Normally, Module is recompiled directly, and the resulting aar package is located in the / build/outputs/aar/, of the Module root directory as shown below:

Here's how to add an aar package to other Android projects:

First, copy the aar package to the libs directory

Second, configure the build.gradle file:

Add A to android as follows:

Repositories {flatDir {dirs' libs'}}

B add compile (name:lib-zxing-release', ext:'aar') to dependencies

The complete build.gradle file is as follows:

Apply plugin: 'com.android.application' android {... Repositories {flatDir {dirs' libs'}} dependencies {... Compile (name:'lib-zxing-release', ext:'aar')} above is all the content of the article "how to package Module into Jar in Android studio". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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