2015-02-06 132 views
23

我有一个使用Gradle和Android Studio的库和Android应用程序。我可以直接包含库项目如下Gradle:如何将Android库发布到本地存储库

compile project(':library') 

因为我不想与库源代码网了,我想给库发布到本地资源库,这样我可以为

使用
compile 'com.mygroup:library:1.0' 

有没有建议吗?

回答

39

我只是找到了解决办法。在库项目的build.gradle,添加此

apply plugin: 'maven' 

group = 'com.mygroup' 
version = '1.0' 

uploadArchives { 
    repositories { 
     mavenDeployer { 
      repository(url: "file://[your local maven path here]") 
     } 
    } 
} 

在项目文件夹中,键入以下命令

gradle uploadArchives 

阅读​​了解更多信息

+2

'叫它[本地Maven路径这里]'像'〜/ .m2'文件夹? – zygimantus 2017-05-08 20:53:42

+3

知识库(网址:mavenLocal())。getUrl()) – abg 2017-08-01 13:48:17

0

settings.gradle添加

include 'riz.com.mylibrary' 
project(':riz.com.mylibrary').projectDir = new File('C:\\Users\\Rizwan Asif\\AndroidStudioProjects\\mylibrary') 

然后在build.gradle中依赖资本投资者入境计划添加

compile project(':riz.com.mylibrary') 
+1

如果您同时开发一个库,这是最好的方法,但是当我尝试对同步进行分级时,出现'Configuration with name'默认'not found'错误。 – Megakoresh 2016-10-05 08:16:10

25

对于Android的图书​​馆,你应该使用Android摇篮,Maven插件https://github.com/dcendents/android-maven-gradle-plugin

buildscript { 
    repositories { 
     mavenCentral() 
    } 

    dependencies { 
     classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3' 
    } 
} 

apply plugin: 'com.android.library' 
apply plugin: 'com.github.dcendents.android-maven' 

然后发布到你的本地库运行:

./gradlew install 

这将安装它在$ HOME/.m2/repository中。在您的应用程序或其他项目中,您可以通过将mavenLocal()添加到您的存储库来包含它。

或者,如果您的库位于GitHub上,那么您可以使用JitPack简单地将其包含在其他项目中。然后,您不必运行上述命令,只需使用已推送的内容即可。

+0

这应该得到信用作为正确的答案。 – 2016-02-28 00:39:27

+3

我想还需要lib中的'group ='com.example' version ='1.0''。 – WindRider 2016-05-24 15:51:41

4

我更喜欢将Java源代码和javadoc添加到Maven存储库。以下脚本使用android maven plugin将您的Android库发布到Maven存储库。它创建.aar,javadoc.jar,sources.jar和.pom,并在将文件上传到Maven存储库后更新maven-metadata.xml。我也把脚本放在GitHub上。

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

//Your android configuration 
android { 
    //... 
} 

//maven repository info 
group = 'com.example' 
version = '1.0.0' 

ext { 
    //Specify your maven repository url here 
    repositoryUrl = 'ftp://your.maven.repository.com/maven2' 
    //Or you can use 'file:\\\\C:\\Temp' or 'maven-temp' for a local maven repository 
} 

//Upload android library to maven with javadoc and android sources 
configurations { 
    deployerJars 
} 

//If you want to deploy to an ftp server 
dependencies { 
    deployerJars "org.apache.maven.wagon:wagon-ftp:2.2" 
} 

// custom tasks for creating source/javadoc jars 
task javadoc(type: Javadoc) { 
    source = android.sourceSets.main.java.srcDirs 
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) 
    destinationDir = file("../javadoc/") 
    failOnError false 
} 

task javadocJar(type: Jar, dependsOn: javadoc) { 
    classifier = 'javadoc' 
    from javadoc.destinationDir 
} 

//Creating sources with comments 
task androidSourcesJar(type: Jar) { 
    classifier = 'sources' 
    from android.sourceSets.main.java.srcDirs 
} 

//Put the androidSources and javadoc to the artifacts 
artifacts { 
    archives androidSourcesJar 
    archives javadocJar 
} 

uploadArchives { 
    repositories { 
     mavenDeployer { 
      configuration = configurations.deployerJars 
      repository(url: repositoryUrl) { 
       //if your repository needs authentication 
       authentication(userName: "username", password: "password") 
      } 
     } 
    } 
} 

./gradlew uploadArchives 
相关问题