@linux1s1s
2017-02-08T10:05:37.000000Z
字数 2625
阅读 2301
Tools 2017-02
新建Studio项目,一般我们需要发布到Maven仓库的Module都带有如下性质:
apply plugin: 'com.android.library'
目前新建的Android项目都是规整的目录结构如下:

而对应生成的build.gradle一般如下:
apply plugin: 'com.android.library'android {compileSdkVersion 24buildToolsVersion "24.0.0"defaultConfig {minSdkVersion 15targetSdkVersion 24versionCode 1versionName "1.0"}buildTypes {release {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'}}}dependencies {compile fileTree(dir: 'libs', include: ['*.jar'])testCompile 'junit:junit:4.12'compile 'com.android.support:appcompat-v7:24.1.0'}
如果项目是直接从Eclipse转为Studio的话会多出如下部分
sourceSets {main {manifest.srcFile 'AndroidManifest.xml'jniLibs.srcDirs = ['libs']java.srcDirs = ['src']resources.srcDirs = ['src']aidl.srcDirs = ['src']renderscript.srcDirs = ['src']res.srcDirs = ['res']assets.srcDirs = ['assets']}// Move the tests to tests/java, tests/res, etc...instrumentTest.setRoot('tests')// Move the build types to build-types/<type>// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...// This moves them out of them default location under src/<type>/... which would// conflict with src/ being used by the main source set.// Adding new build types or product flavors should be accompanied// by a similar customization.debug.setRoot('build-types/debug')release.setRoot('build-types/release')}
上面会指定将原先不规则的目录结构转换为规则的Studio结构,下面上传Maven配置的时候会用到。
无论是上面哪一种build.gradle文件,我们需要将下面的配置添加到该文件中
apply plugin: 'maven'android {task sourcesJar(type: Jar) {classifier = 'sources'from android.sourceSets.main.java.srcDirs}artifacts {archives sourcesJar}uploadArchives {repositories {mavenDeployer {repository(url: "http://127.0.0.1:8081/nexus/content/repositories/releases/") {authentication(userName: 'admin', password: 'admin123')}snapshotRepository(url: "http://127.0.0.1:8081/nexus/content/repositories/snapshots/") {authentication(userName: 'admin', password: 'admin123')}pom.groupId = 'com.aspect.plugin'pom.artifactId = 'deploy'pom.version = '1.0.0'pom.project {name 'Android Library'packaging 'aar'}}}}}
接下来运行./gradlew uploadArchives即可,运行成功以后,检查maven仓库中已经出现了aar文件

其对外的访问地址

上面maven上传的aar如何使用?
在项目级别的build.gradle配置
buildscript {repositories {jcenter()maven { url "http://127.0.0.1:8081/nexus/content/groups/public/" }}dependencies {classpath 'com.android.tools.build:gradle:2.2.1'// NOTE: Do not place your application dependencies here; they belong// in the individual module build.gradle files}}allprojects {repositories {maven { url "http://127.0.0.1:8081/nexus/content/groups/public/" }jcenter()}}
然后再具体的module级别的gradle中需要引用的地方配置即可
dependencies {compile fileTree(dir: 'libs', include: ['*.jar'])compile 'com.aspect.plugin:deploy:1.0.0'}
sync 一下即可。
附注:
maven和gradle转化

