Those things about Android SDK development

Preface

Recently, the company project needs to develop a music sdk for third-party developers to perform operations! As the first development of the SDK, many things are not very clear. You can only use Google if you encounter problems. Now that everything is successful, now record the development steps and problems!

plan selection

In the early stage of development of SDK, you must figure out what package you are out of. Here are two references:

  • jar包
  • arr package

There is a difference between the two, the difference is:

  1. The packaged location is different
AS低版本
jar: /build/intermediates/bundles/debug(release)/classes.jar
AS高版本
jar: /build/intermediates/packaged-classes/release/classes.jar

aar: /build/outputs/aar/libraryname.aar

  1. Jar only contains class files and manifest files.
    In addition to the class files in jar , aar also contains all the resources used in the project. Class and res resource files include all
  2. Different ways of use
jar使用:

1.将打包出来的jar文件加入到libs中

2.在module的build.gradle中加入代码,例如:

implementation files('src/lib/demo.jar')

arr使用 (这里只讲解单层arr依赖):

1.将打包出来的arr文件加入到libs中

2.在module的build.gradle中与android{}平级下加入

       repositories {
           flatDir {
           dirs 'libs'
               }
           }

3.在module的build.gradle中的dependencies里加入

   implementation(name: 'demo', ext: 'aar')//注意这里加入的名字没有后缀名

4.同步后可以在External Libraries中查看新加入的包

How to deal with the tripartite library in the development SDK?

First of all, it is recommended to use third-party libraries as little as possible in the development of SDK, there are many problems when using it! But what should I do if I need to use it under certain circumstances? For example, third-party libraries such as network requests are used. For example, okhttp is now very mature, and we generally choose him as a network request! First we throw a problem

  1. If we add a dependency to the build.gradle in the SDK and package it directly after the development is completed, a problem will arise here is that the package does not contain third-party libraries, and the class cannot be found. At this time, the first The three-party developers themselves import the dependencies needed in our SDK. If we don't provide documentation, the developers don't know which ones to import?
  2. We use the arr packaging method to download the third-party library we use, put it into libs and then package it, so that the package that can be typed out can be packaged into the third-party library for use! But there will be another problem that is that third-party developers often encounter package conflicts!

Provide the following ideas for the above problems:

  • Provide development documentation and indicate which third parties are needed in the documentation. Let developers import commonly used third-party libraries for Android, such as okhttp. Because these are commonly used, developers feel that these are not a problem. I will import these third-party libraries without this SDK.
  • Encapsulate the infrequently used libraries into the SDK, because they are not commonly used and few developers will use it to cause version conflicts, such as dagger2 and so on!
  • Use maven warehouse and bintray for version management

How to encapsulate the tripartite library of annotation classes into SDK?

In fact, what I think of here is a very stupid method, but it is indeed a way to solve the problem. If the reader has a better method, welcome to communicate!

Take dagger2 as an example, how to package it into sdk?

step 1

Everyone knows that using dagger will use the following things:

apply plugin: 'kotlin-kapt'
dependencies{
   implementation 'com.google.dagger:dagger:version'
   kapt 'com.google.dagger:dagger-compiler:versio'
}

After we configure it in the above way and cooperate with our business code, we will run the kapt plug-in to generate the intermediate business code. The generated place is:

/build/generated/source/kapt/debug 

Step 2

We copy all these generated intermediate classes and put them in a new directory created by our SDK, such as the folder dagger created here.

Step 3

Our business code needs to import and generate intermediate classes, such as the following DaggerServiceComponent:

 DaggerServiceComponent.builder().serviceModule(ServiceModule()).build().inject(this)

For the DaggerServiceComponent class, we do not use the intermediate class here, we just use the copied class directly! So basically dagger will work!

Step 4

At this time, we will delete all the dagger configuration in our build.gradle, for example, delete the previous one:

apply plugin: 'kotlin-kapt'
dependencies{
   implementation 'com.google.dagger:dagger:version'
   kapt 'com.google.dagger:dagger-compiler:versio'
}

Step 5

We download the dagger library and put libs:

Step 6

Because we want to download the third-party library and package it into the SDK, so here we use the arr packaging method! Pack it directly! In this way, we throw it to a third-party developer and he does not need to do all kinds of troublesome configurations for the dagger in our SDK!

Way of packaging

Arr packaging is very simple

  1. We have created a new library module
  2. Find the gradle on the right side of the IDE, find the assemble under the build folder of the library and run it

jar packaging is a bit more troublesome

  1. In the build.gradle under the library, if the following code:
def SDK_BASENAME = "$SdkName"  /* 生成jar包的名字*/
def SDK_VERSION = "_V$VersionName"
def sdkDestinationPath = "build"  /*设置生成的jar包输出的路径*/
def zipFile = file('build/intermediates/packaged-classes/release/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: ['res/**'])
    baseName = SDK_BASENAME + SDK_VERSION
    destinationDir = file(sdkDestinationPath)
}

makeJar.dependsOn(deleteBuild, build)

2. Find gradle on the right side of the IDE, find the makeJar command in the other folder of the library, and run it, or you can use the command gradlew makeJar to operate

Confusion packaging can refer to here

After the first SDK development, it is estimated that there are many areas to be optimized! If you have a better way, we can communicate

Author: imkobedroid
link: https://www.jianshu.com/p/5578132d28bb

Guess you like

Origin blog.csdn.net/AndroidAlvin/article/details/107101984
Recommended