In the development process of Android, every developer has more or less used third-party open source libraries. Using third-party open source libraries can save developers a lot of energy and time, and then better pay attention to the business logic of the application itself.
Here are some of the open source libraries that developers use most frequently.
Fresco
Fresco is a very powerful open source library for displaying images, and it handles loading and displaying images very well. The ability to load images from the network, local databases, local resources, and to pre-set a preset image placeholder before the image is loaded. There are two levels of cache (memory and hard disk cache)
Dependencies {/ / your app 's other dependencies compile' com. Facebook. The fresco ": fresco" : '} 1.0.1Copy the code
Fresco also provides several other open source libraries that support GIFs, WebP, and more
dependencies { // If your app supports Android versions before Ice Cream Sandwich (API level 14) compile 'com. Facebook. Fresco ": animated - base - support:' 1.0.1 / / For animated GIF support the compile 'com. Facebook. Fresco ": animated - GIF:' 1.0.1 / / For WebP support, O animated WebP compile 'com. Facebook. Fresco ": animated - WebP:' 1.0.1 compile 'com. Facebook. Fresco ": webpsupport:' 1.0.1 / / For WebP support, Without animations compile 'com. Facebook. Fresco ": webpsupport:' 1.0.1 / / dojo.provide the Android support library (you took Already have this or a similar dependency) compile 'com. Android. Support: support - core - utils: 24.2.1'}Copy the code
Glide
Glide is a fast and efficient multimedia management and picture loading framework that encapsulates multimedia decoding, memory and hard disk caching, and is interface friendly
Dependencies {the compile 'com. Making. Bumptech. Glide: glide: 3.7.0' compile 'com. Android. Support: support - v4:19.1.0'}Copy the code
OkHttp
OkHttp is an Android HTTP+HTTP/2 client that encapsulates the connection to the network
Dependencies {the compile 'com. Squareup. Okhttp3: okhttp: 3.6.0'}Copy the code
FastAndroidNetworking
FastAndroidNetworking is a web engine based on OkHttp
Dependencies {the compile 'com. Amitshekhar. Android: android - networking: 0.4.0'}Copy the code
RxJava
RxJava-Reactive Extensions for the JVM
Dependencies {the compile 'IO. Reactivex. Rxjava2: rxjava: at 2.0.5'}Copy the code
package rxjava.examples; import io.reactivex.*; public class HelloWorld { public static void main(String[] args) { Flowable.just("Hello world").subscribe(System.out::println); }}Copy the code
If you are using a platform that does not already support A Java 8 lambda, use the following code
Flowable.just("Hello world") .subscribe(new Consumer<String>() { @Override public void accept(String s) { System.out.println(s); });Copy the code
EventBus
The Android event bus has been optimized to transfer data between Activities, Fragments, Threads, Services, etc., with less code and higher quality
The compile 'org. Greenrobot: eventbus: 3.0.0'Copy the code
Define events
public static class MessageEvent { /* Additional fields if needed */ }
Copy the code
Prepare subscribers
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {/* Do something */};
Copy the code
Register and unregister subscribers
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
@Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
Copy the code
Send the event
EventBus.getDefault().post(new MessageEvent());
Copy the code
Device Year Class
Device Year Class is an Android library that provides better ways to modify applications based on the phone’s hardware
The compile 'com. Facebook. Device. Yearclass: yearclass: 2.0.0Copy the code
int year = YearClass.get(getApplicationContext());
if (year >= 2013) {
// Do advanced animation
} else if (year > 2010) {
// Do simple animation
} else {
// Phone too slow, don't do any animations
}
Copy the code
Network Connection Class
An Android open source library that monitors network connection quality and allows users to adjust application behavior (loading low-quality images and videos, etc.) based on network connection quality
The compile 'com.facebook.net work. Connectionclass: connectionclass: 1.0.1'Copy the code
Android Debug Database
Android Debug Database is a powerful open source library that enables developers to Debug databases and SharedPreferences directly through a browser
DebugCompile 'com. Amitshekhar. Android debug - db: 0.5.0'Copy the code
LeakCanary
LeakCanary is an open source library that detects memory leaks
Dependencies {debugCompile 'com. Squareup. Leakcanary: leakcanary - android: 1.5' releaseCompile 'com. Squareup. Leakcanary: leakcanary - android - no - op: 1.5' testCompile 'com. Squareup. Leakcanary: leakcanary - android - no - op: 1.5'}Copy the code
public class ExampleApplication extends Application { @Override public void onCreate() { super.onCreate(); if (LeakCanary.isInAnalyzerProcess(this)) { // This process is dedicated to LeakCanary for heap analysis. // You should not init your app in this process. return; } LeakCanary.install(this); // Normal app init code... }}Copy the code
MPAndroidChart
A powerful chart making open source library, support line chart, pie chart, radar chart, bubble chart, etc
Dependencies {the compile 'com. Making. PhilJay: MPAndroidChart: v3.0.1'}Copy the code
ButterKnife
ButterKnife is a view-binding tool that generates some corresponding, cleaner code through annotations
Dependencies {the compile 'com. Jakewharton: butterknife: 8.5.1 annotationProcessor 'com. Jakewharton: butterknife - compiler: 8.5.1'}Copy the code
class ExampleActivity extends Activity { @BindView(R.id.user) EditText username; @BindView(R.id.pass) EditText password; @BindString(R.string.login_error) String loginErrorMessage; @OnClick(R.id.submit) void submit() { // TODO call server... } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.simple_activity); ButterKnife.bind(this); // TODO Use fields... }}Copy the code
Dagger
An injection framework
dependencies {
compile 'com.google.dagger:dagger:2.x'
annotationProcessor 'com.google.dagger:dagger-compiler:2.x'
}
Copy the code
GreenDao
GreenDao is an open source Android ORM framework, better operation SQlite, provides a friendly interface to operate the underlying database operations
Realm
Simple and fast storage, save more development time, is a mobile device database
Timber
Timber is an open source log framework
The compile 'com. Jakewharton. Timber: timber: 4.5.1'Copy the code
Androig GPU Image
Provides an image filter framework based on OpenGL
Dependencies repositories {jcenter ()} {the compile 'jp. Co. Cyberagent. Android. Gpuimage: gpuimage - library: the 1.4.1'}Copy the code
@Override public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity); Uri imageUri = ... ; mGPUImage = new GPUImage(this); mGPUImage.setGLSurfaceView((GLSurfaceView) findViewById(R.id.surfaceView)); mGPUImage.setImage(imageUri); // this loads image on the current thread, should be run in a thread mGPUImage.setFilter(new GPUImageSepiaFilter()); // Later when image should be saved saved: mGPUImage.saveToPictures("GPUImage", "ImageWithFilter.jpg", null); }Copy the code
Uri imageUri = ... ; mGPUImage = new GPUImage(context); mGPUImage.setFilter(new GPUImageSobelEdgeDetection()); mGPUImage.setImage(imageUri); mGPUImage.saveToPictures("GPUImage", "ImageWithFilter.jpg", null);Copy the code
Open source library related video explanation
The 8 most commonly used Android modules and third-party open source modules (Jetpack/IOC/RxJava/ Plug-in/hotfix Design/Network Access framework/componentization/Image loading framework