现在的位置: 首页 > 移动开发> 正文
基于Gradle生成Android apklibs
2013年08月21日 移动开发 暂无评论 ⁄ 被围观 4,085+

So, you've decided to switch your Android library project to the new hotness (Gradle, Android's new build system)? About to release a new Android library? Read on.

1. What is apklib?
apklib is a way to bundle an Android library project.

Many Android developers thought the existing Android build system, based on Ant, was insufficient. Maven is arguably a better build system, so the android-maven-plugin was created, to let Maven users build their Android projects.

Maven downloads all of the dependencies for your project - for most Java projects, these are jars. 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.

2. Why doesn’t Gradle support it?
The Android team introduced the aar format at Google I/O 2013. From what I gather, it differs from apklib in one major way: classes are compiled and included in a classes.jar in the root of the aar, whereas apklib cannot contain compiled class files or jars (they are ignored).

3. Why doesn’t the android-maven-plugin support aar?
I suppose they haven’t got around to it yet. Plus, apklib came first! Ask +Manfred Moser :-)

4. Show me the code
(you can see a complete example here: https://github.com/googlemaps/android-maps-utils/blob/master/library/build.gradle)

So, if you want to ship an apklib to keep your Maven users happy, you’ll need to generate it and upload from your Gradle build script. I suppose someone will one day release a Gradle plugin, but for now the build rules are simple:

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

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

You may need to tweak this a bit (different paths for source code, resources, etc). You will then want to declare your apklib as an artifact of your project:

artifacts {
archives apklib
}

5. Uploading to Maven
Deploying multiple artifacts to Maven is a bit tricky. If you’re using the mavenplugin from Gradle, you’ll need to modify your mavenDeployer entry:

addFilter('aar') { artifact, file ->
artifact.name == archivesBaseName
}
addFilter('apklib') { artifact, file ->
artifact.name == archivesBaseName + '-apklib'
}

You’ll end up with two artifacts, yourproject and yourproject-apklib. You may want to inverse these (yourproject and yourproject-aar), if you already have people depending on the apklib packaging with the existing name.

文章选自:https://plus.google.com/+ChristopherBroadfoot/posts/7uyipf8DTau

给我留言

留言无头像?


×
腾讯微博