how to load class dynamically from aar file with DexClassLoader

You can not load aar file at runtime because aar file contains resources and classes.jar file and does not conatain a dex file.
But
you can use injector gradle plugin to get dex from your aar and merge all your aar resources into your project and after that you can use injector-android lib to load that dex files at runtime. Check out inject-example project


the diffent about the jar and dex file

the below article detail descript about the diffrend and you can read it ,then you can get the result

[AAR to DEX] Loading and Running Code at Runtime in Android Application

The difference between JAR and AAR

JAR is just a Java library containing java class files and nothing more. AAR is a format for android libraries which contains a JAR file, android resource file (like layout XML, attribute XML, etc..) and an android manifest file. During the build, an R.java file is generated for each android library and for the main project of course, and all java files are compiled to a one or multiple DEX files (DEX is a Dalvik executable format which can be loaded by android runtime (ART) ). So in an APK, there are only DEX files(not JAR or AAR file), and resources and manifest. Android R.java file is an auto-generated file by AAPT (Android Asset Packaging Tool) that contains resource IDs for all the resources of res/ directory.

Why do I need to load some code at runtime?

There are many reasons to do that. Maybe your dependency libraries are too big and you want you APK to have a small size or maybe that libraries are requested for some feature which is not supported for all devices or it is not required at first launch and you have your own logic for differentiation if device supports that feature or not, or if you need to show user the feature or not. Why ship the APK with that feature code? If you are reading this I think you already have your own reasons :)

JAR To DEX

Android does not support loading JAR file, so there must be a way to compile the JAR file to DEX file. For that, there is the D8 tool which is located in android_sdk/build-tools/version/. To convert JAR to DEX you can run this command from command line

d8 --release --output lib.dex path_to_jar_lib.jar

The DEX file is generated and there is no need to build the android project with that JAR library, so in gradle dependancies section instead of declaring that library as implementation or an api configuration, it needs to be a provided configuration which means that build this project as if this library exists but DO NOT include that JAR in application source files from which the DEX files are compiled

AAR to DEX

To get DEX file from AAR library is a little bit difficult because you must deal with resource files. AAR contains a JAR file and resources. There isn’t a need to make that resources downloadable because most libraries only contain a few resource files which aren’t large and are mostly layout XML files or some general numbers or booleans or something else. So the right thing to do is to merge that resources with the main project resources and change that dependency to be a provided dependency and convert the JAR file to DEX file. But there is a problem with that JAR file. it is not an ordinary JAR file. During the build time, AAPT will not generate an R java file for that library because the library is a provided dependency and the R file usages in that JAR file will crash at runtime. Instead of that, the application R java file will contain the resources ids including the library resources. So the solution to this problem is to manulay create a R.java file which will delegate all resources ids to the R file with the app package name and compile that R file and put it in jar file which can be done with jar -ufv option. And now imagine that an update for this library is released.

Solution: Injector

As I said at the beginning I have created a solution to this problem. What if I told you that this could be done at build time and you even can’t notice that some resource is being moved from one project to another and you don’t have to remember the command line tools with their flags. The solution is Injector. Injector is a Gradle plugin that does all the above explained for you automatically. First of all, you need to add injector to your Gradle buildscript classpath. Your gradle buildscript should look like this

and so on ......