I've got a multi-module Android project which have one module called app which is the Android application, that contains features where each feature is separate module.
I would like to extract one of the feature modules as a aar library.
I started from creating a demo app inside the project that have my feature module as a dependency:
implementation project(':player')
I adjusted the code of the app to make it working and in my Demo app inside the project it was working as expected.
My next step was to build the aar file and include it in separate project. I did it by using task:
Gradle -> player -> Tasks -> build -> build
After importing created aar file in new project I'm getting a lot of java.lang.ClassNotFoundException exceptions because classes from modules that player module depends on are not in the generated .jar file.
I didn't mention, but player module depends on few other modules like domain, base-android, data and so on and it seems like all of those modules are not included in aar file.
At first I thought that changing from implementation for those modules to api will solve the problem, but it didn't.
My question is: is there something I'm missing while building the aar library? I thought that if something is working great when included in the module within one project then it should also work like that when after creating a aar file, but it seems like it's not.
You are getting ClassNotFoundException as your module's aar doesn't contains your other submodules' code or aar files. To fix this issue you can check out this gradle plugin that will include all sub modules's arr into your main module automatically. Check out fat AAR, here is the link : https://github.com/kezong/fat-aar-android
Related
In the project that I am working on, the code base has about 25 modules shared between a couple of apps, the idea being that the same code could be reused between the apps.
When I build the project, I almost always get the following error:
Direct local .aar file dependencies are not supported when building an AAR. The resulting AAR would be broken because the classes and Android resources from any local .aar file dependencies would not be packaged in the resulting AAR.
The module causing that error is usually different.
Oddly, when I run the app instead of building the project, it works just fine.
I have tried Invalidating Cache and Restarting and re-cloning the repo but it doesn't fix the issue.
Worth mentioning that the problem seems to affect only a few developers.
Any idea why the project runs but doesn't build?
You are using an AAR file in another project. Parent AAP/Module needs to add all dependencies which you have used in your local AAR file.
I am using Android studio 1.5. I have a library project, core-speech-service.aar. This AAR file is generated correctly (all classes are built and packed inside classes.jar in the AAR file).
Now I am creating another AAR library google-speech-service.aar which depends on the first AAR (core-speech-service). So I add it as library dependency (compile) in the new AAR library project.
So far, so good. Android studio copies core-speech-service.aar in to the new library project as a library module. But when I build the google-speech-service library project, it creates an AAR file, but the packed AAR file neither contains classes from the core-speech-service.aar neither it has the core-speech-service.aar itself.
Am I missing something ?
Thanks
abhay
You will have to reference the library as an outside dependency on your apk. I believe that as of now you cannot have an aar inside another
is it possible to build an apk and an aar library with gradle from the same project source?
Challenge is to modify the build.gradle so that both products will be built. Do you have any advice?
Finally, Gradle offers me a really simple solution for this issue:
I've created a multi module project with Android Studio and Gradle (see http://www.petrikainulainen.net/programming/gradle/getting-started-with-gradle-creating-a-multi-project-build/). One module for the app (Android Application Project) and one for the library (Android Library Project).
After moving all sources, that belong to the library, to the library project, I'm able to build the .aar file and the .apk file (which uses also sources from the library project, no problem thanks to the multi module project).
I switched over to Android Studio a few months ago, but only until recently did I ever add or remove library modules from my project. I've run into a problem where simply including the library module (such as google play services) is insufficient in getting my app to compile.
I made sure the library module uses its own jar as a dependency. The references jar can be seen here:
But unless I include both the library project AND this jar library onto my main app module as a dependency, the app will not compile. I was under the impression that just including the library itself ought to be enough, because it includes all the res/ files AND the jar itself which contains the java files.
I get it to work by doing the following:
But shouldn't just the 1 project library/module be enough? If you look you can see I need to do the same with Android v7's appcompat library.
You don't need to add the the compiled module (the .jar file) in the dependencies of itself module.
In other words, once you import your module to your project, the only other thing you should have to do is to add it as a module dependency to your project.
When you add the module library, Android Studio will automatically generate/add the .jar file to libraries and then to your project's dependencies.
Module Dependencies
Google Play Services Library
Main Project Module Dependencies
I would like to include this plist parser module in my Android application, ideally without just copying the entire module source into my source tree (if that would even work).
I have successfully added the module as a project into Eclipse (3.7.0) and resolved errors by fixing the build path to include Android 2.1, which is what I am using. However, now I'm stuck. All of the information on using external libraries with your Android project I can find expect you to have a JAR of the library, but I only have this source code. I can run the plist parser module as an Android application, which appears to compile an .apk, but that doesn't actually do anything because it's not a standalone application. Any options to just build the module without running it are greyed out in the Eclipse interface.
How can I either build this module into a .jar for inclusion, or include it in some other way?
Edit: In order to clear the errors in the module after I added it to Eclipse, I followed the instructions in this answer.
You can either convert the whole thing to a library project or simply include the source code in your app's project. To create a library project, you can import the project from github, and after you get it compile, remove any activities, go to the project's Properties->Android and check the 'Is a library' check box. Then add it as a dependency to your own project.