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 generate a custom jar package for Android Studio

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to generate custom jar packages from Android Studio". 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 to generate custom jar packages from Android Studio".

If you want to export a project as a jar package for use by other projects, you can export the project as a jar package directly in eclipse, while in AS you can modify the gradle before processing.

Next, let's introduce the specific steps:

1. Create a new project with an arbitrary project name, eg:MakeJarApplication, and a new module type of android-library in the project, named testLibrary. As shown in the figure:

Project structure diagram

2. Make app rely on this library and add compile project (': testlibrary') to the build.gradle file under app.

Dependencies {compile fileTree (dir: 'libs', include: [' * .jar']) androidTestCompile ('com.android.support.test.espresso:espresso-core:2.2.2', {exclude group:' com.android.support' Module: 'support-annotations'}) compile' com.android.support:appcompat-v7:25.3.1' compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4' testCompile' junit:junit:4.12' compile project (': testlibrary')}

3. Create a TestShow class in testlibrary so that external app can be called. And call in app

(1) TestShow. Java

Public class TestShow {public void show (Context context, String msg, TextView textView) {textView.setText (msg); Toast.makeText (context, msg, Toast.LENGTH_SHORT). Show ();} public void test () {System.out.println ("Test Information, test () method");}}

(2), MainActivity.java

@ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); show_tv = ((TextView) findViewById (R.id.show_tv)); ((Button) findViewById (R.id.btn)) .setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {new TestShow () .show (MainActivity.this, "call show method in jar package", show_tv) });}

4. The configuration in build.gradle of testlibrary can generate jar files:

Def SDK_BASENAME = "TestSdk"; def SDK_VERSION = "_ V1.0"; def sdkDestinationPath = "build" Def zipFile = file ('build/intermediates/bundles/default/classes.jar') task deleteBuild (type: Delete) {delete sdkDestinationPath + SDK_BASENAME + SDK_VERSION + ".jar"} task makeJar (type: Jar) {from zipTree (zipFile) from fileTree (dir:' src/main',includes: ['assets/**']) / / enter the assets directory into the jar package baseName = SDK_BASENAME + SDK_VERSION destinationDir = file (sdkDestinationPath)} makeJar.dependsOn (deleteBuild, build)

Where SDK_BASENAME = "TestSdk"; SDK_VERSION = "_ V1.0"; is the name that defines the generated jar as TestSdk_V1.0.jar.

5. There is a "other" in the Gradle directory on the right. Expand to find "makejar", and then double-click "makejar" to generate the jar file. The result is as follows:

Step result map

You can now copy TestSdk.jar to the libs directory in a new module, and then you can call the new TestShow () .show (context, ", show_tv); method.

6. Call the result in the project:

At this point, I believe you have a deeper understanding of "how Android Studio generates custom 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