Skip to content

Commit

Permalink
Enhanced android testing (#1538)
Browse files Browse the repository at this point in the history
* Upgrading AGP to 7.4.0

* Making android test a standalone project

* Adapting ByteBuddyLocalClassesEnhancerTask to the new 7.4.0 apis

* Using ByteBuddyLocalClassesEnhancerTask with the new artifacts api

* Avoiding compilation oom

* Fixing "toTransform" method reflection

* Splitting resources from classes for local projects

* Using custom dependency rule to merge class dirs and resources from local and remote deps

* Using custom dependency rule to merge class dirs and resources from local and remote deps

* Using the last api's ALL scope

* Adding SPI stability tests

* Clean up tests

* Updating tests

* Fixing android descriptor

* Removed project references in ByteBuddyLocalClassesEnhancerTask due to https://docs.gradle.org/8.3/userguide/configuration_cache.html#config_cache:requirements:use_project_during_execution

* Update working regarding ByteBuddyLocalClassesEnhancerTask's scope

* Update working regarding ByteBuddyLocalClassesEnhancerTask's scope

* Update wordings and renaming methods in ByteBuddyAndroidPlugin

* Renaming bytebuddy 7.4 transform tasks

* Removed project references in ByteBuddyLocalClassesEnhancerTask due to https://docs.gradle.org/8.3/userguide/configuration_cache.html#config_cache:requirements:use_project_during_execution

* Update working regarding ByteBuddyLocalClassesEnhancerTask's scope

* Update working regarding ByteBuddyLocalClassesEnhancerTask's scope

* Update wordings and renaming methods in ByteBuddyAndroidPlugin

* Renaming bytebuddy 7.4 transform tasks

* Clean up

* Moving back ByteBuddyAndroidPlugin

* Clean up test's properties

* Removing "getProject()" calls in ByteBuddyLocalClassesEnhancerTask due to https://docs.gradle.org/8.3/userguide/configuration_cache.html#config_cache:requirements:use_project_during_execution

* Renaming vars

* Updating tests

* Reverting indentation and gradle versions

* Fixing indentation

* Cleaning up the readme for tests

* Using Task.getLogger in ByteBuddyLocalClassesEnhancerTask
  • Loading branch information
LikeTheSalad authored Oct 6, 2023
1 parent fe96422 commit 9495532
Show file tree
Hide file tree
Showing 39 changed files with 851 additions and 181 deletions.
5 changes: 5 additions & 0 deletions byte-buddy-gradle-plugin/android-plugin-test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
build/
.gradle/
.idea/
local.properties
captures/
19 changes: 7 additions & 12 deletions byte-buddy-gradle-plugin/android-plugin-test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ android {

dependencies {
implementation group: 'com.google.android.material', name: 'material', version: '1.6.1'
implementation project(":service-definition-lib")
}
Original file line number Diff line number Diff line change
@@ -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";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
net.bytebuddy.android.test.aar.lib.service.TargetServiceImplementation
17 changes: 9 additions & 8 deletions byte-buddy-gradle-plugin/android-plugin-test/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Original file line number Diff line number Diff line change
@@ -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<MainActivity> 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<String> 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"));
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.bytebuddy.android.test">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Bytebuddygradleplugin">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
<manifest package="net.bytebuddy.android.test" />

This file was deleted.

This file was deleted.

Binary file not shown.
Binary file not shown.

This file was deleted.

This file was deleted.

This file was deleted.

14 changes: 10 additions & 4 deletions byte-buddy-gradle-plugin/android-plugin-test/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
buildscript {
def pom = new groovy.xml.XmlSlurper().parse(rootProject.file('pom.xml'))
def version = pom.parent.version.text().toString()
ext {
outerPom = new groovy.xml.XmlSlurper().parse(rootProject.file('../pom.xml'))
byteBuddyVersion = outerPom.parent.version.text().toString()
}
repositories {
mavenCentral()
google()
}
dependencies {
classpath group: 'com.android.tools.build', name: 'gradle', version: '7.2.0'
classpath files(new File("${rootProject.buildDir}/libs/byte-buddy-gradle-plugin-${version}.jar").canonicalFile)
classpath group: 'com.android.tools.build', name: 'gradle', version: '7.4.0'

def byteBuddyPlugin = new File("${rootDir}/../build/libs/byte-buddy-gradle-plugin-${byteBuddyVersion}.jar").canonicalFile
def byteBuddyLib = new File("$rootDir/../../byte-buddy/target/byte-buddy-${byteBuddyVersion}.jar").canonicalFile
classpath files(byteBuddyPlugin)
classpath files(byteBuddyLib.absolutePath)
}
}

Expand Down
4 changes: 4 additions & 0 deletions byte-buddy-gradle-plugin/android-plugin-test/bytebuddy.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dependencies {
def location = new File("$rootDir/../../byte-buddy/target/byte-buddy-${byteBuddyVersion}.jar").canonicalFile
implementation files(location.absolutePath)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
android.useAndroidX=true
Loading

0 comments on commit 9495532

Please sign in to comment.