As Gradle does not support apklib
dependencies how can one migrate from apklib
dependencies to aar
dependencies? Is it possible to either manually or automatically convert apklib
dependency to aar
? If yes - how, if no - why not?
In this question I assume that I don't have original project for apklib
, but rather the file itself.
You cannot convert the apklib to aar. You have to update the dependencies manually to point to an aar file. The aar is compiled and contain lint and proguard rules which the apklib can't necessarily determine automatically.
You can read a bit more on the differences here:
Android dependencies : apklib vs aar files
apklib doesn't contain object files, only source files, so the only way it can be included in any project is by recompiling it. According to some docs you can read at:
Android dependencies : apklib vs aar files and https://plus.google.com/+ChristopherBroadfoot/posts/7uyipf8DTau
And you can see in Chris Broadfoot's post that the task that generates the apklib just zips up the
AndroidManifest.xml
file and thesrc
andres
directories:Note that his post is about creating an apklib from Gradle, which is a slightly weird thing to want to do, but it provides a pretty clear idea of how one of these things is structured.
The good news is that this is "standard" Android project structure as of when Eclipse was the primary IDE, and Android Studio knows how to import modules that look like this. So follow these steps:
Unpack your apklib into a temporary directory. It's in zip format, so something like
unzip library.apklib
should work.Look at what's unpacked. You should see a directory containing
AndroidManifest.xml
,src
, andres
folders.In Android Studio, inside an existing project, choose File > Import Module and point it at the directory from step 2.
Android Studio should manage the import by copying the files into your project in a new module, arranging them in its preferred directory structure, and will add a
build.gradle
file for the module.At this point you're up and running. If you actually want the
.aar
file, then check to see if your new module is set up as a library module or an application module. In itsbuild.gradle
, if you've gotapply plugin: 'com.android.library'
then it's a library module, and Gradle will generate an.aar
as part of the build: it will be in thebuild/outputs/aar
directory after a successful build.If your module imported as an app module and not a library (
apply plugin: 'com.android.application'
, then change thatapply plugin
statement, and you may also need to remove theapplicationId
statement from the build file. Now it should compile as a library and generate a.aar
.Happy coding!