[关闭]
@linux1s1s 2017-02-08T18:05:37.000000Z 字数 2625 阅读 1974

Tool Time-Android Studio发布项目到Maven仓库

Tools 2017-02


新建Android Studio项目

新建Studio项目,一般我们需要发布到Maven仓库的Module都带有如下性质:

  1. apply plugin: 'com.android.library'

目前新建的Android项目都是规整的目录结构如下:

此处输入图片的描述

而对应生成的build.gradle一般如下:

  1. apply plugin: 'com.android.library'
  2. android {
  3. compileSdkVersion 24
  4. buildToolsVersion "24.0.0"
  5. defaultConfig {
  6. minSdkVersion 15
  7. targetSdkVersion 24
  8. versionCode 1
  9. versionName "1.0"
  10. }
  11. buildTypes {
  12. release {
  13. minifyEnabled false
  14. proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
  15. }
  16. }
  17. }
  18. dependencies {
  19. compile fileTree(dir: 'libs', include: ['*.jar'])
  20. testCompile 'junit:junit:4.12'
  21. compile 'com.android.support:appcompat-v7:24.1.0'
  22. }

如果项目是直接从Eclipse转为Studio的话会多出如下部分

  1. sourceSets {
  2. main {
  3. manifest.srcFile 'AndroidManifest.xml'
  4. jniLibs.srcDirs = ['libs']
  5. java.srcDirs = ['src']
  6. resources.srcDirs = ['src']
  7. aidl.srcDirs = ['src']
  8. renderscript.srcDirs = ['src']
  9. res.srcDirs = ['res']
  10. assets.srcDirs = ['assets']
  11. }
  12. // Move the tests to tests/java, tests/res, etc...
  13. instrumentTest.setRoot('tests')
  14. // Move the build types to build-types/<type>
  15. // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
  16. // This moves them out of them default location under src/<type>/... which would
  17. // conflict with src/ being used by the main source set.
  18. // Adding new build types or product flavors should be accompanied
  19. // by a similar customization.
  20. debug.setRoot('build-types/debug')
  21. release.setRoot('build-types/release')
  22. }

上面会指定将原先不规则的目录结构转换为规则的Studio结构,下面上传Maven配置的时候会用到。

配置Gradle

无论是上面哪一种build.gradle文件,我们需要将下面的配置添加到该文件中

  1. apply plugin: 'maven'
  2. android {
  3. task sourcesJar(type: Jar) {
  4. classifier = 'sources'
  5. from android.sourceSets.main.java.srcDirs
  6. }
  7. artifacts {
  8. archives sourcesJar
  9. }
  10. uploadArchives {
  11. repositories {
  12. mavenDeployer {
  13. repository(url: "http://127.0.0.1:8081/nexus/content/repositories/releases/") {
  14. authentication(userName: 'admin', password: 'admin123')
  15. }
  16. snapshotRepository(url: "http://127.0.0.1:8081/nexus/content/repositories/snapshots/") {
  17. authentication(userName: 'admin', password: 'admin123')
  18. }
  19. pom.groupId = 'com.aspect.plugin'
  20. pom.artifactId = 'deploy'
  21. pom.version = '1.0.0'
  22. pom.project {
  23. name 'Android Library'
  24. packaging 'aar'
  25. }
  26. }
  27. }
  28. }
  29. }

gradlew uploadArchives

接下来运行./gradlew uploadArchives即可,运行成功以后,检查maven仓库中已经出现了aar文件

此处输入图片的描述

其对外的访问地址

此处输入图片的描述

使用

上面maven上传的aar如何使用?

在项目级别的build.gradle配置

  1. buildscript {
  2. repositories {
  3. jcenter()
  4. maven { url "http://127.0.0.1:8081/nexus/content/groups/public/" }
  5. }
  6. dependencies {
  7. classpath 'com.android.tools.build:gradle:2.2.1'
  8. // NOTE: Do not place your application dependencies here; they belong
  9. // in the individual module build.gradle files
  10. }
  11. }
  12. allprojects {
  13. repositories {
  14. maven { url "http://127.0.0.1:8081/nexus/content/groups/public/" }
  15. jcenter()
  16. }
  17. }

然后再具体的module级别的gradle中需要引用的地方配置即可

  1. dependencies {
  2. compile fileTree(dir: 'libs', include: ['*.jar'])
  3. compile 'com.aspect.plugin:deploy:1.0.0'
  4. }

sync 一下即可。

附注:

maven和gradle转化

此处输入图片的描述

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注