Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix all Gradle build warnings #1170

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ buildscript {
'guava': 'com.google.guava:guava:23.6-jre',
'release': [
'runtime': "com.jakewharton:butterknife:${versions.release}",
'compiler': "com.jakewharton:butterknife-compiler:${versions.release}"
'compiler': "com.jakewharton:butterknife-compiler:${versions.release}",
'gradlePlugin': "com.jakewharton:butterknife-gradle-plugin:${versions.release}"
],
'kotlin': [
'stdLibJre8': "org.jetbrains.kotlin:kotlin-stdlib-jre8:${versions.kotlin}",
'reflect': "org.jetbrains.kotlin:kotlin-reflect:${versions.kotlin}"
]
]

Expand All @@ -74,7 +76,7 @@ subprojects { project ->
google()
}

if (!project.name.equals('butterknife-gradle-plugin')) {
if (project.name != 'butterknife-gradle-plugin') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

apply plugin: 'checkstyle'

task checkstyle(type: Checkstyle) {
Expand All @@ -93,4 +95,26 @@ subprojects { project ->
}
}
}

project.tasks.withType(JavaCompile) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

it.options.with {
setCompilerArgs(getCompilerArgs() + [
// strict compilation, treat all warnings as errors
'-Werror',
// enable all warnings
'-Xlint:all',
// hide warning: [options] bootstrap class path not set in conjunction with -source 1.7
'-Xlint:-options',
// hide warning: No processor claimed any of these annotations: android.support.annotation.Nullable
'-Xlint:-processing'
])
}
}

