jfrog / artifactory-gradle-plugin

JFrog Gradle plugin for Build Info extraction and Artifactory publishing.
Apache License 2.0
20 stars 15 forks source link

Support for Android publishing artifacts w/Gradle 8+ and AGP 8.1.2 #77

Open enaess opened 9 months ago

enaess commented 9 months ago

Describe the bug

The use of the 'maven-publish' plugin is being deprecated for Android builds in favor of using Android's

android 
{
     publishing {
           multipleVariants {
                 allVariants()
           }
     }
}

See here: https://developer.android.com/build/publish-library/configure-pub-variants#groovy

Current behavior

I've looked at the examples by git clone https://github.com/jfrog/project-examples.git, and following the gradle example: gradle-android-library-example-ci-server/app/build.gradle

This unfortunately doesn't seem to work at all. I am in lack of a better example / guidance for how to integrate Artifactory into our Android build.

Reproduction steps

git clone https://github.com/jfrog/project-examples.git cd into the android-library-example-ci-server

Do necessary upgrades to run example with Android 8.1.2 and Gradle 8.4

Here is a patch for getting it to comple:

diff --git a/gradle-examples/gradle-android-library-example-ci-server/app/build.gradle b/gradle-examples/gradle-android-library-example-ci-server/app/build.gradle
index dafe38e..ea24999 100644
--- a/gradle-examples/gradle-android-library-example-ci-server/app/build.gradle
+++ b/gradle-examples/gradle-android-library-example-ci-server/app/build.gradle
@@ -1,13 +1,16 @@
-apply plugin: 'com.android.library'
-apply plugin: 'maven-publish'
+plugins {
+    id 'com.android.library' version "8.1.2"
+    id 'maven-publish'
+}

 android {
-    compileSdkVersion 30
-    buildToolsVersion "30.0.3"
+    compileSdkVersion 34
+    buildToolsVersion "34.0.0"

+    namespace "com.example.testlibrary"
     defaultConfig {
-        minSdkVersion 23
-        targetSdkVersion 30
+        minSdkVersion 30
+        targetSdkVersion 34
         versionCode 1
         versionName "1.1s"

@@ -20,10 +23,6 @@ android {
             proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
         }
     }
-    compileOptions {
-        sourceCompatibility JavaVersion.VERSION_1_8
-        targetCompatibility JavaVersion.VERSION_1_8
-    }

     // How to Rename the output AAR file:
     //      - https://stackoverflow.com/questions/24728591/how-to-set-name-of-aar-output-from-gradle
@@ -60,4 +59,4 @@ dependencies {
     testImplementation 'junit:junit:4.+'
     androidTestImplementation 'androidx.test.ext:junit:1.1.3'
     androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
-}
\ No newline at end of file
+}
diff --git a/gradle-examples/gradle-android-library-example-ci-server/build.gradle b/gradle-examples/gradle-android-library-example-ci-server/build.gradle
index 5a7c7fc..a9d6b10 100644
--- a/gradle-examples/gradle-android-library-example-ci-server/build.gradle
+++ b/gradle-examples/gradle-android-library-example-ci-server/build.gradle
@@ -1,25 +1,10 @@
-// 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.2"
-
-        // NOTE: Do not place your application dependencies here; they belong
-        // in the individual module build.gradle files
-    }
-}
-
 allprojects {
     repositories {
         google()
         mavenCentral()
-        jcenter() // Warning: this repository is going to shut down soon
     }
 }

 task clean(type: Delete) {
     delete rootProject.buildDir
-}
\ No newline at end of file
+}
diff --git a/gradle-examples/gradle-android-library-example-ci-server/gradle/wrapper/gradle-wrapper.properties b/gradle-examples/gradle-android-library-example-ci-server/gradle/wrapper/gradle-wrapper.properties
index 92b8e57..aba2543 100644
--- a/gradle-examples/gradle-android-library-example-ci-server/gradle/wrapper/gradle-wrapper.properties
+++ b/gradle-examples/gradle-android-library-example-ci-server/gradle/wrapper/gradle-wrapper.properties
@@ -1,6 +1,6 @@
 #Wed Aug 11 17:23:15 ART 2021
 distributionBase=GRADLE_USER_HOME
-distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
 distributionPath=wrapper/dists
 zipStorePath=wrapper/dists
 zipStoreBase=GRADLE_USER_HOME
diff --git a/gradle-examples/gradle-android-library-example-ci-server/settings.gradle b/gradle-examples/gradle-android-library-example-ci-server/settings.gradle
index a52a854..e10870b 100644
--- a/gradle-examples/gradle-android-library-example-ci-server/settings.gradle
+++ b/gradle-examples/gradle-android-library-example-ci-server/settings.gradle
@@ -1,2 +1,9 @@
+pluginManagement {
+    repositories {
+        google()
+        mavenCentral()
+    }
+}
+
 rootProject.name = "test-library-name"
 include ':app'

Produces the following error:

* What went wrong:
A problem occurred configuring project ':app'.
> Failed to notify project evaluation listener.
   > Could not create an instance of type com.android.build.api.variant.impl.LibraryVariantBuilderImpl.
      > Namespace not specified. Specify a namespace in the module's build file. See https://d.android.com/r/tools/upgrade-assistant/set-namespace for information about setting the namespace.

        If you've specified the package attribute in the source AndroidManifest.xml, you can use the AGP Upgrade Assistant to migrate to the namespace value in the build file. Refer to https://d.android.com/r/tools/upgrade-assistant/agp-upgrade-assistant for general information about using the AGP Upgrade Assistant.
   > Could not get unknown property 'release' for SoftwareComponent container of type org.gradle.api.internal.component.DefaultSoftwareComponentContainer.

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.

Expected behavior

Should work

Artifactory Gradle plugin version

5.1.10

Operating system type and version

Ubuntu 22.04 Linux

JFrog Artifactory version

7.41.14

Gradle version

8.4

mricciuti commented 9 months ago

This issus seems similar to https://github.com/jfrog/artifactory-gradle-plugin/issues/75 . From your stack trace it looks like Artifactory plugin uses internal Gradle apis, that were removed in latest Gradle version.

enaess commented 9 months ago

@mricciuti Yes, this seems to be new behavior from version 8.x with AGP. Even when I revert to 7.4.2 (the latest of the AGP 7.x series), it gets passed this "unknown property 'release'" problem.

In looking at this in greater detail, the

android 
{
     publishing {
           multipleVariants {
                 allVariants()
           }
     }
}

Creates a publication for the "components.default". If you use the following

android {
    publishing
         singleVariant("release") {
               withSourcesJar()
         }
    }
}

Creates a publication for the "components.release".

In either case, you can further detail the publication clause:

afterEvaluate {
android 
{
     publishing {
           // name it something else, e.g. multiple
           multipleVariants("multiple") {
                 allVariants()
           }
     }
      publishing {
        publications {
            release(MavenPublication) {
                from components.multiple   // <- becomes multiple instead of "default" if you left the above unnamed.
                artifactId = "${rootProject.name}"
                groupId = "com.example.testlibrary"
                version = "${android.defaultConfig.versionName}"
            }   
        }   
    }   
}
enaess commented 9 months ago

I still think there needs a documentation update on JFrog's behalf, either by updating the gradle examples and/or documentation.