如何设置Android库以与Gradle一起使用

时间:2019-05-28 15:44:21

标签: android gradle android-gradle

我正在尝试编写一些Android库以在多个项目中重用。因此,我希望能够将库存放在回购中,然后使用gradle导入它。我想实现的一个例子是Picasso lib。
https://github.com/square/picasso
开始使用此库所需要做的唯一一件事就是将此行添加到我的应用程序build.gradle中:implementation 'com.squareup.picasso:picasso:2.71828'
有人可以指点我参考或提供步骤吗?

预先感谢

1 个答案:

答案 0 :(得分:-1)

您可以按照此步骤逐步上传android库。 https://medium.com/@anitaa_1990/6-easy-steps-to-upload-your-android-library-to-bintray-jcenter-59e6030c8890 然后,您可以添加build.gradle文件以使用它。

关注此事。

第1步:使用您的GitHub / Google帐户在Bintray中创建一个帐户 步骤2:首先在GitHub中创建一个存储库。然后在Bintray中添加该存储库。 步骤3:在Android Studio中打开您的库项目,并将此行添加到build.gradle文件中:

classpath 'com.novoda:bintray-release:{latest_version}'

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.0-alpha07'

        // Step I: add this line
        classpath 'com.novoda:bintray-release:0.8.0'


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

第4步:将项目详细信息添加到/ app目录下的build.gradle文件中

apply plugin: ‘com.android.library’
publish {

    def groupProjectID = '{package_name}'
    def artifactProjectID = '{name_of_your_library}'
    def publishVersionID = 'library_version_code'

    userOrg = '{username_of_bintray}'
    repoName = 'repository_name'
    groupId = groupProjectID
    artifactId = artifactProjectID
    publishVersion = publishVersionID
    desc = '{library description}'
    website = '{github_url}

}

//Step I: Add the below line

apply plugin: 'com.novoda.bintray-release'
apply plugin: 'com.android.library'



//Step II: Add the below publish details
publish {

    def groupProjectID = 'com.an.optimize'
    def artifactProjectID = 'optimize'
    def publishVersionID = '0.1.0'

    userOrg = 'murthyanitaa'
    repoName = 'Optimize'
    groupId = groupProjectID
    artifactId = artifactProjectID
    publishVersion = publishVersionID
    desc = 'Android library for displaying data based on JSON configuration fetched from server. With this library, you can kiss goodbye to string.xml, dimen.xml, arrays.xml. Keep all your string/integer/array config in one file. The library will automatically fetch the data from the url you provide.'
    website = 'https://github.com/anitaa1990/Optimize'
}


android {
    compileSdkVersion 27
    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'


    implementation "android.arch.work:work-runtime:1.0.0-alpha01"
}

步骤5:上载到Bintray:在Android Studio的终端中,添加以下命令以清理,构建并将库上载到Bintray:

gradle clean build bintrayUpload -PbintrayUser={userid_bintray} -PbintrayKey={apikey_bintray} -PdryRun=false

步骤6:链接到JCenter:在Bintray的项目存储库下,您将看到库以及您指定的版本详细信息。现在,您需要做的就是单击“添加到JCenter”。