development

Android 라이브러리 Gradle 릴리스 JAR

big-blog 2020. 11. 21. 09:34
반응형

Android 라이브러리 Gradle 릴리스 JAR


android-library프로젝트의 Jar 패키징을 해제하려면 어떻게 해야합니까?
나는 classes.jar이 아래에 build/bundles/release/classes.jar있으며 이것이 올바른 Jar 패키지 ( *.class파일 포함 ) 라고 생각합니다 .

AAR 대신 JAR로 라이브러리를 릴리스하는 공식적인 방법이 있습니까?

편집
Gradle을 사용하여 Maven 아티팩트를 릴리스하고 AAR 패키지와 함께 JAR을 릴리스하고 싶습니다. https://chris.banes.me/2013/08/27/pushing-aars-to-maven-central/을
기반으로 서명, md5, 매니페스트, ...

apply plugin: 'maven'
apply plugin: 'signing'

configurations {
    archives {
        extendsFrom configurations.default
    }
}

def sonatypeRepositoryUrl
if (isReleaseBuild()) {
    println 'RELEASE BUILD'
    sonatypeRepositoryUrl = "https://oss.sonatype.org/service/local/staging/deploy/maven2/"
} else {
    println 'DEBUG BUILD'
    sonatypeRepositoryUrl = "https://oss.sonatype.org/content/repositories/snapshots/"
}

if(!hasProperty('nexusPassword')) {
    ext.set('nexusPassword', System.console().readPassword("\n\$ Type in password for Sonatype nexus account " + nexusUsername + ": "))
}

if(!signing.hasProperty('password')) {
    ext.set('signing.password', System.console().readPassword("\n\$ Type in GPG key password: "))
}

afterEvaluate { project ->
    uploadArchives {
        repositories {
            mavenDeployer {
                beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }

                pom.artifactId = POM_ARTIFACT_ID

                repository(url: sonatypeRepositoryUrl) {
                    authentication(userName: nexusUsername, password: nexusPassword)
                }

                pom.project {
                    name POM_NAME
                    packaging POM_PACKAGING
                    description POM_DESCRIPTION
                    url POM_URL

                    scm {
                        url POM_SCM_URL
                        connection POM_SCM_CONNECTION
                        developerConnection POM_SCM_DEV_CONNECTION
                    }

                    licenses {
                        license {
                            name POM_LICENCE_NAME
                            url POM_LICENCE_URL
                            distribution POM_LICENCE_DIST
                        }
                    }

                    developers {
                        developer {
                            id "loopj"
                            name "James Smith"
                        }
                        developer {
                            id "smarek"
                            name "Marek Sebera"
                        }
                    }
                }
            }
        }
    }

    signing {
        required { isReleaseBuild() && gradle.taskGraph.hasTask("uploadArchives") }
        sign configurations.archives
    }

    task androidJavadocs(type: Javadoc) {
        source = android.sourceSets.main.java.srcDirs
    }

    task androidJavadocsJar(type: Jar) {
        classifier = 'javadoc'
        from androidJavadocs.destinationDir
    }

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

    artifacts {
        archives androidSourcesJar
        archives androidJavadocsJar
    }
}

사용

task androidJar(type: Jar) {
    from android.sourceSets.main.java.srcDirs
}

안드로이드 SDK에 대해 컴파일 및 링크되지 않은 자바 파일 만 패키징합니다.


While I haven't tried uploading the artifacts with a deployment to Sonatype (or even a local repo), here's what I managed to come up with a few weeks ago when trying to tackle the same problem.

android.libraryVariants.all { variant ->
  def name = variant.buildType.name
  if (name.equals(com.android.builder.core.BuilderConstants.DEBUG)) {
    return; // Skip debug builds.
  }
  def task = project.tasks.create "jar${name.capitalize()}", Jar
  task.dependsOn variant.javaCompile
  task.from variant.javaCompile.destinationDir
  artifacts.add('archives', task);
}

Then run the following:

./gradlew jarRelease

Another way to generate a jar from a library project through gradle is as follows:

In your library's build.gradle:

def jarName = 'someJarName.jar'

task clearJar(type: Delete) {
    delete "${project.buildDir}/libs/" + jarName
}

task makeJar(type: Copy) {
    from("${project.buildDir}/intermediates/bundles/release/")
    into("${project.buildDir}/libs/")
    include('classes.jar')
    rename('classes.jar', jarName)
}

makeJar.dependsOn(clearJar, build)

What we are doing here is just copying the classes.jar generated by the Android Gradle plugin. Be sure to look into your build directory for this file and see if its contents are in the way you want.

Then run the makeJar task and the resulting jar will be in library/build/libs/${jarName}.jar

The will have the class files according to your configuration for release. If you are obfuscating it, then the files in the jar will be obfuscated.


Just because the previous answers were not fitting my needs, I share my tricky version to generate a jar with the project classes and the aar/jar dependencies.

// A tricky jar operation, we want to generate a jar files that contains only the required classes used.
// Dependencies are in jar and aar, so we need to open the aar to extract the classes and put all at the same level
// (aar is a zip that contains classes.jar, the latter is a zip that contains .class files)
task jar(type: Jar) {
    from {
        List<File> allFiles = new ArrayList<>();
        configurations.compile.collect {
            for (File f : zipTree(it).getFiles()) {
                if (f.getName().equals("classes.jar")) {
                    allFiles.addAll(zipTree(f).getAt("asFileTrees").get(0).getDir())
                }
            }
        }
        allFiles.add(new File('build/intermediates/classes/release'))
        allFiles // To return the result inside a lambda
    }
    archiveName( project.ext.appName + '.jar' )
}

This does NOT manage the build types / flavours but can be adapted (it's just ok to build on build-type release without flavour).

If ever you've a smarter or more elegant solution, please share!

참고URL : https://stackoverflow.com/questions/19307341/android-library-gradle-release-jar

반응형