Skip to content

Commit

Permalink
DEF-2701 - Added extension Jars to classpath when compiling Java sour…
Browse files Browse the repository at this point in the history
…ces. (#44)
  • Loading branch information
andsveking authored May 16, 2017
1 parent 18a3078 commit aa49147
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 1 deletion.
7 changes: 7 additions & 0 deletions server/src/main/java/com/defold/extender/Extender.java
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,13 @@ public File buildJavaExtension(File manifest, Map<String, Object> manifestContex
if (rJar != null) {
classPath += ":" + rJar.getAbsolutePath();
}

// Get extension supplied Jar libraries
List<String> extJars = getJars(extDir);
for (String jarPath : extJars) {
classPath += ":" + jarPath;
}

context.put("classPath", classPath);
context.put("sourcesListFile", sourcesListFile.getAbsolutePath());
String command = templateExecutor.execute(platformConfig.javacCmd, context);
Expand Down
2 changes: 1 addition & 1 deletion server/src/test/java/com/defold/extender/ExtenderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ public void testCollectLibraries() {
@Test
public void testCollectJars() {
List<String> result = Extender.collectFilesByPath(new File("test-data/ext/lib/armv7-android"), "(.+\\.jar)");
assertEquals(1, result.size());
assertEquals(2, result.size());
assertTrue(result.get(0).endsWith("test-data/ext/lib/armv7-android/Dummy.jar"));
}

Expand Down
63 changes: 63 additions & 0 deletions server/src/test/java/com/defold/extender/IntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,69 @@ public void buildAndroidCheckCompiledJava() throws IOException, ExtenderClientEx
assertTrue(dexClasses.contains("Lcom/defold/Test;"));
}

/*
* Test if a Java source can import classes specified in a supplied Jar file.
*/
@Test
public void buildAndroidJavaJarDependency() throws IOException, ExtenderClientException, InterruptedException {

org.junit.Assume.assumeTrue("Defold version does not support Java compilation test.",
configuration.platform.contains("android") &&
(configuration.version.version.isGreaterThan(1, 2, 103) || configuration.version.version.isVersion(0, 0, 0) )
);

clearCache();

File cacheDir = new File("build");
ExtenderClient extenderClient = new ExtenderClient("http://localhost:" + EXTENDER_PORT, cacheDir);
List<ExtenderResource> sourceFiles = Lists.newArrayList(
new FileExtenderResource("test-data/ext/ext.manifest"),
new FileExtenderResource("test-data/ext/src/test_ext.cpp"),
new FileExtenderResource("test-data/ext/src/TestJar.java"),
new FileExtenderResource("test-data/ext/lib/armv7-android/libalib.a"),
new FileExtenderResource("test-data/ext/lib/armv7-android/JarDep.jar"));
File destination = Files.createTempFile("dmengine", ".zip").toFile();
File log = Files.createTempFile("dmengine", ".log").toFile();

String platform = configuration.platform;
String sdkVersion = configuration.version.sha1;

try {
extenderClient.build(
platform,
sdkVersion,
sourceFiles,
destination,
log
);
} catch (ExtenderClientException e) {
System.out.println(new String(Files.readAllBytes(log.toPath())));
throw e;
}

assertTrue("Resulting engine should be of a size greater than zero.", destination.length() > 0);
assertEquals("Log should be of size zero if successful.", 0, log.length());

ZipFile zipFile = new ZipFile(destination);
ZipEntry classesDexEntry = zipFile.getEntry("classes.dex");
assertNotEquals(null, classesDexEntry);
assertNotEquals(null, zipFile.getEntry("libdmengine.so"));


InputStream in = zipFile.getInputStream(classesDexEntry);
Path tmpClassesDexPath = Files.createTempFile("classes", "dex");
Files.copy(in, tmpClassesDexPath, StandardCopyOption.REPLACE_EXISTING);

// Verify that classes.dex contains our Dummy class and our compiled Java class
DexFile dexFile = DexFileFactory.loadDexFile(tmpClassesDexPath.toFile().getAbsolutePath(), 19 ); // api level
Set<String> dexClasses = new HashSet<>();
for (ClassDef classDef: dexFile.getClasses()) {
dexClasses.add(classDef.getType());
}

assertTrue(dexClasses.contains("Lcom/defold/JarDep;"));
assertTrue(dexClasses.contains("Lcom/defold/Test;"));
}
@Test
public void buildAndroidRJar() throws IOException, ExtenderClientException, InterruptedException {

Expand Down
Binary file added server/test-data/ext/lib/armv7-android/JarDep.jar
Binary file not shown.
9 changes: 9 additions & 0 deletions server/test-data/ext/src/TestJar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.defold;

import com.defold.JarDep;

class Test {
static String doStuff() {
return JarDep.DoStuff();
}
}

0 comments on commit aa49147

Please sign in to comment.