project.tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) {
kotlinOptions {
verbose = true
allWarningsAsErrors = true
}
}
}
2 changes: 1 addition & 1 deletion butterknife-compiler/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dependencies {
implementation project(':butterknife-annotations')
implementation deps.auto.common
implementation deps.guava
api deps.javapoet
implementation deps.javapoet
compileOnly deps.auto.service
compileOnly files(org.gradle.internal.jvm.Jvm.current().getToolsJar())

Expand Down
1 change: 1 addition & 0 deletions butterknife-gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ dependencies {
implementation deps.javaparser
implementation deps.javapoet
implementation deps.kotlin.stdLibJre8
configurations.all.each { it.resolutionStrategy.force deps.kotlin.reflect }

testImplementation deps.junit
testImplementation deps.truth
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,6 @@ class ButterKnifePlugin : Plugin<Project> {
}

private operator fun <T : Any> ExtensionContainer.get(type: KClass<T>): T {
return getByType(type.java)!!
return getByType(type.java)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private FinalRClassBuilder() { }
public static void brewJava(File rFile, File outputDir, String packageName, String className)
throws Exception {
CompilationUnit compilationUnit = JavaParser.parse(rFile);
TypeDeclaration resourceClass = compilationUnit.getTypes().get(0);
TypeDeclaration<?> resourceClass = compilationUnit.getTypes().get(0);

TypeSpec.Builder result =
TypeSpec.classBuilder(className).addModifiers(PUBLIC).addModifiers(FINAL);
Expand All @@ -65,7 +65,7 @@ private static void addResourceType(List<String> supportedTypes, TypeSpec.Builde
String type = node.getNameAsString();
TypeSpec.Builder resourceType = TypeSpec.classBuilder(type).addModifiers(PUBLIC, STATIC, FINAL);

for (BodyDeclaration field : node.getMembers()) {
for (BodyDeclaration<?> field : node.getMembers()) {
if (field instanceof FieldDeclaration) {
FieldDeclaration declaration = (FieldDeclaration) field;
// Check that the field is an Int because styleable also contains Int arrays which can't be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import org.junit.runners.Parameterized.Parameters
import java.io.File

import java.util.Properties

@RunWith(Parameterized::class)
class FixturesTest(val fixtureRoot: File, val name: String) {
@Suppress("unused") // Used by JUnit reflectively.
@get:Rule val buildFilesRule = BuildFilesRule(fixtureRoot)

@Test fun execute() {
val androidHome = androidHome()
File(fixtureRoot, "local.properties").writeText("sdk.dir=$androidHome\n")
fixtureRoot.resolve("local.properties").writer().use {
val localProp = Properties()
localProp.setProperty("sdk.dir", androidHome())
localProp.store(it, "generated by ${this::class}")
}

val runner = GradleRunner.create()
.withProjectDir(fixtureRoot)
Expand Down Expand Up @@ -48,4 +51,4 @@ class FixturesTest(val fixtureRoot: File, val name: String) {
.filter { it.isDirectory }
.map { arrayOf(it, it.name) }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.example.butterknife;

import android.test.ActivityInstrumentationTestCase2;
import com.example.butterknife.library.SimpleActivity;

public final class SimpleActivityTest extends ActivityInstrumentationTestCase2<SimpleActivity> {
@SuppressWarnings("deprecation") // ignore, simple test is simple, Espresso is too heavy for this
public final class SimpleActivityTest
extends android.test.ActivityInstrumentationTestCase2<SimpleActivity> {
public SimpleActivityTest() {
super(SimpleActivity.class);
}
Expand Down
29 changes: 19 additions & 10 deletions butterknife/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,29 @@ dependencies {
testImplementation files(getRuntimeJar())
testImplementation files(org.gradle.internal.jvm.Jvm.current().getToolsJar())
testImplementation project(':butterknife-compiler')
testCompileOnly deps.javapoet
}

def getRuntimeJar() {
try {
final File javaBase = new File(System.getProperty("java.home")).getCanonicalFile();
File runtimeJar = new File(javaBase, "lib/rt.jar");
if (runtimeJar.exists()) {
return runtimeJar;
project.tasks.all {
if (it.name =~ /AndroidTestJavaWithJavac$/) {
it.options.with {
setCompilerArgs(getCompilerArgs() + [
// runner-1.0.1.aar\1fdddc98585f7e7ee30d9c2c44d0ee7f\jars\classes.jar
// (android/support/test/InstrumentationRegistry.class):
// warning: [classfile] MethodParameters attribute
// introduced in version 52.0 class files is ignored in version 51.0 class files
'-Xlint:-classfile'
])
}
runtimeJar = new File(javaBase, "jre/lib/rt.jar");
return runtimeJar.exists() ? runtimeJar : null;
} catch (IOException e) {
throw new RuntimeException(e);
}
}

static def getRuntimeJar() {
final javaBase = new File(System.properties.'java.home').canonicalFile
[ 'lib/rt.jar', 'jre/lib/rt.jar' ]
.collect { new File(javaBase, it) }
.grep { it.exists() }
.find()
}

apply from: rootProject.file('gradle/gradle-mvn-push.gradle')
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ static class IntTarget {

@Test public void asInt() {
IntTarget target = new IntTarget();
@SuppressWarnings("deprecation")
int expected = context.getResources().getColor(R.color.red);

Unbinder unbinder = new BindColorTest$IntTarget_ViewBinding(target, context);
Expand All @@ -34,6 +35,7 @@ static class ColorStateListTarget {

@Test public void asColorStateList() {
ColorStateListTarget target = new ColorStateListTarget();
@SuppressWarnings("deprecation")
ColorStateList expected = context.getResources().getColorStateList(R.color.colors);

Unbinder unbinder = new BindColorTest$ColorStateListTarget_ViewBinding(target, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ static class Target {

@Test public void asDrawable() {
Target target = new Target();
@SuppressWarnings("deprecation")
Drawable expected = context.getResources().getDrawable(R.drawable.circle);

Unbinder unbinder = new BindDrawableTest$Target_ViewBinding(target, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public final class UtilsTest {
Utils.findRequiredView(view, android.R.id.button1, "yo mama");
fail();
} catch (IllegalStateException e) {
assertThat(e).hasMessage("Required view 'button1' with ID "
assertThat(e).hasMessageThat().isEqualTo("Required view 'button1' with ID "
+ android.R.id.button1
+ " for yo mama was not found. If this view is optional add '@Nullable' (fields) or '@Optional' (methods) annotation.");
}
Expand All @@ -51,7 +51,7 @@ public final class UtilsTest {
Utils.findRequiredView(view, android.R.id.button1, "yo mama");
fail();
} catch (IllegalStateException e) {
assertThat(e).hasMessage("Required view '<unavailable while editing>' "
assertThat(e).hasMessageThat().isEqualTo("Required view '<unavailable while editing>' "
+ "with ID " + android.R.id.button1
+ " for yo mama was not found. If this view is optional add '@Nullable' (fields) or '@Optional' (methods) annotation.");
}
Expand All @@ -62,7 +62,7 @@ public final class UtilsTest {
Utils.castParam("abc", "Foo", 3, "foo()", 4, Integer.class);
fail();
} catch (IllegalStateException ise) {
assertThat(ise.getMessage()).isEqualTo(
assertThat(ise).hasMessageThat().isEqualTo(
"Parameter #4 of method 'Foo' was of the wrong type for parameter #5 of method 'foo()'. See cause for more info.");
}
}
Expand Down
7 changes: 4 additions & 3 deletions butterknife/src/main/java/butterknife/ButterKnife.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,10 @@ private static Constructor<? extends Unbinder> findBindingConstructorForClass(Cl
return null;
}
try {
Class<?> bindingClass = cls.getClassLoader().loadClass(clsName + "_ViewBinding");
//noinspection unchecked
bindingCtor = (Constructor<? extends Unbinder>) bindingClass.getConstructor(cls, View.class);
@SuppressWarnings("unchecked")
Class<Unbinder> bindingClass = (Class<Unbinder>)
cls.getClassLoader().loadClass(clsName + "_ViewBinding");
bindingCtor = bindingClass.getConstructor(cls, View.class);
if (debug) Log.d(TAG, "HIT: Loaded binding class and constructor.");
} catch (ClassNotFoundException e) {
if (debug) Log.d(TAG, "Not found. Trying superclass " + cls.getSuperclass().getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import android.view.View;

/**
* A {@linkplain View.OnClickListener click listener} that debounces multiple clicks posted in the
* same frame. A click on one button disables all buttons for that frame.
* A {@linkplain android.view.View.OnClickListener click listener} that debounces multiple clicks
* posted in the same frame. A click on one button disables all buttons for that frame.
*/
public abstract class DebouncingOnClickListener implements View.OnClickListener {
static boolean enabled = true;
Expand Down
4 changes: 3 additions & 1 deletion butterknife/src/main/java/butterknife/internal/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ public static float getFloat(Context context, @DimenRes int id) {
}

@SafeVarargs
@SuppressWarnings("varargs")
public static <T> T[] arrayOf(T... views) {
return filterNull(views);
}

@SafeVarargs
@SuppressWarnings("varargs")
public static <T> List<T> listOf(T... views) {
return new ImmutableList<>(filterNull(views));
}
Expand All @@ -71,7 +73,7 @@ private static <T> T[] filterNull(T[] views) {
if (end == length) {
return views;
}
//noinspection unchecked
@SuppressWarnings("unchecked")
T[] newViews = (T[]) Array.newInstance(views.getClass().getComponentType(), end);
System.arraycopy(views, 0, newViews, 0, end);
return newViews;
Expand Down
15 changes: 13 additions & 2 deletions gradle/gradle-mvn-push.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ afterEvaluate { project ->

if (project.getPlugins().hasPlugin('com.android.application') ||
project.getPlugins().hasPlugin('com.android.library')) {
def releaseVariants = project.android.libraryVariants.findAll {
it.buildType.name.equalsIgnoreCase('release')
}
task install(type: Upload, dependsOn: assemble) {
repositories.mavenInstaller {
configuration = configurations.archives
Expand Down Expand Up @@ -135,9 +138,17 @@ afterEvaluate { project ->
}
}

task androidJavadocs(type: Javadoc) {
task androidJavadocs(type: Javadoc, dependsOn: javaPreCompileRelease) {
source = android.sourceSets.main.java.source
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
classpath += project.files(android.bootClasspath.join(File.pathSeparator))
doFirst {
classpath += files(releaseVariants.collect { it.javaCompile.classpath.files })
}
options {
jFlags '-Xmx128M'
addStringOption 'Xmaxwarns', '1000'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

addStringOption 'Xmaxerrs', '1000'
}
}

task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
Expand Down
2 changes: 1 addition & 1 deletion sample/library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
}

dependencies {
classpath "com.jakewharton:butterknife-gradle-plugin:${versions.release}"
classpath deps.release.gradlePlugin
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}
}

Expand Down