Coder Social home page Coder Social logo

devinmorgan / shadow-plugin-for-aar Goto Github PK

View Code? Open in Web Editor NEW
11.0 2.0 0.0 110 KB

This represents an example Gradle Plugin that uses the Shadow Plugin to relocate the JAR within an AAR to an arbitrary package namepsace to avoid collisions with library consumers

Kotlin 100.00%

shadow-plugin-for-aar's Introduction

Motivation

The Gradle Shadow Plugin is a very useful plugin for allowing Java library authors to relocate packages from their original namespace to a new package namespace to avoid collisions between a library’s dependencies and a host application’s dependencies. Android library authors face the same concerns as Java library authors. However, the Gradle Shadow Plugin does not work out of the box for Android Artifacts (AARs) - the artifact format of Android Libraries - as the Gradle Shadow Plugin is only designed to work with Java Artifacts (JARs) out of the box. This repo defines an example Gradle Plugin called the ShadowAarDependenciesPlugin that provides Android library authors with an easy way for declaring Android Artifact dependencies in need of namespace relocations.

Getting started

Step 1

In your app/build.gradle.kts, make sure to import the ShadowAarDependenciesPlugin plugin and the ShadowAarDependenciesPluginExtension extension.

import com.example.ShadowAarDependenciesPlugin
import com.example.ShadowAarDependenciesPluginExtension

Step 2

You’ll then need apply the ShadowAarDependenciesPlugin plugin

apply<ShadowAarDependenciesPlugin>()

Step 3

Next, declare all of the package namespace relocations you are looking to perform in the extension

extensions.getByType(ShadowAarDependenciesPluginExtension::class.java).apply {
    relocations.set(mutableListOf(
        listOf("com.original.namespaceA", "com.new.namespaceA"),
        listOf("com.original.namespaceB", "com.new.namespaceB"),
    ))
}

Step 4

Finally, declare all of the Android Artifact dependencies that require namespace relocation in the aarImplementation configuration. The aarImplementation configuration will put the relocated packages on both the compile time classpath and the runtime classpath.

dependencies {
    add("aarImplementation", "com.datadoghq:dd-sdk-android-logs:2.2.0")
}

How does it work?

As we’ve already established, The Gradle Shadow Plugin operates out of the box on Java Artifacts (JARs) and is not capable of operating on Android Artifacts (AARs). However, Android Artifacts are typically just zip files that contain a classes.jar, which is the library’s compiled JVM code. This plugin bridges the gap by creating a special Gradle configuration for declaring AAR dependencies (there’s no need to do this for JAR libraries as you can just use the Gradle Shadow Plugin’s ShadowJar task directly), fetches the AAR artifacts from the remote repository, unzips the AARs, extracts the classes.jar, passes these extracted JARs to a ShadowJar task to perform namespace relocation, and finally puts these relocated JARs back into the standard implementation configuration.

graph TB
    A[Start: Declare AAR Dependencies in Special Configuration] --> B[Fetch AAR Artifacts]
    B --> C[Unzip AARs]
    C --> D[Extract classes.jar]
    D --> E[Pass JARs to ShadowJar Task]
    E --> F[Perform Namespace Relocation]
    F --> G[Put Relocated JARs into implementation Configuration]
    J[Declare JAR Dependencies] -.-> E

    style A fill:#f9d6d2
    style B fill:#f9e5d2
    style C fill:#f9f3d2
    style D fill:#eaf9d2
    style E fill:#d2f9d7
Loading

shadow-plugin-for-aar's People

Contributors

devinmorgan avatar

Stargazers

 avatar Dennis Ryazantsev avatar Alexey avatar  avatar 陈 轲 avatar  avatar Artem Baranov avatar  avatar minu avatar Christopher-Marcel Esser avatar  avatar

Watchers

 avatar minu avatar

shadow-plugin-for-aar's Issues

Issue with duplicate .properties file

Having some issue using this solution on a library that contain .properties file. I have the impression that using mergeServiceFiles() would solve the issue.

Plugin is worked?

I tried to use your plugin, but I had problems losing annotations, duplicating classes using the example of the Room library

Example

Duplicate class ru.analytics.test.room.TypeConverters found in modules jetified-analytics (analytics.jar) and jetified-app (app.jar) Duplicate class ru.analytics.test.room.Update found in modules jetified-analytics (analytics.jar) and jetified-app (app.jar) Duplicate class ru.analytics.test.room.Upsert found in modules jetified-analytics (analytics.jar) and jetified-app (app.jar) Duplicate class ru.analytics.test.room.migration.AutoMigrationSpec found in modules jetified-analytics (analytics.jar) and jetified-app (app.jar) Duplicate class ru.analytics.test.room.migration.Migration found in modules jetified-analytics (analytics.jar) and jetified-app (app.jar) Duplicate class ru.analytics.test.room.migration.MigrationImpl found in modules jetified-analytics (analytics.jar) and jetified-app (app.jar) Duplicate class ru.analytics.test.room.migration.MigrationKt found in modules jetified-analytics (analytics.jar) and jetified-app (app.jar) Duplicate class ru.analytics.test.room.paging.LimitOffsetDataSource found in modules jetified-analytics (analytics.jar) and jetified-app (app.jar) Duplicate class ru.analytics.test.room.paging.LimitOffsetDataSource$1 found in modules jetified-analytics (analytics.jar) and jetified-app (app.jar) Duplicate class ru.analytics.test.room.util.CursorUtil found in modules jetified-analytics (analytics.jar) and jetified-app (app.jar)

Annotations also disappear in generated files.
C:\Project\analytics-sdk\analytics\build\tmp\kapt3\stubs\googleDebug\ru\analytics\sdk\session\data\dao\SessionWithRemovedEventDao.java:23: error: incompatible types: NonExistentClass cannot be converted to Annotation @error.NonExistentClass ^
For method
@org.jetbrains.annotations.Nullable @error.NonExistentClass public abstract java.lang.Object insert(@org.jetbrains.annotations.NotNull ru.analytics.sdk.session.data.entities.SessionWithRemovedEventEntity session, @org.jetbrains.annotations.NotNull kotlin.coroutines.Continuation<? super kotlin.Unit> continuation);

how I use it

`shadowAarPlugin {
relocations.value(
        [["androidx", "ru.analytics.test"]]
)

}
`

It seems to me that your solution is not working

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.