Android Packaging: Android Studio Packaging Code into jar Package Tutorial

  • 2021-11-24 02:54:55
  • OfStack

1. Create a new as project and a new model module

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

2. After compiling, open the build-intermediates-bundles directory under model. There are two folders in the directory, debug and default, and there is one classess. jar under default folder, which is the compiled jar package.

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

3. Ensure that the classess. jar file exists, 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 existing 
  delete 'build/libs/mysdk.jar'
  // Set the copied file 
  from('build/intermediates/bundles/default/')
  // Score jar File directory behind package 
  into('build/libs/')
  // Will classes.jar Put in build/libs/ Directory 
  //include ,exclude Parameter to set the filter 
  // (All we care about is classes.jar This file) 
  include('classes.jar')
  // Rename 
  rename ('classes.jar', 'mysdk.jar')
 }
 makeJar.dependsOn(build)

Note here: Your classess. jar file directory is in parentheses in the above code, and my own directory is posted here. Different as versions may lead to different directories, so friends can directly change to your own classess. jar path when using it.

4. After compiling, go to the Terminal command line of as, enter the following command and enter:

gradlew makeJar

When the following text appears, it means that the compilation was successful.

BUILD SUCCESSFUL

The exported jar package is in the build-libs folder under the model module, which is copied for our later use.

In this way, we will make our own code module into jar package, and import jar package directly when using it.

Supplementary knowledge: Android. mk compiles jar

Android. mk compiles jar

Android. mk file configuration


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

#include $(BUILD_STATIC_JAVA_LIBRARY)

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

Difference between BUILD_JAVA_LIBRARY and BUILD_STATIC_JAVA_LIBRARY

BUILD_JAVA_LIBRARY compiles jar package, inside is DEX format file, if the user wants to use this jar package to Eclipse to do Android E113EN development, Eclipse is not aware of this format file, usually will report error: Conversion to Dalvik format failed with error 1

BUILD_STATIC_JAVA_LIBRARY compiled out of the jar package, each java file corresponding to the class file exists separately, as the name implies, each java file used in the variables are statically compiled to class, this format of jar package can be imported in Eclipse and normal use, but there may be a certain compatibility hidden trouble


Related articles: