diff --git a/java/dagger/hilt/android/processor/BUILD b/java/dagger/hilt/android/processor/BUILD index f86e1e16d94..40e43b714c1 100644 --- a/java/dagger/hilt/android/processor/BUILD +++ b/java/dagger/hilt/android/processor/BUILD @@ -97,8 +97,10 @@ gen_maven_artifact( javadoc_srcs = [ "//java/dagger/hilt:hilt_processing_filegroup", ], + # The shaded deps are added using jarjar, but they won't be shaded until later + # due to: https://github.com/google/dagger/issues/2765. For the shaded rules see + # util/deploy-hilt.sh shaded_deps = ["@com_google_auto_auto_common//jar"], - shaded_rules = ["rule com.google.auto.common.** dagger.hilt.android.shaded.auto.common.@1"], ) filegroup( diff --git a/java/dagger/hilt/processor/BUILD b/java/dagger/hilt/processor/BUILD index 9518a37b4a3..61fa934671a 100644 --- a/java/dagger/hilt/processor/BUILD +++ b/java/dagger/hilt/processor/BUILD @@ -119,8 +119,10 @@ gen_maven_artifact( javadoc_srcs = [ "//java/dagger/hilt:hilt_processing_filegroup", ], + # The shaded deps are added using jarjar, but they won't be shaded until later + # due to: https://github.com/google/dagger/issues/2765. For the shaded rules see + # util/deploy-hilt.sh shaded_deps = ["@com_google_auto_auto_common//jar"], - shaded_rules = ["rule com.google.auto.common.** dagger.hilt.android.shaded.auto.common.@1"], ) filegroup( diff --git a/java/dagger/internal/codegen/BUILD b/java/dagger/internal/codegen/BUILD index c29bb230794..1ff445723ef 100644 --- a/java/dagger/internal/codegen/BUILD +++ b/java/dagger/internal/codegen/BUILD @@ -113,14 +113,13 @@ gen_maven_artifact( # The javadocs should only include ComponentProcessor.java, since that is the only class used # externally. Specifically, ComponentProcessor.forTesting() is required for testing SPI plugins. javadoc_srcs = ["ComponentProcessor.java"], + # The shaded deps are added using jarjar, but they won't be shaded until later + # due to: https://github.com/google/dagger/issues/2765. For the shaded rules see + # util/deploy-dagger.sh shaded_deps = [ "@com_google_auto_auto_common//jar", "//java/dagger/internal/codegen/xprocessing", ], - shaded_rules = [ - "rule androidx.room.compiler.processing.** dagger.shaded.androidx.room.compiler.processing.@1", - "rule com.google.auto.common.** dagger.shaded.auto.common.@1", - ], ) java_plugin( diff --git a/java/dagger/spi/BUILD b/java/dagger/spi/BUILD index 1f383b6e827..401d0c1da29 100644 --- a/java/dagger/spi/BUILD +++ b/java/dagger/spi/BUILD @@ -77,6 +77,8 @@ gen_maven_artifact( "dagger.spi", ], javadoc_srcs = [":spi-srcs"], + # The shaded deps are added using jarjar, but they won't be shaded until later + # due to: https://github.com/google/dagger/issues/2765. For the shaded rules see + # util/deploy-dagger.sh shaded_deps = ["@com_google_auto_auto_common//jar"], - shaded_rules = ["rule com.google.auto.common.** dagger.spi.shaded.auto.common.@1"], ) diff --git a/javatests/artifacts/dagger/simple/src/main/java/dagger/simple/SimpleApplication.java b/javatests/artifacts/dagger/simple/src/main/java/dagger/simple/SimpleApplication.java index f13610101d0..e74815b188b 100644 --- a/javatests/artifacts/dagger/simple/src/main/java/dagger/simple/SimpleApplication.java +++ b/javatests/artifacts/dagger/simple/src/main/java/dagger/simple/SimpleApplication.java @@ -44,5 +44,8 @@ interface SimpleComponent { public static void main(String[] args) { Foo foo = DaggerSimpleApplication_SimpleComponent.create().foo(); + + // Execute other classes + AssistedInjects.main(args); } } diff --git a/javatests/artifacts/dagger/simpleKotlin/build.gradle b/javatests/artifacts/dagger/simpleKotlin/build.gradle new file mode 100644 index 00000000000..8c863abf660 --- /dev/null +++ b/javatests/artifacts/dagger/simpleKotlin/build.gradle @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2020 The Dagger Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +buildscript { + ext.kotlin_version = "1.5.0" +} + +plugins { + id 'application' + id 'org.jetbrains.kotlin.jvm' version "$kotlin_version" + id 'org.jetbrains.kotlin.kapt' version "$kotlin_version" +} + +repositories { + google() + mavenCentral() + mavenLocal() +} + +java { + // Make sure the generated source is compatible with Java 7. + sourceCompatibility = JavaVersion.VERSION_1_8 +} + +dependencies { + implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" + implementation 'com.google.dagger:dagger:LOCAL-SNAPSHOT' + kapt 'com.google.dagger:dagger-compiler:LOCAL-SNAPSHOT' + + // This is testImplementation rather than kaptTest because we're actually + // testing the reference to ComponentProcessor. + // See https://github.com/google/dagger/issues/2765 + testImplementation 'com.google.dagger:dagger-compiler:LOCAL-SNAPSHOT' + testImplementation 'com.google.truth:truth:1.0.1' + testImplementation 'junit:junit:4.13' +} + +application { + mainClass = 'dagger.simple.SimpleApplicationKt' +} \ No newline at end of file diff --git a/javatests/artifacts/dagger/simpleKotlin/gradle/wrapper/gradle-wrapper.jar b/javatests/artifacts/dagger/simpleKotlin/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000000..5c2d1cf016b Binary files /dev/null and b/javatests/artifacts/dagger/simpleKotlin/gradle/wrapper/gradle-wrapper.jar differ diff --git a/javatests/artifacts/dagger/simpleKotlin/gradle/wrapper/gradle-wrapper.properties b/javatests/artifacts/dagger/simpleKotlin/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000000..0f80bbf516c --- /dev/null +++ b/javatests/artifacts/dagger/simpleKotlin/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/javatests/artifacts/dagger/simpleKotlin/gradlew b/javatests/artifacts/dagger/simpleKotlin/gradlew new file mode 100755 index 00000000000..b0d6d0ab5de --- /dev/null +++ b/javatests/artifacts/dagger/simpleKotlin/gradlew @@ -0,0 +1,188 @@ +#!/usr/bin/env sh + +# +# Copyright 2015 the original author or authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/javatests/artifacts/dagger/simpleKotlin/src/main/java/dagger/simpleKotlin/AssistedInjects.kt b/javatests/artifacts/dagger/simpleKotlin/src/main/java/dagger/simpleKotlin/AssistedInjects.kt new file mode 100644 index 00000000000..284ef4dce54 --- /dev/null +++ b/javatests/artifacts/dagger/simpleKotlin/src/main/java/dagger/simpleKotlin/AssistedInjects.kt @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2021 The Dagger Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dagger.simpleKotlin + +import dagger.Component +import dagger.assisted.Assisted +import dagger.assisted.AssistedFactory +import dagger.assisted.AssistedInject +import javax.inject.Inject + +// This is a regression test for https://github.com/google/dagger/issues/2309 +/** A simple, skeletal application that defines an assisted inject binding. */ +class AssistedInjects { + @Component + interface MyComponent { + fun fooFactory(): FooFactory + + fun parameterizedFooFactory(): ParameterizedFooFactory + } + + class Bar @Inject constructor() + + class Foo @AssistedInject constructor(val bar: Bar, @Assisted val str: String) + + @AssistedFactory + interface FooFactory { + fun create(str: String): Foo + } + + class ParameterizedFoo @AssistedInject constructor(val t1: T1, @Assisted val t2: T2) + + @AssistedFactory + interface ParameterizedFooFactory { + fun create(t2: T2): ParameterizedFoo + } + + companion object { + // Called from SimpleApplication.main() + fun main() { + val foo: Foo = DaggerAssistedInjects_MyComponent.create().fooFactory().create("") + + val parameterizedFoo: ParameterizedFoo = + DaggerAssistedInjects_MyComponent.create().parameterizedFooFactory().create("") + } + } +} diff --git a/javatests/artifacts/dagger/simpleKotlin/src/main/java/dagger/simpleKotlin/SimpleApplication.kt b/javatests/artifacts/dagger/simpleKotlin/src/main/java/dagger/simpleKotlin/SimpleApplication.kt new file mode 100644 index 00000000000..fb211083a4e --- /dev/null +++ b/javatests/artifacts/dagger/simpleKotlin/src/main/java/dagger/simpleKotlin/SimpleApplication.kt @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2020 The Dagger Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dagger.simpleKotlin + +import dagger.Component +import dagger.Module +import dagger.Provides +import javax.inject.Inject +import javax.inject.Singleton + +/** A simple, skeletal application that defines a simple component. */ +class SimpleApplication { + class Foo @Inject constructor() + + @Module + object SimpleModule { + @Provides + fun provideFoo(): Foo { + return Foo() + } + } + + @Singleton + @Component(modules = [SimpleModule::class]) + interface SimpleComponent { + fun foo(): Foo + } + + companion object { + fun main() { + val foo: Foo = DaggerSimpleApplication_SimpleComponent.create().foo() + } + } +} + +fun main() { + SimpleApplication.main() + AssistedInjects.main() +} diff --git a/javatests/artifacts/dagger/simpleKotlin/src/test/java/dagger/simpleKotlin/ComponentProcessorBuildTest.kt b/javatests/artifacts/dagger/simpleKotlin/src/test/java/dagger/simpleKotlin/ComponentProcessorBuildTest.kt new file mode 100644 index 00000000000..26f1767e965 --- /dev/null +++ b/javatests/artifacts/dagger/simpleKotlin/src/test/java/dagger/simpleKotlin/ComponentProcessorBuildTest.kt @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2021 The Dagger Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dagger.simpleKotlin + +import com.google.common.truth.Truth.assertThat +import dagger.internal.codegen.ComponentProcessor +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 + +@RunWith(JUnit4::class) +class ComponentProcessorBuildTest { + + // This is a regression test for https://github.com/google/dagger/issues/2765 + // to make sure ComponentProcessor builds in kotlin. + @Test + fun testComponentProcessor() { + val processor = ComponentProcessor.forTesting() + + assertThat(processor).isNotNull() + } +} diff --git a/tools/maven.bzl b/tools/maven.bzl index 2c648a218ac..f842c88acef 100644 --- a/tools/maven.bzl +++ b/tools/maven.bzl @@ -74,7 +74,6 @@ def gen_maven_artifact( javadoc_exclude_packages, javadoc_android_api_level, shaded_deps, - shaded_rules, manifest, lint_deps, proguard_specs @@ -96,7 +95,6 @@ def _gen_maven_artifact( javadoc_exclude_packages, javadoc_android_api_level, shaded_deps, - shaded_rules, manifest, lint_deps, proguard_specs): @@ -131,7 +129,6 @@ def _gen_maven_artifact( javadoc_exclude_packages: The packages to exclude from the javadocs. javadoc_android_api_level: The android api level for the javadocs. shaded_deps: The shaded deps for the jarjar. - shaded_rules: The shaded rules for the jarjar. manifest: The AndroidManifest.xml to bundle in when packaing an 'aar'. lint_deps: The lint targets to be bundled in when packaging an 'aar'. proguard_specs: The proguard spec files to be bundled in when packaging an 'aar' @@ -148,7 +145,6 @@ def _gen_maven_artifact( ) shaded_deps = shaded_deps or [] - shaded_rules = shaded_rules or [] artifact_targets = [artifact_target] + (artifact_target_libs or []) lint_deps = lint_deps or [] @@ -172,7 +168,6 @@ def _gen_maven_artifact( name = name + "-classes", testonly = testonly, jars = artifact_targets + shaded_deps, - rules = shaded_rules, merge_meta_inf_files = merge_meta_inf_files, ) if lint_deps: @@ -217,7 +212,6 @@ def _gen_maven_artifact( name = name, testonly = testonly, jars = artifact_targets + shaded_deps, - rules = shaded_rules, merge_meta_inf_files = merge_meta_inf_files, ) diff --git a/tools/shader/build.gradle b/tools/shader/build.gradle new file mode 100644 index 00000000000..110c0deafa3 --- /dev/null +++ b/tools/shader/build.gradle @@ -0,0 +1,99 @@ +/* + * Copyright (C) 2020 The Dagger Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.util.jar.Manifest; +import com.github.jengelman.gradle.plugins.shadow.transformers.Transformer +import com.github.jengelman.gradle.plugins.shadow.transformers.TransformerContext +import shadow.org.apache.tools.zip.ZipOutputStream +import shadow.org.apache.tools.zip.ZipEntry + +plugins { + id 'java-library' + id 'com.github.johnrengelman.shadow' version '6.1.0' +} + +repositories { + google() + mavenCentral() + mavenLocal() +} + +java { + sourceCompatibility = JavaVersion.VERSION_1_8 +} + +// This transformation makes sure the input jar's MANIFEST.MF properties, +// such as "Created-by:" get merged into the final artifact jar (although it's +// not clear how necessary this is). +/** A transform that merges in the MANIFEST.MF files from the inputJar. */ +class ManifestMerger implements Transformer { + private static final String MANIFEST_MF = "META-INF/MANIFEST.MF"; + + private Manifest manifest; + + @Override + boolean canTransformResource(FileTreeElement element) { + return MANIFEST_MF.equalsIgnoreCase(element.relativePath.pathString); + } + + @Override + void transform(TransformerContext context) { + if (manifest == null) { + manifest = new Manifest(context.is); + } else { + Manifest toMerge = new Manifest(context.is); + manifest.getMainAttributes() + .putAll(toMerge.getMainAttributes().entrySet()); + manifest.getEntries().putAll(toMerge.getEntries().entrySet()); + } + } + + @Override + boolean hasTransformedResource() { true } + + @Override + void modifyOutputStream( + ZipOutputStream os, boolean preserveFileTimestamps) { + os.putNextEntry(new ZipEntry(MANIFEST_MF)); + if (manifest != null) { + ByteArrayOutputStream content = new ByteArrayOutputStream(); + manifest.write(content); + os.write(content.toByteArray()); + } + } +} + +configurations { + shaded +} + +shadowJar { + archiveClassifier = "" // postfix for output jar + configurations = [project.configurations.shaded] + transform(ManifestMerger.class) + + // Add a 'relocate' declaration for each shaded rule. + // Format: "key1,value1;key2,value2;key3,value3" + def rules = project.getProperty("shadedRules").split(";") + for (def i = 0; i < rules.size(); i++) { + def rule = rules[i].split(",") + relocate "${rule[0]}", "${rule[1]}" + } +} + +dependencies { + shaded files(project.getProperty("inputJar")) +} diff --git a/tools/shader/gradle/wrapper/gradle-wrapper.jar b/tools/shader/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000000..5c2d1cf016b Binary files /dev/null and b/tools/shader/gradle/wrapper/gradle-wrapper.jar differ diff --git a/tools/shader/gradle/wrapper/gradle-wrapper.properties b/tools/shader/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000000..0f80bbf516c --- /dev/null +++ b/tools/shader/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/tools/shader/gradlew b/tools/shader/gradlew new file mode 100755 index 00000000000..b0d6d0ab5de --- /dev/null +++ b/tools/shader/gradlew @@ -0,0 +1,188 @@ +#!/usr/bin/env sh + +# +# Copyright 2015 the original author or authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/util/deploy-dagger.sh b/util/deploy-dagger.sh index 164c2c24ef2..16400f30c4e 100755 --- a/util/deploy-dagger.sh +++ b/util/deploy-dagger.sh @@ -17,12 +17,14 @@ readonly EXTRA_MAVEN_ARGS=("$@") # @param {string} module_name the JPMS module name to include in the jar. This # is an optional parameter and can only be used with jar files. _deploy() { - local library=$1 - local pomfile=$2 - local srcjar=$3 - local javadoc=$4 - local module_name=$5 + local shaded_rules=$1 + local library=$2 + local pomfile=$3 + local srcjar=$4 + local javadoc=$5 + local module_name=$6 bash $(dirname $0)/deploy-library.sh \ + "$shaded_rules" \ "$library" \ "$pomfile" \ "$srcjar" \ @@ -34,6 +36,7 @@ _deploy() { } _deploy \ + "" \ java/dagger/libcore.jar \ java/dagger/pom.xml \ java/dagger/libcore-src.jar \ @@ -41,6 +44,7 @@ _deploy \ "dagger" _deploy \ + "" \ gwt/libgwt.jar \ gwt/pom.xml \ gwt/libgwt.jar \ @@ -48,6 +52,7 @@ _deploy \ "" _deploy \ + "com.google.auto.common,dagger.shaded.auto.common;androidx.room.compiler.processing,dagger.shaded.androidx.room.compiler.processing" \ java/dagger/internal/codegen/artifact.jar \ java/dagger/internal/codegen/pom.xml \ java/dagger/internal/codegen/artifact-src.jar \ @@ -55,6 +60,7 @@ _deploy \ "" _deploy \ + "" \ java/dagger/producers/artifact.jar \ java/dagger/producers/pom.xml \ java/dagger/producers/artifact-src.jar \ @@ -62,6 +68,7 @@ _deploy \ "" _deploy \ + "com.google.auto.common,dagger.spi.shaded.auto.common" \ java/dagger/spi/artifact.jar \ java/dagger/spi/pom.xml \ java/dagger/spi/artifact-src.jar \ @@ -69,6 +76,7 @@ _deploy \ "" _deploy \ + "" \ java/dagger/android/android.aar \ java/dagger/android/pom.xml \ java/dagger/android/libandroid-src.jar \ @@ -76,6 +84,7 @@ _deploy \ "" _deploy \ + "" \ java/dagger/android/android-legacy.aar \ java/dagger/android/legacy-pom.xml \ "" \ @@ -83,6 +92,7 @@ _deploy \ "" _deploy \ + "" \ java/dagger/android/support/support.aar \ java/dagger/android/support/pom.xml \ java/dagger/android/support/libsupport-src.jar \ @@ -90,6 +100,7 @@ _deploy \ "" _deploy \ + "" \ java/dagger/android/support/support-legacy.aar \ java/dagger/android/support/legacy-pom.xml \ "" \ @@ -97,6 +108,7 @@ _deploy \ "" _deploy \ + "" \ shaded_android_processor.jar \ java/dagger/android/processor/pom.xml \ java/dagger/android/processor/libprocessor-src.jar \ @@ -104,6 +116,7 @@ _deploy \ "" _deploy \ + "" \ java/dagger/grpc/server/libserver.jar \ java/dagger/grpc/server/server-pom.xml \ java/dagger/grpc/server/libserver-src.jar \ @@ -111,6 +124,7 @@ _deploy \ "" _deploy \ + "" \ java/dagger/grpc/server/libannotations.jar \ java/dagger/grpc/server/annotations-pom.xml \ java/dagger/grpc/server/libannotations-src.jar \ @@ -118,6 +132,7 @@ _deploy \ "" _deploy \ + "" \ shaded_grpc_server_processor.jar \ java/dagger/grpc/server/processor/pom.xml \ java/dagger/grpc/server/processor/libprocessor-src.jar \ @@ -125,6 +140,7 @@ _deploy \ "" _deploy \ + "" \ java/dagger/lint/lint-artifact.jar \ java/dagger/lint/lint-pom.xml \ java/dagger/lint/lint-artifact-src.jar \ @@ -132,6 +148,7 @@ _deploy \ "" _deploy \ + "" \ java/dagger/lint/lint-android-artifact.aar \ java/dagger/lint/lint-android-pom.xml \ "" \ diff --git a/util/deploy-hilt.sh b/util/deploy-hilt.sh index b295a7a9249..325ef13f239 100755 --- a/util/deploy-hilt.sh +++ b/util/deploy-hilt.sh @@ -17,12 +17,14 @@ readonly EXTRA_MAVEN_ARGS=("$@") # @param {string} module_name the JPMS module name to include in the jar. This # is an optional parameter and can only be used with jar files. _deploy() { - local library=$1 - local pomfile=$2 - local srcjar=$3 - local javadoc=$4 - local module_name=$5 + local shaded_rules=$1 + local library=$2 + local pomfile=$3 + local srcjar=$4 + local javadoc=$5 + local module_name=$6 bash $(dirname $0)/deploy-library.sh \ + "$shaded_rules" \ "$library" \ "$pomfile" \ "$srcjar" \ @@ -34,6 +36,7 @@ _deploy() { } _deploy \ + "" \ java/dagger/hilt/android/artifact.aar \ java/dagger/hilt/android/pom.xml \ java/dagger/hilt/android/artifact-src.jar \ @@ -41,6 +44,7 @@ _deploy \ "" _deploy \ + "" \ java/dagger/hilt/android/testing/artifact.aar \ java/dagger/hilt/android/testing/pom.xml \ java/dagger/hilt/android/testing/artifact-src.jar \ @@ -48,6 +52,7 @@ _deploy \ "" _deploy \ + "com.google.auto.common,dagger.hilt.android.shaded.auto.common" \ java/dagger/hilt/processor/artifact.jar \ java/dagger/hilt/processor/pom.xml \ java/dagger/hilt/processor/artifact-src.jar \ @@ -55,6 +60,7 @@ _deploy \ "" _deploy \ + "com.google.auto.common,dagger.hilt.android.shaded.auto.common" \ java/dagger/hilt/android/processor/artifact.jar \ java/dagger/hilt/android/processor/pom.xml \ java/dagger/hilt/android/processor/artifact-src.jar \ @@ -62,6 +68,7 @@ _deploy \ "" _deploy \ + "" \ java/dagger/hilt/artifact-core.jar \ java/dagger/hilt/pom.xml \ java/dagger/hilt/artifact-core-src.jar \ diff --git a/util/deploy-library.sh b/util/deploy-library.sh index 719a47f7568..02f7c9ef155 100755 --- a/util/deploy-library.sh +++ b/util/deploy-library.sh @@ -12,19 +12,28 @@ set -eu # @param {string} module_name the JPMS module name to include in the jar. This # is an optional parameter and can only be used with jar files. deploy_library() { - local library=$1 - local pomfile=$2 - local srcjar=$3 - local javadoc=$4 - local module_name=$5 - local mvn_goal=$6 - local version_name=$7 - shift 7 + local shaded_rules=$1 + local library=$2 + local pomfile=$3 + local srcjar=$4 + local javadoc=$5 + local module_name=$6 + local mvn_goal=$7 + local version_name=$8 + shift 8 local extra_maven_args=("$@") bazel build --define=pom_version="$version_name" \ $library $pomfile + # Shade the library if shaded_rules exist + if [[ ! -z "$shaded_rules" ]]; then + bash $(dirname $0)/shade-library.sh \ + $(bazel_output_file $library) $shaded_rules + # The output jar name is the same as the input library appended with -shaded + library="${library%.*}-shaded.${library##*.}" + fi + # TODO(bcorso): Consider moving this into the "gen_maven_artifact" macro, this # requires having the version checked-in for the build system. add_tracking_version \ diff --git a/util/run-local-gradle-tests.sh b/util/run-local-gradle-tests.sh index d2525fc8d93..af5735e1a01 100755 --- a/util/run-local-gradle-tests.sh +++ b/util/run-local-gradle-tests.sh @@ -5,6 +5,7 @@ set -ex readonly GRADLE_PROJECTS=( "java/dagger/hilt/android/plugin" "javatests/artifacts/dagger/simple" + "javatests/artifacts/dagger/simpleKotlin" ) for project in "${GRADLE_PROJECTS[@]}"; do echo "Running gradle tests for $project" diff --git a/util/shade-library.sh b/util/shade-library.sh new file mode 100644 index 00000000000..73767d63a2f --- /dev/null +++ b/util/shade-library.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +set -eux + +readonly INPUT_JAR=$1 +readonly SHADE_RULES=$2 + +_shade_libary() { + local shader=$(dirname $0)/../tools/shader + local output="${INPUT_JAR%.*}-shaded.${INPUT_JAR##*.}" + + ./$shader/gradlew -p $shader shadowJar \ + -PinputJar="../../$INPUT_JAR" \ + -PshadedRules=$SHADE_RULES + + # Copy the shaded jar to the specified output + cp $shader/build/libs/shader.jar $output +} + +_shade_libary