Android supports third-party jar packages, and how Eclipse imports jar packages

Article source: https://my.oschina.net/zhoulc/blog/112573

Usually we use two formats of jar package files when we develop android.

1. In the eclipse environment , the introduction of a third-party jar package refers to the jar package containing the .class file exported through the eclipse tool.

2. In the source code environment, the jar package generated by configuring the Android.mk file is also the jar package file that we rely on for development under the source code, which is the jar package file containing class.dex. class.dex isThrough the file format that can be directly run on the Dalvik virtual machine in the Android system.

Conversion between two jar packages: 1) .class=>class.dex The role of the dx tool is to convert .class into a dex file

                           2) class.dex=>.class can use decompilation tools

Specifically how to export the jar package through Eclipse and generate the jar package under the source code will not be described in detail, there are many online information

Reasons for third-party jar package dependencies:

    In the normal process, if the project needs to support the reference of third-party jar packages, if it is developed through Eclipse, the jar package containing the .class file is directly loaded into the project, and the final compiled apk contains the entire jar package. file (class.dex), if it is compiled from source code, directly add the dependency on the jar package file under system/framework in Android.mk, and finally the jar package file is also compiled with the apk.
    Since this development project needs to be done, the app application and the jar package need to be separated, that is, the jar package file needs to be independent and cannot be compiled with the apk. Generally speaking, the size of the apk will be greatly reduced.

1. Create a new Java Project under Eclipse (as a jar package file)

Implement some interfaces and attributes in the Person class. If the Person class calls the android sdk method, please see the second step. If not, skip the second step and go to the third step. ( The author did not call the method in the android SDK in the jar package )


2. Add a reference to the Android jar package (that is, the SDK interface) in the TestToJar project

    Find the android.jar package in the example that comes with the SDK . Right-click on the project, select android.jar in Java Build Path -> Libraries-> Add Jars in properties .


3. Add the registration file <permission xml file name>.xml to the project

    This file is used to register the library with the system, and the name can be determined by yourself. In the following, it is assumed that the file is  mylibxml.xml .
Add the following to the file:

<?xml version="1.0" encoding="UTF-8"?>
<permissions>
<library    name="com.mytest.lib"
file="/system/framework/mylib.jar"/>
</permissions>

The  library  is used to  associate the two attributes of name  and  file  under it, and the value of name  will be used later. The  name  here is specified as the package name of the project.

The  file  here is specified as the storage path of the library file, which should be:
/system/framework/<jar file name>.jar

Fourth, add a makefile to the project

Create the following files in the root directory of the library file project:
Android.mk
This file is used to compile the project, and the name is fixed.
Add the following to the file:

LOCAL_PATH:= $(call my-dir)
#MAKE_JAR 编译jar包过程到 /system/framework下面 APK需要的jar包include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_MODULE := mylib
include $(BUILD_JAVA_LIBRARY)
#MAKE_XML 编译XML过程到 /system/etc/permissions/下面include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := mylibxml.xml
LOCAL_MODULE_CLASS := ETC
LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)/permissions
LOCAL_SRC_FILES := $(LOCAL_MODULE)
include $(BUILD_PREBUILT)

Among them, the LOCAL_MODULE  part of the  MAKE_JAR  part specifies the name of the jar package file to be generated, which
needs to be  consistent with the file  part under  the library  in the .xml  file  . Among them, the LOCAL_MODULE  section of the  MAKE_XML  section specifies the .xml  file used to register the library  .

5. Copy all the code of the java project into the compilation environment of the android source code

Usually copied into system/app/

Then through mm compilation, a .jar  file and a  .xml  file will be generated  in the android source code compilation environment .

The above two files are needed by the application at runtime.
Before debugging the application, you need to copy two files to the corresponding directory of the device:
.jar  file: /system/framework/
.xml  file: /system/etc/permissions/
Copy to the device and use the following command:
$ sudo adb push <. jar or .xml file path> <path in device>
After copying, the device needs to be restarted.

6. The preliminary preparations are basically ready, and start writing test cases. (Verify that the apk can find the jar package file in the device)

(1) Use the Eclipse tool to create a new Android test case

Note: Under normal circumstances, the compiler will give us an error. For the sake of comfort, I exported the above java project into a jar file and loaded it in through Eclipse, so the compiler did not prompt an error message.

(2) Edit the AndroidManifest.xml file

编辑工程根目录下的 AndroidManifest.xml 文件。
在 application 下添加以下内容:
<uses-library
android:name="com.mytest.lib">
</uses-library>
其中的 :name 表示所引用的库文件的包名。
其与库工程的 .xml 文件中的 name 应该是一致的。
这样在应用运行时就能够找到相应的 .jar 文件了。
如果引用了多个库,需要添加多个 uses-library 标签。

 
(3)   为工程添加   makefile   文件:  

在库文件工程的根目录下创建以下文件:
Android.mk
该文件是用来编译工程的,名称固定。
在文件中加入以下内容:

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := $(call all-java-files-under, src)
LOCAL_JAVA_LIBRARIES := mylib
LOCAL_PACKAGE_NAME := myapp
LOCAL_CERTIFICATE := platforminclude 
$(BUILD_PACKAGE)

其中的 LOCAL_JAVA_LIBRARIES 表示程序会用到的库文件。
其名成与库工程下的 .mk 文件中 MAKE_JAR 部分下的 LOCAL_MODULE 应该是一致的。
其中的 LOCAL_PACKAGE_NAME 表示应用最后生成的名称。

(4)把Android 测试case拷入android源码编译环境编译

把测试case同样拷入system/app下面
然后通过mm编译生成了myapp.apk
然后通过adb install 命令把apk安装到设备
最后按照常规方式用eclipse编译生成的apk与通过此方法编译生成的apk,发现两个大小差异非常大

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324736511&siteId=291194637