johnrengelman / shadow

Gradle plugin to create fat/uber JARs, apply file transforms, and relocate packages for applications and libraries. Gradle version of Maven's Shade plugin.
http://imperceptiblethoughts.com/shadow/
Apache License 2.0
3.63k stars 389 forks source link

Relocating not working - Android project #498

Open molchawa opened 5 years ago

molchawa commented 5 years ago

Hi, Project setup: Gradle version: 5.1.1 Shadow version: 5.0.0

I would like to use shadow to relocate packages but I always get the same error:

 Could not get unknown property 'runtime' for configuration container of type org.gradle.api.internal.artifacts.configurations.DefaultConfigurationContainer.

This is my build.gradle file:

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'org.jetbrains.dokka'
apply plugin: 'com.github.johnrengelman.shadow'

project.group = 'com.test'
project.version = '4.0.0'

def shadowPackage = 'com.test.shadow'
def shadowJarBaseName = project.name + '-' + project.version
def miniJarBaseName = shadowJarBaseName + '-mini'

configurations {
    shadow // to be included in fat aar with changed package name
    transitive // to be included in pom.xml as a runtime dependency

    implementation.extendsFrom shadow
    api.extendsFrom shadow // to propagate shadowed dependencies to submodules
    api.extendsFrom transitive
}

android {
    compileSdkVersion 28

    defaultConfig {
        minSdkVersion 17
        targetSdkVersion 28
        versionCode 100
        versionName project.version
        testInstrumentationRunner "androidx.singleThread.runner.AndroidJUnitRunner"

        javaCompileOptions {
            annotationProcessorOptions {
                arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            manifestPlaceholders = [shadowPrefix: "$shadowPackage."]
        }
        debug {
            debuggable true
            manifestPlaceholders = [shadowPrefix: ""]
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    dataBinding {
        enabled = true
    }
}

buildscript {
    repositories {
        google()
        jcenter()
        maven { url "https://plugins.gradle.org/m2/" }
    }

    dependencies {
        classpath "com.android.tools.build:gradle:$buildToolsVersion"
        classpath "com.github.jengelman.gradle.plugins:shadow:$shadowPluginVersion"
    }
}

task javadocBuild(type: GradleBuild) {
    dir = '../test_sdk'
    tasks = ['javadocJar']
}

import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import proguard.gradle.ProGuardTask

// assembleRelease is created dynamically, hence the need to defer below tasks
afterEvaluate {
    def aarDir = "$buildDir/extracted-aars"

    task extractAars {
        group 'other'

        doLast {
            configurations.shadow.filter { it.name.endsWith('.aar') }.collect { aar ->
                copy {
                    from zipTree(aar)
                    into "$aarDir/${aar.name.replace('.aar', '')}"
                }
            }
        }
    }

    task shadowJar(type: ShadowJar) {
        dependsOn assembleRelease, extractAars
        group 'other'

        configurations = [project.configurations.shadow]

        exclude '*.aar'
        exclude '*.xml'
        exclude 'META-INF/maven/**'
        exclude 'META-INF/services/**'
        exclude 'META-INF/*.kotlin_module'
        exclude '**/*.kotlin_builtins'
        exclude 'kotlin/**'
        exclude 'kotlinx/**'
        exclude 'androidx/**'
        exclude 'android/**'
        exclude 'org/intellij/**'

        relocate 'arrow', "${shadowPackage}.arrow"
        relocate 'retrofit2', "${shadowPackage}.retrofit2"
        relocate 'okhttp3', "${shadowPackage}.okhttp3"
        relocate 'timber', "${shadowPackage}.timber"
        relocate 'com.github', "${shadowPackage}.com.github"
        relocate 'com.google.gson', "${shadowPackage}.com.google.gson"
        relocate 'org.altbeacon', "${shadowPackage}.org.altbeacon"

        from fileTree(aarDir).filter { it.name == 'classes.jar' }
        from zipTree("$buildDir/outputs/aar/$archivesBaseName-release.aar").filter { it.name == 'classes.jar' }
    }
}

So can someone help me to resolve this problem?

YunanChen commented 3 years ago

@molchawa Hi, have you resloved this problem?

ghost commented 2 years ago

@molchawa any updates? have you resolved this?

devinmorgan commented 9 months ago

Hey there! It seems out of the box that the ShadowJar task only operates on Java Artifacts (JARs). I wanted to link to this repo, which serves as an example Gradle Plugin that allows you to use the ShadowJar task for Android libraries, which are Android Artifacts (AARs). Hopefully this helps!