Skip to content
Browse files

Merge pull request #2151 from mstepanov/timob_9010

[TIMOB-9010] optimize source code packaging
  • Loading branch information...
2 parents 8fd0008 + 1ad941b commit 4419e95ec09bacdacdb0d3b9115fe95439abbc37 @billdawson billdawson committed
View
7 support/android/builder.py
@@ -1328,10 +1328,9 @@ def build_generated_classes(self):
# kroll-apt.jar is needed for modules
classpath = os.pathsep.join([classpath, self.kroll_apt_jar])
+ classpath = os.pathsep.join([classpath, os.path.join(self.support_dir, 'lib', 'titanium-verify.jar')])
if self.deploy_type != 'production':
- classpath = os.pathsep.join([classpath,
- os.path.join(self.support_dir, 'lib', 'titanium-verify.jar'),
- os.path.join(self.support_dir, 'lib', 'titanium-debug.jar')])
+ classpath = os.pathsep.join([classpath, os.path.join(self.support_dir, 'lib', 'titanium-debug.jar')])
debug("Building Java Sources: " + " ".join(src_list))
javac_command = [self.javac, '-encoding', 'utf8',
@@ -1958,8 +1957,8 @@ def jar_includer(path, isfile):
dex_args += self.android_jars
dex_args += self.module_jars
+ dex_args.append(os.path.join(self.support_dir, 'lib', 'titanium-verify.jar'))
if self.deploy_type != 'production':
- dex_args.append(os.path.join(self.support_dir, 'lib', 'titanium-verify.jar'))
dex_args.append(os.path.join(self.support_dir, 'lib', 'titanium-debug.jar'))
# the verifier depends on Ti.Network classes, so we may need to inject it
has_network_jar = False
View
101 support/android/jspacker.py
@@ -1,7 +1,10 @@
#!/usr/bin/env python
+import run
import sys, string, os
-from base64 import b64encode
+
+template_dir = os.path.abspath(os.path.dirname(sys._getframe(0).f_code.co_filename))
+titanium_prep = os.path.abspath(os.path.join(template_dir,'titanium_prep.jar'))
JAVA_TEMPLATE = """\
package ${package_name};
@@ -9,78 +12,65 @@
import java.util.Collections;
import java.util.Map;
import java.util.HashMap;
-import android.util.Base64;
+import java.nio.CharBuffer;
+import java.nio.ByteBuffer;
+import java.nio.charset.Charset;
+import java.lang.reflect.Method;
import org.appcelerator.kroll.util.KrollAssetHelper;
+import org.appcelerator.kroll.common.Log;
public class AssetCryptImpl implements KrollAssetHelper.AssetCrypt
{
- private static Map<String, String> assets = initAssets();
-
- private static Map<String, String> initAssets()
- {
- Map<String, String> assets = new HashMap<String, String>();
-
- ${init_assets}
-
- return Collections.unmodifiableMap(assets);
+ private static class Range {
+ int offset;
+ int length;
+ public Range(int offset, int length) {
+ this.offset = offset;
+ this.length = length;
+ }
}
+${init_assets}
+
public String readAsset(String path)
{
- String data = assets.get(path);
- if (data == null) {
+ Range range = assets.get(path);
+ if (range == null) {
return null;
}
- return new String(Base64.decode(data, Base64.DEFAULT));
+ return new String(filterDataInRange(assetsBytes, range.offset, range.length)); // charset encoding?
+ }
+
+ private static byte[] filterDataInRange(byte[] data, int offset, int length)
+ {
+ try {
+ Class clazz = Class.forName("org.appcelerator.titanium.TiVerify");
+ Method method = clazz.getMethod("filterDataInRange", new Class[] {data.getClass(), int.class, int.class});
+ return (byte[])method.invoke(clazz, new Object[] { data, offset, length });
+ } catch (Exception e) {
+ Log.e("AssetCryptImpl", "Unable to load asset data.", e);
+ }
+ return new byte[0];
}
}
"""
-# Limit Java const String length to 32k.
-JAVA_STR_MAX_LEN = 32 * 1024
-
-def to_java_string(s):
- chunks = []
- for offset in xrange(0, len(s), JAVA_STR_MAX_LEN):
- chunks.append('new String("%s")' % s[offset:offset + JAVA_STR_MAX_LEN])
- return '+'.join(chunks)
-
-def to_java_map(map_var, keys_values):
- """Generate code to put a list of key-value pairs into a Java Map instance.
-
- map_var - The variable name of the Java Map instance.
- keys_values - A list of 2-tuples containing a key and value pair.
- """
- result = []
- for k, v in keys_values:
- result.append('%s.put(%s, %s);' % (map_var, k, v))
- return '\n'.join(result)
-
-def read_file(filename):
- file = open(filename, "rt")
- try:
- lines = file.read()
- finally:
- file.close()
- return lines
-
class Crypt(object):
"""Helps generate source for an AssetCrypt implementation."""
KEYS_MAP_VAR = 'keys'
def __init__(self):
- self.keys = []
- self.assets = []
+ self.files = []
- def add_asset(self, key, data):
+ def add_asset(self, filename):
# Convert Window paths to Unix style.
- self.keys.append(key.replace('\\', '/'))
- self.assets.append(data)
+ self.files.append(filename.replace('\\', '/'))
- def generate_code(self, package, target_file):
+ def generate_code(self, asset_dir, package, target_file):
"""Generate the Java class source and write to target file.
+ asset_dir = The assets base directory
package - The Java package name for this class.
taget_file - Path to output generate Java source file.
"""
@@ -93,14 +83,14 @@ def generate_code(self, package, target_file):
output = open(os.path.join(target_dir, 'AssetCryptImpl.java'), 'w')
- asset_map = []
- for index, key in enumerate(self.keys):
- data = b64encode(self.assets[index])
- asset_map.append((to_java_string(key), to_java_string(data)))
+ sys.stdout.flush()
+ cmdargs = ['java', '-jar', titanium_prep, package, asset_dir]
+ cmdargs.extend(self.files)
+ so = run.run(cmdargs)
output.write(string.Template(JAVA_TEMPLATE).substitute(
package_name = package,
- init_assets = to_java_map('assets', asset_map)
+ init_assets = so
))
output.close()
@@ -123,9 +113,8 @@ def rel_asset_path(path):
# Gather sources together so we can form a crypt to store them.
for source in sources:
filename = str(source)
- lines = read_file(filename)
- crypt.add_asset(rel_asset_path(filename), lines)
+ crypt.add_asset(rel_asset_path(filename))
# Generate Java code and output to target file.
- crypt.generate_code(package, str(target))
+ crypt.generate_code(asset_dir, package, str(target))
View
BIN support/android/lib/titanium-verify.jar
Binary file not shown.
View
BIN support/android/titanium_prep.jar
Binary file not shown.

0 comments on commit 4419e95

Please sign in to comment.
Something went wrong with that request. Please try again.