diff --git a/byte-buddy-gradle-plugin/android-plugin-test/.gitignore b/byte-buddy-gradle-plugin/android-plugin-test/.gitignore new file mode 100644 index 00000000000..55b147bea55 --- /dev/null +++ b/byte-buddy-gradle-plugin/android-plugin-test/.gitignore @@ -0,0 +1,5 @@ +build/ +.gradle/ +.idea/ +local.properties +captures/ \ No newline at end of file diff --git a/byte-buddy-gradle-plugin/android-plugin-test/README.md b/byte-buddy-gradle-plugin/android-plugin-test/README.md index 08ad87e9168..6e73f42d2a4 100644 --- a/byte-buddy-gradle-plugin/android-plugin-test/README.md +++ b/byte-buddy-gradle-plugin/android-plugin-test/README.md @@ -4,22 +4,17 @@ How to run the tests ### Environment setup * The environment var `ANDROID_HOME` must be set and pointing to the Android SDK dir. -* Java 11 is required, more info [here](https://developer.android.com/studio/releases/gradle-plugin#jdk-11). -* A Gradle wrapper version of 7.2+ is required in order to be compatible with the Android - Gradle plugin used in this project, more info on it - [here](https://developer.android.com/studio/releases/gradle-plugin#updating-gradle). -* There must be an Android device connected through [ADB](https://developer.android.com/studio/command-line/adb), either +* Java 11 is required, more + info [here](https://developer.android.com/studio/releases/gradle-plugin#jdk-11). +* There must be an Android device connected + through [ADB](https://developer.android.com/studio/command-line/adb), either a running Emulator or a real device plugged in with [USB debugging](https://developer.android.com/studio/command-line/adb#Enabling) enabled. -* Set environment var `ANDROID_TEST` to `"true"`. - -This module is disabled by default to avoid issues with the Android Gradle Plugin and Intellij IDEA -version incompatibility, which causes the project analysis to fail when opened with said IDE, though it doesn't -cause any issues when running it from the command line. That's why the environment var `ANDROID_TEST` is needed -in order to switch this module on by setting it to `"true"`. ### Steps * Build Byte Buddy alongside the Byte Buddy Gradle plugin. -* Run the following command from the Byte Buddy Gradle plugin +* Located in the root dir of this test project, run the following command: dir: `./gradlew connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.class=net.bytebuddy.android.test.ByteBuddyInstrumentedTest` +* Optionally, you can also open up this test project with Android Studio, open up the test class and + run it through the IDE. \ No newline at end of file diff --git a/byte-buddy-gradle-plugin/android-plugin-test/aar-bytebuddy-plugin/src/main/java/net/bytebuddy/plugin/gradle/test/aar/AarAdvice.java b/byte-buddy-gradle-plugin/android-plugin-test/aar-bytebuddy-plugin/src/main/java/net/bytebuddy/plugin/gradle/test/aar/AarAdvice.java index dfd86bebf77..5bcad3b1d6f 100644 --- a/byte-buddy-gradle-plugin/android-plugin-test/aar-bytebuddy-plugin/src/main/java/net/bytebuddy/plugin/gradle/test/aar/AarAdvice.java +++ b/byte-buddy-gradle-plugin/android-plugin-test/aar-bytebuddy-plugin/src/main/java/net/bytebuddy/plugin/gradle/test/aar/AarAdvice.java @@ -29,6 +29,6 @@ public class AarAdvice { */ @Advice.OnMethodExit public static void onExit(@Advice.Return(readOnly = false) String returned) { - returned = "bar"; + returned = "instrumented local class from aar plugin"; } } diff --git a/byte-buddy-gradle-plugin/android-plugin-test/aar-bytebuddy-target/build.gradle b/byte-buddy-gradle-plugin/android-plugin-test/aar-bytebuddy-target/build.gradle index dcb7351eb66..d0ed79b9b18 100644 --- a/byte-buddy-gradle-plugin/android-plugin-test/aar-bytebuddy-target/build.gradle +++ b/byte-buddy-gradle-plugin/android-plugin-test/aar-bytebuddy-target/build.gradle @@ -25,4 +25,5 @@ android { dependencies { implementation group: 'com.google.android.material', name: 'material', version: '1.6.1' + implementation project(":service-definition-lib") } diff --git a/byte-buddy-gradle-plugin/android-plugin-test/aar-bytebuddy-target/src/main/java/net/bytebuddy/android/test/aar/lib/service/TargetServiceImplementation.java b/byte-buddy-gradle-plugin/android-plugin-test/aar-bytebuddy-target/src/main/java/net/bytebuddy/android/test/aar/lib/service/TargetServiceImplementation.java new file mode 100644 index 00000000000..9fe659434d8 --- /dev/null +++ b/byte-buddy-gradle-plugin/android-plugin-test/aar-bytebuddy-target/src/main/java/net/bytebuddy/android/test/aar/lib/service/TargetServiceImplementation.java @@ -0,0 +1,26 @@ +/* + * Copyright 2014 - Present Rafael Winterhalter + * + * 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 net.bytebuddy.android.test.aar.lib.service; + +import com.service.definition.ServiceDefinition; + +public class TargetServiceImplementation implements ServiceDefinition { + + @Override + public String getValue() { + return "Target service impl"; + } +} diff --git a/byte-buddy-gradle-plugin/android-plugin-test/aar-bytebuddy-target/src/main/resources/META-INF/services/com.service.definition.ServiceDefinition b/byte-buddy-gradle-plugin/android-plugin-test/aar-bytebuddy-target/src/main/resources/META-INF/services/com.service.definition.ServiceDefinition new file mode 100644 index 00000000000..5a52a2cb841 --- /dev/null +++ b/byte-buddy-gradle-plugin/android-plugin-test/aar-bytebuddy-target/src/main/resources/META-INF/services/com.service.definition.ServiceDefinition @@ -0,0 +1 @@ +net.bytebuddy.android.test.aar.lib.service.TargetServiceImplementation \ No newline at end of file diff --git a/byte-buddy-gradle-plugin/android-plugin-test/app/build.gradle b/byte-buddy-gradle-plugin/android-plugin-test/app/build.gradle index ac04d874b13..386b93e3101 100644 --- a/byte-buddy-gradle-plugin/android-plugin-test/app/build.gradle +++ b/byte-buddy-gradle-plugin/android-plugin-test/app/build.gradle @@ -26,14 +26,15 @@ android { } } -def outerPom = new groovy.xml.XmlSlurper().parse(rootProject.file("$rootDir/pom.xml")) - dependencies { - implementation group: 'androidx.appcompat', name: 'appcompat', version: '1.5.0' - implementation project(':android-plugin-test:aar-bytebuddy-target') - byteBuddy project(":android-plugin-test:jar-bytebuddy-plugin") - byteBuddy project(":android-plugin-test:aar-bytebuddy-plugin") + implementation project(":service-definition-lib") + implementation project(':aar-bytebuddy-target') + implementation project(":service-implementation-lib") + implementation project(":service-implementation2-lib") + byteBuddy project(":jar-bytebuddy-plugin") + byteBuddy project(":aar-bytebuddy-plugin") testImplementation group: 'junit', name: 'junit', version: outerPom.properties.'version.junit' - androidTestImplementation group: 'androidx.test.ext', name: 'junit', version: '1.1.3' - androidTestImplementation group: 'androidx.test.espresso', name: 'espresso-core', version: '3.4.0' + androidTestImplementation group: 'androidx.test', name: 'rules', version: '1.5.0' + androidTestImplementation group: 'androidx.test', name: 'runner', version: '1.5.2' + androidTestImplementation group: 'androidx.test.ext', name: 'junit', version: '1.1.5' } diff --git a/byte-buddy-gradle-plugin/android-plugin-test/app/src/androidTest/java/net/bytebuddy/android/test/ByteBuddyInstrumentedTest.java b/byte-buddy-gradle-plugin/android-plugin-test/app/src/androidTest/java/net/bytebuddy/android/test/ByteBuddyInstrumentedTest.java index 0ead9aa097a..472475656d0 100644 --- a/byte-buddy-gradle-plugin/android-plugin-test/app/src/androidTest/java/net/bytebuddy/android/test/ByteBuddyInstrumentedTest.java +++ b/byte-buddy-gradle-plugin/android-plugin-test/app/src/androidTest/java/net/bytebuddy/android/test/ByteBuddyInstrumentedTest.java @@ -1,30 +1,37 @@ package net.bytebuddy.android.test; -import static androidx.test.espresso.Espresso.onView; -import static androidx.test.espresso.assertion.ViewAssertions.matches; -import static androidx.test.espresso.matcher.ViewMatchers.withId; -import static androidx.test.espresso.matcher.ViewMatchers.withText; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; -import androidx.test.ext.junit.rules.ActivityScenarioRule; -import androidx.test.ext.junit.runners.AndroidJUnit4; +import com.service.definition.ServiceDefinition; + +import net.bytebuddy.android.test.aar.lib.SomeAarClass; -import org.junit.Rule; import org.junit.Test; -import org.junit.runner.RunWith; -@RunWith(AndroidJUnit4.class) -class ByteBuddyInstrumentedTest { +import java.util.ArrayList; +import java.util.List; +import java.util.ServiceLoader; - @Rule - public ActivityScenarioRule activityRule = new ActivityScenarioRule<>(MainActivity.class); +public class ByteBuddyInstrumentedTest { @Test public void bytecodeInstrumentation() { - // On local java - onView(withId(R.id.text_from_local_java_class)).check(matches(withText("bar"))); - // On dependency class - onView(withId(R.id.text_from_aar_dependency)).check(matches(withText("bar"))); - // From aar - onView(withId(R.id.text_instrumented_from_aar)).check(matches(withText("bar"))); + assertEquals("instrumented local class from jar plugin", new SomeClass().method()); + assertEquals("instrumented local class from aar plugin", new AnotherClass().method()); + assertEquals("instrumented aar lib's class from jar plugin", new SomeAarClass().method()); + } + + @Test + public void verifySPIsAreNotMissing() { + List values = new ArrayList<>(); + for (ServiceDefinition serviceDefinition : ServiceLoader.load(ServiceDefinition.class)) { + values.add(serviceDefinition.getValue()); + } + + assertEquals(3, values.size()); + assertTrue(values.contains("Target service impl")); + assertTrue(values.contains("Service implementation")); + assertTrue(values.contains("Service implementation2")); } } \ No newline at end of file diff --git a/byte-buddy-gradle-plugin/android-plugin-test/app/src/main/AndroidManifest.xml b/byte-buddy-gradle-plugin/android-plugin-test/app/src/main/AndroidManifest.xml index bdf383e9b47..4d2e630d7d9 100644 --- a/byte-buddy-gradle-plugin/android-plugin-test/app/src/main/AndroidManifest.xml +++ b/byte-buddy-gradle-plugin/android-plugin-test/app/src/main/AndroidManifest.xml @@ -1,21 +1,2 @@ - - - - - - - - - - - + diff --git a/byte-buddy-gradle-plugin/android-plugin-test/app/src/main/java/net/bytebuddy/android/test/MainActivity.java b/byte-buddy-gradle-plugin/android-plugin-test/app/src/main/java/net/bytebuddy/android/test/MainActivity.java deleted file mode 100644 index 48dd5315404..00000000000 --- a/byte-buddy-gradle-plugin/android-plugin-test/app/src/main/java/net/bytebuddy/android/test/MainActivity.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2014 - Present Rafael Winterhalter - * - * 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 net.bytebuddy.android.test; - -import android.os.Bundle; -import android.widget.TextView; - -import androidx.annotation.IdRes; -import androidx.appcompat.app.AppCompatActivity; - -import net.bytebuddy.android.test.aar.lib.SomeAarClass; - -/** - * The main activity to use under test. - */ -public class MainActivity extends AppCompatActivity { - - /** - * {@inheritDoc} - */ - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main); - - setTextTo(R.id.text_from_local_java_class, new SomeClass().someMethod()); - setTextTo(R.id.text_from_aar_dependency, new SomeAarClass().someMethod()); - setTextTo(R.id.text_instrumented_from_aar, new AnotherClass().someMethod()); - } - - /** - * Sets a resource text to a given value. - * - * @param id The text id. - * @param text The text to set. - */ - private void setTextTo(@IdRes int id, String text) { - TextView textView = findViewById(id); - textView.setText(text); - } -} diff --git a/byte-buddy-gradle-plugin/android-plugin-test/app/src/main/res/layout/activity_main.xml b/byte-buddy-gradle-plugin/android-plugin-test/app/src/main/res/layout/activity_main.xml deleted file mode 100644 index bcd2967ea73..00000000000 --- a/byte-buddy-gradle-plugin/android-plugin-test/app/src/main/res/layout/activity_main.xml +++ /dev/null @@ -1,26 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/byte-buddy-gradle-plugin/android-plugin-test/app/src/main/res/mipmap-mdpi/ic_launcher.webp b/byte-buddy-gradle-plugin/android-plugin-test/app/src/main/res/mipmap-mdpi/ic_launcher.webp deleted file mode 100644 index 4f0f1d64e58..00000000000 Binary files a/byte-buddy-gradle-plugin/android-plugin-test/app/src/main/res/mipmap-mdpi/ic_launcher.webp and /dev/null differ diff --git a/byte-buddy-gradle-plugin/android-plugin-test/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp b/byte-buddy-gradle-plugin/android-plugin-test/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp deleted file mode 100644 index 62b611da081..00000000000 Binary files a/byte-buddy-gradle-plugin/android-plugin-test/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp and /dev/null differ diff --git a/byte-buddy-gradle-plugin/android-plugin-test/app/src/main/res/values/colors.xml b/byte-buddy-gradle-plugin/android-plugin-test/app/src/main/res/values/colors.xml deleted file mode 100644 index ca1931bca99..00000000000 --- a/byte-buddy-gradle-plugin/android-plugin-test/app/src/main/res/values/colors.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - #FFBB86FC - #FF6200EE - #FF3700B3 - #FF03DAC5 - #FF018786 - #FF000000 - #FFFFFFFF - diff --git a/byte-buddy-gradle-plugin/android-plugin-test/app/src/main/res/values/strings.xml b/byte-buddy-gradle-plugin/android-plugin-test/app/src/main/res/values/strings.xml deleted file mode 100644 index b02be6da869..00000000000 --- a/byte-buddy-gradle-plugin/android-plugin-test/app/src/main/res/values/strings.xml +++ /dev/null @@ -1,3 +0,0 @@ - - app - diff --git a/byte-buddy-gradle-plugin/android-plugin-test/app/src/main/res/values/themes.xml b/byte-buddy-gradle-plugin/android-plugin-test/app/src/main/res/values/themes.xml deleted file mode 100644 index 227462dcdf0..00000000000 --- a/byte-buddy-gradle-plugin/android-plugin-test/app/src/main/res/values/themes.xml +++ /dev/null @@ -1,4 +0,0 @@ - - -