livto
livto

Reputation: 39

Android studio can't publish a library on mavenLocal with maven-publish plugin

I searched a lot on the web, but couldn't find a solution. I have a a project named VolleyLib with a library module named volleylib. I'm trying to publish the library on mavenLocal repo, but when build the project no publishing task is executed (only prepareLintJarForPublish). And of course nothing goes into .m2 folder (which I created myself after installed maven...). I can't really get why and what is wrong. Probably something's missing... Here's my build.gradle for volleylib module:

plugins {
    id 'com.android.library'
    id 'maven-publish'
}

android {
    compileSdk 31

    defaultConfig {
        minSdk 22
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles "consumer-rules.pro"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.4.0'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    implementation 'com.android.volley:volley:1.2.1'

    implementation 'com.fasterxml.jackson.jr:jackson-jr-annotation-support:2.13.1'

    implementation 'androidx.room:room-common:2.4.1'
    annotationProcessor 'androidx.room:room-compiler:2.4.1'
    implementation 'androidx.room:room-coroutines:2.1.0-alpha04'
    implementation 'androidx.room:room-runtime:2.4.1'
    implementation 'androidx.room:room-migration:2.4.1'
    implementation 'androidx.room:room-rxjava2:2.4.1'
}

afterEvaluate {
    publishing {
        publications {
            // Creates a Maven publication called "release".
            release(MavenPublication) {
                // Applies the component for the release build variant.
                from components.release

                // You can then customize attributes of the publication as shown below.
                groupId = 'com.example.VolleyLib'
                artifactId = 'final'
                version = '1.0'
            }
            // Creates a Maven publication called “debug”.
            debug(MavenPublication) {
                // Applies the component for the debug build variant.
                from components.debug

                groupId = 'com.example.VolleyLib'
                artifactId = 'final-debug'
                version = '1.0'
            }
        }
        repositories {
            mavenLocal()
        }
    }
}

Any help would be greatly appreciated. It's the first time I try this, so apologize if it's a trivial question...

Thank you all in advance.

Upvotes: 1

Views: 2041

Answers (1)

ישו אוהב אותך
ישו אוהב אותך

Reputation: 29783

My working library build.gradle is almost like yours. Except, it doesn't use the following:

plugins {
    id 'com.android.library'
    id 'maven-publish'
}

but the following:

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

And I'm using Java 8 for compileOptions block:

android {
    ...
    ...
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

And, lastly, without repositories in afterEvaluate:

afterEvaluate {
    publishing {
        ...

        // Remove the following commented line:   
        // repositories {
        //    mavenLocal()
        // }
    }
}

Then, to build and install the library to maven local, click gradle icon on the left menu of Android Studio. Then select projectName -> libraryName -> Run Configuration -> publishToMavenLocal.


UPDATE:

Here the complete build.gradle.

root build.gradle:

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

buildscript {

  repositories {
    jcenter()
    mavenCentral()
    google()
  }
  dependencies {
    classpath 'com.android.tools.build:gradle:7.0.1'
  }
}

repositories {
  mavenCentral()
}

allprojects {
  repositories {
    jcenter()
    mavenLocal()
    mavenCentral()
    maven { url "https://jitpack.io" }
    google()
  }
}

ext {

  compileSdkVersion = 31
  buildToolsVersion = '30.0.2'
  minSdkVersion = 16
  targetSdkVersion = 31

  junitVersion = '4.13.2'

  androidxAppcompatVersion = '1.4.1' 

  androidApplicationId = 'com.project.sample'
  androidVersionCode = 1000
  androidVersionName = "1.0.0"
  androidAppName = 'Sample'
}

my library build.gradle (jcplayer library in root/jcplayer directory):

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

android {
  compileSdkVersion rootProject.ext.compileSdkVersion
  buildToolsVersion rootProject.ext.buildToolsVersion
  defaultConfig {
    minSdkVersion rootProject.ext.minSdkVersion
    targetSdkVersion rootProject.ext.targetSdkVersion
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
  }
  buildTypes {
    release {
      minifyEnabled false
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
  }
}

dependencies {
  implementation "androidx.appcompat:appcompat:$androidxAppcompatVersion"
}


// Because the components are created only during the afterEvaluate phase, you must
// configure your publications using the afterEvaluate() lifecycle method.
afterEvaluate {
  publishing {
    publications {
      // Creates a Maven publication called "release".
      jcplayer(MavenPublication) {
        // Applies the component for the release build variant.
        from components.release

        // You can then customize attributes of the publication as shown below.
        groupId = rootProject.ext.androidApplicationId
        artifactId = 'jcplayer'
        version = rootProject.ext.androidVersionName
      }
    }
  }
}

Upvotes: 0

Related Questions