How can you get Gradle to fail assembly when there is a non-UTF8 character in the source code?

This is a pretty rare problem, but I would really like it to not build when this happens:

/Users/jundai/perforce/trunk/service/test/com/mycompany/PriceFormattingTests.java:93: error: unmappable character for encoding UTF-8 return new CurrencyModel("373959", new Price("10.20", "EUR"), " 10.20", new Price("12.10", "USD"), "$12.10"); 

With Ant or running javac on the command line, using -source 1.6 or -source 1.7 will crash. Using Gradle, it prints as error: (if sourceCompatibility set to 6 or higher), but the build is still successful.

I tried various ways to get the -source argument in the javac command for the compileJava task, but nothing I tried seems to be able to get Gradle to report this as an error.

Anyone else run into this?

EDIT: a few more details:

If I have a file encoded in winansi : src/main/java/Test.java :

 public class Test { public static void main(String[] args) { System.out.println("Testing UTF-8 compilation: C'est drΓ΄le, tout Γ  coup je ne sais pas quoi dire."); } } 

then this passes without errors or warnings using these build.gradle , Gradle 1.3 and Java 1.7:

 apply plugin: 'java' tasks.withType(Compile) { options.encoding = "iso-8859-1" } 

Exit:

 [1.9.3-p327] gradle$ gradle build :compileJava :processResources UP-TO-DATE :classes :jar :assemble :compileTestJava UP-TO-DATE :processTestResources UP-TO-DATE :testClasses UP-TO-DATE :test :check :build BUILD SUCCESSFUL 

If I remove options.encoding or install it in UTF-8 , I get the following:

 [1.9.3-p327] gradle$ gradle build :compileJava /Users/jbateskobashigawa/play/gradle/src/main/java/Test.java:3: error: unmappable character for encoding UTF8 System.out.println("Testing UTF-8 compilation: C est dr le, tout   coup je ne sais pas quoi dire."); ^ /Users/jbateskobashigawa/play/gradle/src/main/java/Test.java:3: error: unmappable character for encoding UTF8 System.out.println("Testing UTF-8 compilation: C est dr le, tout   coup je ne sais pas quoi dire."); ^ /Users/jbateskobashigawa/play/gradle/src/main/java/Test.java:3: error: unmappable character for encoding UTF8 System.out.println("Testing UTF-8 compilation: C est dr le, tout   coup je ne sais pas quoi dire."); ^ :processResources UP-TO-DATE ... (more stuff) BUILD SUCCESSFUL 

Setting sourceTypeCompatibility between 1.5 , 1.6 and 1.7 doesn't seem to do anything. 1.5 when using -source on javac turns the error into warning: With Gradle, it is still error: but it is interesting that it does not recompile in the next build, whereas 1.6 and 1.7 it does.

I tried all kinds of ways to skip -source in javac when creating Gradle, but none of them work:

Does not create:

 options.compilerArgs < '-source 1.7' 

It creates, but does not throw an error (the same as without a flag):

 options.compilerArgs << '-source' options.compilerArgs << '1.7' 

All this seems to be due to the fact that Gradle does not actually use the javac executable to compile - it uses some kind of JVM compilation API that has a lot of very confusing code, If I try to reproduce what Gradle does, I can create a class to compile my class that looks like this: javax/tools/CompileTest.java

You can reproduce the problem using this mini-project: https://github.com/Jun-Dai/gradle_utf8_compilation_issue

Does anyone know about this problem without analyzing the output of the Gradle assembly and not building based on this particular error message?

+7
javac gradle
source share
3 answers

This seems to allow it to fail assembly:

 options.fork = true options.forkOptions.executable = 'javac' 

Not sure if this has other consequences, but I will give it back.

+1
source share
 //ignoreFailures = true 

Paste and uncomment the line above, if you want it to fail, put this statement in the next section, see if it works.

 tasks.withType(Compile) { options.debug = true options.compilerArgs = ["-g"] } 
+1
source share

Try this in the buildscript section {} of the build.gradle file:

 buildscript { repositories { mavenCentral() } tasks.withType(Compile) { options.encoding = "UTF-8" } dependencies { classpath 'com.android.tools.build:gradle:0.5.+' } } apply plugin: 'java' 

UPDATE Now you can do this:

 android { compileOptions.encoding = "UTF-8" } 
0
source share

All Articles