0

I have a Java server that it is compiling Android APKs by command line and writing the output into a file, this is the source code:

Process p = Runtime.getRuntime().exec("gradlew assembleRelease", null , new File(this.workDir));            
        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = null;     
        while ((line = input.readLine()) != null) {
            standardOutput.writeln(line);
        }
        input.close();

It is working perfect with all the Android projects but if the android project has a directory or a file with rare characters (á, ñ...) inside assets folder, then, gets stuck in line = input.readLine()

I opened jconsole to see where is being stuck and i got this:

Name: Thread-3
State: RUNNABLE
Total blocked: 0  Total waited: 6
Stack trace: 
java.io.FileInputStream.readBytes(Native Method)
java.io.FileInputStream.read(FileInputStream.java:255)
java.io.BufferedInputStream.read1(BufferedInputStream.java:284)
java.io.BufferedInputStream.read(BufferedInputStream.java:345)
   - locked java.io.BufferedInputStream@e70e4f8
sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
   - locked java.io.InputStreamReader@4426351b
java.io.InputStreamReader.read(InputStreamReader.java:184)
java.io.BufferedReader.fill(BufferedReader.java:161)
java.io.BufferedReader.readLine(BufferedReader.java:324)
   - locked java.io.InputStreamReader@4426351b
java.io.BufferedReader.readLine(BufferedReader.java:389)
com.mobinGen.jobs.AndroidJob.releaseCompile(AndroidJob.java:329)
com.mobinGen.jobs.AndroidJob.jobProcess(AndroidJob.java:122)
com.mobinGen.jobs.BaseJob.process(BaseJob.java:138)
com.mobinGen.generationAPI.GeneratorAndroid$AndroidJobsAsker.run(GeneratorAndroid.java:230)

As you can see it is blocked in line 329, which is while ((line = input.readLine()) != null)

How can I deal with this? it is a huge problem because the server is being waiting for ever without compiling the next coming Android projects.

halfer
  • 19,824
  • 17
  • 99
  • 186
NullPointerException
  • 36,107
  • 79
  • 222
  • 382

2 Answers2

1

Firstly, are you sure you want to read from p.getInputStream()? The gradle process will be writing to p.getOutputStream() or p.getErrorStream()

Secondly, I think you should use the tooling api to invoke gradle instead of using Runtime (See GradleConnector)

lance-java
  • 25,497
  • 4
  • 59
  • 101
  • I need to do this without gradleconnector. BTW are you sure about outputstream? in the documentation of inputstream i can read this: Returns the input stream connected to the normal output of the subprocess. The stream obtains data piped from the standard output of the process represented by this Process object. – NullPointerException Feb 22 '16 at 14:49
  • how to get gradle output from outputstream? – NullPointerException Feb 22 '16 at 14:49
0

Perhaps you want to use an explicit charset in the InputStreamReader.

See InputStreamReader(InputStream in, Charset cs)

lance-java
  • 25,497
  • 4
  • 59
  • 101