Android studio package aar

Take the official version of android studio 2.0 as an example

1. The aar package is the aar file generated after packaging the src, res, and lib in the android project under Android  studio. After the aar package is imported into other android studio projects, other projects can easily reference source code and resource files

2. Generate aar package steps:

2.1 Open a project with android studio, then create a new Module, select Android Library when creating a new Module, and then press the new normal project operation

 

 

2.2 The new Module type is android Library as shown below

 

2.3 After writing the code in the newly created Module, the aar package will be automatically generated after compiling the entire project, and select the Module you created (mine is paysdk)

Click Make Module 'xxx' in the menu bar Build as shown below:

 

2.4 After the compilation is completed, it will be shown in the Module's build-->outputs-->aar-->xxxxx.aar:

 

 

3. Other androidstudio projects reference aar package

3.1 Copy the aar package to the libs directory as shown below:

 

 

3.2 Configure the build.gradle file:

join in

  repositories {
        flatDir {
        dirs 'libs'
    }

compile(name:'paysdk-debug', ext:'aar') name is followed by the name of your own aar

Complete configuration file:

copy code
apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.umpay.paydemo"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }


}
repositories {
    flatDir {
        dirs 'libs'
    }
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.3.0'
    compile(name: 'paysdk-debug', ext: 'aar')
}
copy code

 

3.3最后一个同步gradle

在这里说一下发布一个aar的注意事项:

修改Module(也就是生成aar的工程,我的是paysdk)里面的代码的时候,重新编译好之后,需要把引用aar项目当中的aar文件删掉,并且也需要删除掉build--intermediates--exploded-aar--paysdk,完了在重新同步

1 要尽量避免定义内部接口,


这其实是一个编程习惯, 接口interface最好是独立定义, 避免定义在类的内部.


因为当你发布aar时, 内部的接口在混淆后会独立成一个外部的接口Outer$InnerInterface.


然后麻烦来了, 别人在实现这个类的时候必须也写成XXX implement Outer$InnerInterface{}的形式. 否则是找不到这个类的.


想写成XXX implement Outer.InnerInterface{}是不行的, 因为在Outer里没有这个InnerInterface的定义, 在混淆后, 所有的内部类都被改写成了形如Outer$Inner的外部类了. 包括interface, enum, 等等.


更糟糕的是, 你很可能只是发布项目的一部分代码作为aar, 那你的项目内已经引用的Outer.Inner的类或接口都必须写成Outer$Inner的形式, 否则编译不过, 否则运行时找不到类. 所以, 为了以后不必要的麻烦, 尽量避免使用内部类, 内部接口,


除非你确认不会混淆代码, 或者会将所有定义和调用都参与混淆.


2 使用maven发布需要将所有依赖都发布到maven上

If you publish multiple aars and each aar depends on each other, then you need to define these dependencies in the form of maven, otherwise, at compile time, maven will report a null pointer exception when parsing the pom of an aar.

The formal definition of maven:

apply plugin: 'maven'
compile 'com.nineoldandroids:library:2.4.0+'

3 The attributes or names defined in res should be prefixed

It's normal to define resource files in your aar. The resource file is likely to have the same name as someone else's resource, and the tragedy is that someone else may have the same name as yours because of a reference to another aar. He can't modify that The resource name of aar, just like he cannot modify your aar resource name. In order to avoid this embarrassment, it is best to add a prefix to the defined resource process, such as your module abbreviation, trademark abbreviation, etc. To avoid conflicts, You can also advertise, why not do it.

Guess you like

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