Use Android Studio jar packaging or aar

In the daily development, will inevitably encounter sdk development tasks, and the development of the main there are two cases sdk (at least for now his experience is so) that only packaged sdk Java files or packaged in both Java sdk file another resource file, this blog will be divided into car terms how to use Android studio name jar packaging.

table of Contents

jar package contains only java files

Jar containing the resource and layout files

Use aar package


jar package contains only java files

1. Here first in the right-app or Android studio engineering -> new -> Modult -> Android Library, then as shown below:

2. Add to the app testLib dependencies: the right app -> open module settings -> Dependencies -> + number (select Modult dependency) -> select testlib

3. Add the appropriate logic to write code in testlib.

4. After the app to compile, debug and release will have two folders in testlib / build / intermediates / intermediate-jars directory after compile (compile time based on app you choose may be a release or debug), point open the corresponding folder will see classes.jar file, in fact, this is a jar package jar in the form of our testlib.

Note: In many other online tutorials noted classes.jar directory is build / intermediates / bundles / below but I found studio test after 3.0 do not have this folder, so if I say that some students can not find an address can follow this to try.

At this point you can get other items used inside, if you need to add confusion can refer to my other blog post about proguard confusion of a summary .

有些同学可能觉得这个路径不太好找,这里提供一段gradle脚本,编译后自动对我们指定目录下的classes.jar文件进行copy和移动重命名。(在testlib的build.gradle文件中最外层添加如下task)

task makeJar(type: Copy) {
    //删除build/libs/目录中存在的jar
    delete 'build/libs/testlib.jar'
    //设置拷贝的文件目录
    from('build/intermediates/intermediate-jars/debug/')
    //copy后jar包的文件目录
    into('build/libs/')
    //include ,exclude参数来设置过滤
    //(我们只关心classes.jar这个文件)
    include('classes.jar')
    //重命名
    rename ('classes.jar', 'testlib.jar')
}

makeJar.dependsOn(build)

然后在Terminal中执行gradlew makeJar(Windows)或者./gradlew makeJar(mac)即可在build/libs下生成testlib.jar

含有资源和布局文件的jar

1.这里首先在Android studio中右键app或者工程-->new-->Modult-->Android Library,之后如下图:

2.给app添加testLib的依赖关系:右键app-->open module settings-->Dependencies-->+号(选择Modult dependency)-->选择testlib

3.在testlib中添加编写相应的逻辑代码。

4.完成后编译app,编译通过之后会在testlib/build/outputs/aar目录下会有testlib-release.aar和testlib-debug.aar文件(根据你app编译时候选择的是release还是debug可能只有一个),将这个aar文件导出给其他应用使用。

aar包的使用

1.在app的libs目录下添加aar包

2.在app的build.gradle中的android标签中添加:

repositories {  
        flatDir {  
            dirs 'libs'  
        }  
    }

3.添加aar依赖

dependencies {  
    compile fileTree(include: ['*.jar'], dir: 'libs')  
    compile(name: 'aar名字', ext: 'aar')  
}

当一个library类型的module需要引用aar文件时:

1.先在该library中按照上面123的代码,在library的module下的bulid.gradle中配置

2.任何依赖此library的module必须声明在它的build.gradle声明此 aar 的 lib 所在的位置,这个位置根据文件路径所确定。

android{    //android节点下配置
    repositories {
          flatDir {
            dirs 'libs', '../../../../library_module/libs'
          }
    }
}

3.配置完②步骤后,还要在project的build.gradle文件中配置,如下

allprojects {
    repositories {
        jcenter()
        flatDir {
            dirs '../../../library_module/libs'
        }
    }
}

注意:dirs 所指示的是文件路径,所以针对不同的位置配置,“../../../library_module/libs” 所代表的路径也是不一致的。

 

 

参考资料:

https://www.cnblogs.com/aimqqroad-13/p/8514274.html

发布了10 篇原创文章 · 获赞 4 · 访问量 1002

Guess you like

Origin blog.csdn.net/qq_25097107/article/details/88778208