-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Building your own Android library
Building your own Android library enables other developers to take advantage of code that you've written. You can share existing activities, services, images, drawables, resource strings, and layout files that enable other people to leverage your work such as those documented in the must have libraries guide. Also, if your code base begins to take longer times to compile and/or run, creating a library also enables you to iterate faster by working on a more smaller component.
When you create a new Android project, a new application is always created. You can use this application to test your library. After creating the project, go to New
-> New Module
:
Select Android Library
. There is the option to choose Java library
, but there is a major difference in that an Android library, which compiles to an .AAR file, will include not only the Java classes but the resource files, image files, and Android manifest file normally associated with Android.
You will prompted next to provide a name and the module name. The name will simply be used to label the application in the Android Manifest file, while the module name will correspond to the directory to be created:
When you click Next, a directory with the module name will be generated along with other files including a resource and Java folder:
In addition, a build.gradle
file will be created. One major different is that Android applications use the com.android.application
plugin. Android libraries will use the com.android.library
plugin. This signals to the Android Gradle plug-in to generate an .aar
file instead of an .apk
file normally installed on Android devices.
// Android library
apply plugin: 'com.android.library'
We can use the Maven plugin to release snapshots or releases.
apply plugin: 'maven'
def isReleaseBuild() {
return VERSION_NAME.contains("SNAPSHOT") == false
}
def getOutputDir() {
if (isReleaseBuild()) {
return "${project.buildDir}/releases"
} else {
return "${project.buildDir}/snapshots"
}
}
def getDestUrl() {
if (isReleaseBuild()) {
return "s3://yourmavenrepo-bucket/android/releases"
} else {
return "s3://yourmavenrepo-bucket/android/snapshots"
}
}
uploadArchives {
repositories {
mavenDeployer {
repository(url: "file:///" + getOutputDir())
}
}
}
task copyToS3(type: Exec) {
commandLine 'aws', 's3', 'cp', '--recursive', getOutputDir(), getDestUrl()
}
copyToS3.dependsOn uploadArchives
Currently the Gradle/Amazon S3 integration does not support IAM roles. You will need to provide an access key or secret. To circumvent this issue, you can output the repository to a private repo and use the AWS command-line client to copy the snapshot dirs.
For the app, add the entry to your build.gradle
file:
allprojects {
repositories {
jcenter()
maven {
url "s3://yourmavenrepo-bucket/android/snapshots"
credentials(AwsCredentials) {
accessKey "<ACCESS KEY>"
secretKey "<SECRET KEY>"
}
}
}
If you are trying to access a private Amazon S3 repository, you may see an AWS authentication requires a valid Date or x-amz-date header
error. It is a known issue with Gradle and Java versions.
To fix this issue, you will need to upgrade to Gradle v2.8 by editing your gradle/wrapper.properties
:
distributionUrl=https\://services.gradle.org/distributions/gradle-2.8-all.zip
Created by CodePath with much help from the community. Contributed content licensed under cc-wiki with attribution required. You are free to remix and reuse, as long as you attribute and use a similar license.
Finding these guides helpful?
We need help from the broader community to improve these guides, add new topics and keep the topics up-to-date. See our contribution guidelines here and our topic issues list for great ways to help out.
Check these same guides through our standalone viewer for a better browsing experience and an improved search. Follow us on twitter @codepath for access to more useful Android development resources.