Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert apklib to aar

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.

like image 866
Dmitry Zaytsev Avatar asked Jan 13 '15 14:01

Dmitry Zaytsev


1 Answers

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

The apklib format was invented as a way to share Android code+resources. It’s essentially a zipped up Android library project, which was already the status quo for code+resource sharing.

And you can see in Chris Broadfoot's post that the task that generates the apklib just zips up the AndroidManifest.xml file and the src and res directories:

task apklib(type: Zip) {
    appendix = extension = 'apklib'

    from 'AndroidManifest.xml'
    into('res') {
        from 'res'
    }
    into('src') {
        from 'src'
    }
}

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:

  1. Unpack your apklib into a temporary directory. It's in zip format, so something like unzip library.apklib should work.

  2. Look at what's unpacked. You should see a directory containing AndroidManifest.xml, src, and res folders.

  3. In Android Studio, inside an existing project, choose File > Import Module and point it at the directory from step 2.

  4. 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.

  5. 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 its build.gradle, if you've got apply 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 the build/outputs/aar directory after a successful build.

  6. If your module imported as an app module and not a library (apply plugin: 'com.android.application', then change that apply plugin statement, and you may also need to remove the applicationId statement from the build file. Now it should compile as a library and generate a .aar.

  7. Happy coding!

like image 170
Scott Barta Avatar answered Oct 03 '22 06:10

Scott Barta