2

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 = 'fabrizio.iacobucci90@mail.com'
                        }
                    }
                    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 = 'fabrizio.iacobucci90@mail.com'
                    }
                }
                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.

Fabrizio
  • 233
  • 1
  • 10

2 Answers2

1

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

Dinkar Kumar
  • 2,175
  • 2
  • 12
  • 22
  • Hello dinkar! Thanks for this input. Yesteday I figured it out and I was able to let it work also from Github. However, the bounty is up so the points are yours :) How is jitpack btw compared to other repositories? Thanks! – Fabrizio May 15 '21 at 19:21
  • 1
    Thanks a lot, @Fabrizio, earlier I was using jcentre(bintray) for one of my projects but as that deprecated, I tried jitpack, it's super easy, really super easy to use. I even have created a doc on that which you can read if you want https://www.talentica.com/blogs/publish-your-android-library-on-jitpack-for-better-reachability/ – Dinkar Kumar May 16 '21 at 04:53
  • Thanks for the link! I will try this out. Actually I'm using Github pkgs and Sonatype Maven. – Fabrizio May 16 '21 at 10:33
0

import com.nfx.android.rangebarpreference

change fabrizioiacobucci to nfx will solve your poblem

  • Hello Milan, thanks for your answer. That's the old package which I forked on Github. Since it is not updated for years, I started developing my own one based on it so I cannot put nfx in there – Fabrizio May 11 '21 at 12:55
  • try to import your package locally in the project and find a proper error. if it will not return any error then the naming of the package is the resone. – Milan Chauhan May 11 '21 at 13:01
  • I tried it and Android Studio says it cannot understand which kind of files are contained in the jar. So I selected "Classes" but nothing changed, I cannot import it. – Fabrizio May 11 '21 at 14:18
  • FYI the package link: https://github.com/fabrizioiacobucci/range-bar-preference/packages/783553 I see a lot of files created from the publishing. I downloaded the one called "range-bar-preference-1.0.0.jar" for the test – Fabrizio May 11 '21 at 14:19