Foreword: There are a lot of information on the Internet. This article is just a record of my own understanding and practice.
There are three ways of dependency in Android:
1. Module dependency (project module)
2. Local dependencies (mainly for local jar packages)
3. Remote dependency
Module dependency module
Step①:
Create a normal project, write the function you want (preferably a function that can be reused), and delete the activity and layout files of the project itself. Find the project's build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.lihang.mydialog"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
Change'com.android.application' to:'com.android.library'
The applicationId "com.lihang.mydialog" Delete this line
Sync Now after finishing these two parts. Refresh the project and you're done
Step ②:
Create a new project and rely on the uploaded module. File-->New-->Import Module, the following appears:
Check Import at this time. Also modify the Module name. That's it! ~
Local dependencies (jar package: only package .class files, not resource files)
Step①:
Also create a new project, then File-->New-->New Module, select Android Library
Then check build.gradle , application dependencies. In order to test whether it is successful later, create a new class in the module
public class Utils {
public static String showLog(){
return "jar--返回的数据";
}
}
Step ②:
Add the following code to your module's build.gradle, which is equal to the android tag
//修改jar名字+将指定jar生成的地方
task makeJar(type:Copy){
//如果之前存在,则先删除
delete 'build/libs/demo.jar'
//设置拷贝的文件
from('build/intermediates/packaged-classes/debug/')
//生成jar包后的文件目录位置
into('build/libs/')
//include,exclude参数来设置过滤
include('classes.jar')
//重命名
rename('classes.jar','demo.jar')
}
makeJar.dependsOn(build)
After completing the above steps, click Gradle on the right side of android studio and find your module project Tasks-->other-->makeJar, as shown below
After successful packaging, the following figure:
Then in your module's build-->libs-->demo.jar, you can see that this is done.
You can create a new project and copy demo.jar to libs. Then add a reference. Finally, call the utils class just now in the project. The result was successful.
Note again: The packaged jar will not include resource files. If you need resource files, you can package them into aar
So how to package it into an aar file? , Continue to look at (in fact, aar and module dependencies are the same thing)
Step ③:
Find the module dependency module at the beginning of this article, and after converting a normal project into dependency, directly as shown in the figure below, select Tasks-->build-->assemble of the current module
Then build-->outputs-->aar in your project module, as shown below:
After the generation is successful, how to use it. In fact, the same as module, File-->New-->New Module, select Import.JAR/.AAR Package
As shown:
After doing this step, add dependencies like normal modules. That's it! ! Can be used normally,
Remote dependency (projects often use third-party libraries on github)
How to turn your project into a remote dependency?
Step①:
Complete the step ① part of the local dependency (jar package: only package the .class file, not the resource file) . Put the function code you need into the library, and upload the code to github (note that remote dependencies contain resource files). If you want to upload a github tutorial, you can leave a message. The article will continue to be updated
like this:
Then click release in the red circle. As shown below
After filling in the version number and description. Just click Publish release.
Then log on to https://jitpack.io/ official website. Log in to your github account as shown in the figure, and find the library project you just did:
(In the box are all your github projects, find your library project, then click get it, then congratulations on your success. The new project introduces dependencies, I have personally tested it)
If this article is helpful to you, please visit my github, I hope it will help you: