I cannot declare import for gradle dependency of my just published library

1.2k Views Asked by At

UPDATE2: I was not generating a aar file, now it's included in the package that you can check here: https://github.com/fabrizioiacobucci/range-bar-preference/packages/787218 I see javadoc, sources and aar are there, but when I add the package as dependency in another project I don't even see it in the External Libraries.

UPDATE: This is the problem, I don't see my source files in the downloaded jar:

enter image description here

I recently forked a little project on Github and migrated its old code version to AndroidX and new gradle build. After some time everything work fine and I was able also to publish the library on Git packages. However, I tried to declare it as dependency on a different project on my local computer. It downloads fine but when I try to import it in a source file, I cannot find the package:

Import not resolved

If I go into the project folder I see the library downloaded and related files.

This is the Project build.gradle file of the published library.

build.gradle (project)

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.2.0'
        classpath 'io.github.gradle-nexus:publish-plugin:1.1.0'
    }
}

plugins {
    id 'maven-publish'
}
apply from: "${rootDir}/scripts/publish-root.gradle"
apply plugin: 'io.github.gradle-nexus.publish-plugin'

ext {
    VERSION = '1.0.0'
    DESCRIPTION = 'A range bar that can be used as an android shared preference'
    GROUPID = 'com.fabrizioiacobucci.android'
    ARTIFACTID = 'range-bar-preference'
    GITREPO = 'https://github.com/fabrizioiacobucci/tree/development/range-bar-preference.git'
    PROJECTURL = 'https://github.com/fabrizioiacobucci/tree/development/range-bar-preference'
    PUBLISHGIT = 1
    PUBLISHMAVEN = 0
}

nexusPublishing {
    repositories {
        sonatype {
            stagingProfileId = '1c4ab2dc896731'
            //packageGroup = "com.fabrizioiacobucci.android"
            username = ossrhUsername
            password = ossrhPassword
            nexusUrl.set(uri("https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/"))
            snapshotRepositoryUrl.set(uri("https://s01.oss.sonatype.org/content/repositories/snapshots/"))
        }
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

build.gradle (module)

apply plugin: 'com.android.library'

android {
    compileSdkVersion 30
    buildToolsVersion '31.0.0 rc3'

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 30
        version VERSION

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        vectorDrawables.useSupportLibrary = true
        versionCode 1
        versionName VERSION
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    testOptions {
        unitTests {
            includeAndroidResources = true
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test:runner:1.3.0'
    androidTestImplementation 'androidx.test:rules:1.3.0'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.legacy:legacy-preference-v14:1.0.0'
    implementation 'com.appyvet:materialrangebar:1.3'
    implementation 'androidx.appcompat:appcompat:1.2.0'

    testImplementation 'junit:junit:4.13.2'
    testImplementation 'org.robolectric:robolectric:4.5.1'
}

apply from: "${rootDir}/scripts/publish-module-maven.gradle"
apply from: "${rootDir}/scripts/publish-module-githubpkg.gradle"

publish-module-maven.gradle

apply plugin: 'maven-publish'
apply plugin: 'signing'

task androidSourcesJar(type: Jar) {
    archiveClassifier.set('sources')
    from android.sourceSets.main.java.srcDirs
}

task javadoc(type: Javadoc) {
    failOnError false
    source = android.sourceSets.main.java.srcDirs
    classpath = configurations.compile
}

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

artifacts {
    archives androidSourcesJar
    archives javadocJar
}

afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                // The coordinates of the library, being set from variables that
                // we'll set up later
                groupId GROUPID
                artifactId ARTIFACTID
                version VERSION

                artifact("$buildDir/outputs/aar/range-bar-preference-release.aar")

                artifact androidSourcesJar
                artifact javadocJar

                // Mostly self-explanatory metadata
                pom {
                    name = 'Range Bar Preference'
                    description = 'A range bar that can be used as an android shared preference'
                    url = PROJECTURL
                    groupId GROUPID
                    licenses {
                        license {
                            name = 'The Apache Software License, Version 2.0'
                            url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                            distribution = 'repo'
                        }
                    }
                    developers {
                        developer {
                            id = 'FabrizioIacobucci'
                            name = 'Fabrizio Iacobucci'
                            email = '[email protected]'
                        }
                    }
                    scm {
                        connection = 'scm:git:github.com/fabrizioiacobucci/range-bar-preference.git'
                        developerConnection = 'scm:git:ssh://github.com/fabrizioiacobucci/range-bar-preference.git'
                        url = 'https://github.com/fabrizioiacobucci/range-bar-preference/'
                    }
                }
            }
        }
    }
}

ext["signing.keyId"] = rootProject.ext["signing.keyId"]
ext["signing.password"] = rootProject.ext["signing.password"]
ext["signing.secretKeyRingFile"] = rootProject.ext["signing.secretKeyRingFile"]

signing {
    sign publishing.publications
}

publish-module-githubpkg.gradle

artifacts {
    archives androidSourcesJar
    archives javadocJar
}

project.publishing {
    publications {
        maven(MavenPublication) {
            groupId = GROUPID
            artifactId = ARTIFACTID
            version = VERSION

            artifact("$buildDir/outputs/aar/range-bar-preference-release.aar")

            artifact androidSourcesJar
            artifact javadocJar

            versionMapping {
                usage('java-api') {
                    fromResolutionOf('runtimeClasspath')
                }
                usage('java-runtime') {
                    fromResolutionResult()
                }
            }
            pom {
                name = 'Range Bar Preference'
                packaging = 'aar'
                description = 'A range bar that can be used as an android shared preference'
                url = PROJECTURL
                licenses {
                    license {
                        name = 'The Apache Software License, Version 2.0'
                        url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                        distribution = 'repo'
                    }
                }
                developers {
                    developer {
                        id = 'FabrizioIacobucci'
                        name = 'Fabrizio Iacobucci'
                        email = '[email protected]'
                    }
                }
                scm {
                    connection = 'scm:git:github.com/fabrizioiacobucci/range-bar-preference/range-bar-preference.git'
                    developerConnection = 'scm:git:ssh://github.com/fabrizioiacobucci/range-bar-preference/range-bar-preference.git'
                    url = 'https://github.com/fabrizioiacobucci/range-bar-preference/'
                }
            }
        }
    }
    repositories {
        maven {
            name = "GitHubPackages"
            url = uri('https://maven.pkg.github.com/fabrizioiacobucci/range-bar-preference')
            credentials {
                username = System.getenv("GITHUB_USER")
                password = System.getenv("GITHUB_TOKEN")
            }
        }
    }
}

I don't know if there is anything else relevant to share, please let me know in case. Do you have any idea what am I missing?

Thanks a lot in advance for any help.

2

There are 2 best solutions below

3
On BEST ANSWER

As promised I forked the original project upgraded to android and then all the dependencies to the latest.

For the fast release of SDK to do the POC, I released it to jitpack.io

How to add the new library as a dependency.

  • Add it in your root build.gradle at the end of repositories:

     allprojects {
     repositories {
         ...
         maven { url 'https://jitpack.io' }
     }
    

    }

  • Add this to your app module dependencies:

     dependencies {
        implementation 'com.github.dk19121991:range-bar-preference:0.0.7'
     }
    

I did try it myself and it's working fine for me, I updated the app module in my forked version to use the library from jitpack only, and it's working smoothly.

Please try the library and let me know if you feel anything else you need.

https://github.com/dk19121991/range-bar-preference

4
On

import com.nfx.android.rangebarpreference

change fabrizioiacobucci to nfx will solve your poblem