Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / Android

An Introduction to Jack and Jill on X86

3.50/5 (2 votes)
30 Oct 2015CPOL4 min read 8.8K  
Jack (Java Android Compiler Kit) is a new Google* tool that includes a compiler from Java source code to the Android dex file format.

This article is for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers

Intel® Developer Zone offers tools and how-to information for cross-platform app development, platform and technology information, code samples, and peer expertise to help developers innovate and succeed. Join our communities for Android, Internet of Things, Intel® RealSense™ Technology, and Windows to download tools, access dev kits, share ideas with like-minded developers, and participate in hackathon’s, contests, roadshows, and local events.

Jack (Java* Android* Compiler Kit) is a new Google* tool that includes a compiler from Java source code to the Android dex file format. Jack has its own .jack library and provides most toolchain features as part of a single tool: repackaging, shrinking, obfuscation and multidex. There is also a tool that translates existing .jar files to the .jack library format. This is Jill (Jack Intermediate Library Linker).

Overview

When the tool is enabled, Jill will translate any libraries you are referencing to a new Jack library file (.jack). This prepares them to be quickly merged with other .jack files. The Jack and Android Gradle plugin collects any .jack library files along with your source code and compiles them into a set of dex files. During this process Jack also handles any requested code minification (shrinking and/or obfuscation). The output is then assembled into an apk-file as normal.

Jack and Jill App Build

How to Use Jack with the Gradle Plugin

Jack and Jill are available in Build Tools as of version 21.1.1, via the SDK Manager (Figure 1). Complementary Gradle and Android Studio support is also already available in the Android 1.0.0+ Gradle plugin.
The Gradle plugin enables the experimental Jack build tools by adding useJack in your build config (useJack=true).

Jack and Jill App Build

Figure 1

Using Command Lines

To display the usage information for Jack and Jill use the commands below. Some features may be available on the command line before they are made available through the Android Gradle plugin.

  • Jack usage (Figure 2)
    • java –jar <SDK>/build-tools/< Build Tools version>/jack.jar –help
  • Jack usage (Figure 3)
    • java –jar <SDK>/build-tools/< Build Tools version>/jill.jar –help

Jack &amp; Jill command lines

Figure 2

Jack &amp; Jill command lines

Figure 3

Compilation Support

Java programming language 1.7.
Repackaging, shrinking, obfuscation and multidex features are supported.
Annotation processing is not supported.

Shrinking and Obfuscation Support

Proguard configuration files can be specified on the command line through the "--config-proguard" option.

  • Common options:
    @
    -include
    -basedirectory
    -injars
    -outjars // only 1 output jar supported
    -libraryjars
    -dontoptimize // required: Jack does not optimize
    -dontpreverify // required: Jack does not preverify
    -dontskipnonpubliclibraryclasses
    -dontskipnonpubliclibraryclassmembers
    -forceprocessing
    -keep
    -keepclassmembers
    -keepclasseswithmembers
    -keepnames
    -keepclassmembernames
    -keepclasseswithmembernames
    -printseeds
  • Shrinking options:
    -dontshrink
  • Obfuscation options:
    -dontobfuscate
    -printmapping
    -applymapping
    -obfuscationdictionary
    -classobfuscationdictionary
    -packageobfuscationdictionary
    -useuniqueclassmembernames
    -dontusemixedcaseclassnames
    -keeppackagenames
    -flattenpackagehierarchy
    -repackageclasses
    -keepattributes
    -adaptclassstrings

Repackaging Support

Jack is compatible with "rule" rule types, but is not compatible with "zap" or "keep" rule types. Rule files can be specified on the command line through the "—config-jarjar" option.

Using Gradle

Android Gradle plugin support is under development and there are some limitations:

  • The "-applymapping" obfuscation directive is not yet supported
  • Repackaging (similar to the jarjar tool) is not integrated
  • Jacoco instrumentation is not supported
  • Bytecode manipulation is not supported
  • Some users may receive an Out Of Memory exception while building very large apps. You can resolve this by configuring the build system to use 2G of RAM (or more):
dexOptions { javaMaxHeapSize "2048M" }

Now let's go to the useJack tool. Firstly we have to import an existing Android Application Project in Eclipse IDE as shown below.

Using JACK

  1. Click File -> Click Import… (Figure 1)

    File import

    Figure 1
  2. Click 'Existing Android Code Into Workspace' (Figure 2)

    Existing file

    Figure 2
  3. Click Browse… and select the Hello-jni project from the NDK samples directory (Figure 3)

    Browse

    Figure 3
  4. Finally click Finish (Figure 4)

    Finish

    Figure 4

Now we need to edit Application.mk file in the jni directory as follows:

APP_ABI := x86

Then we have to create file build.gradle for our Android Project as shown below.

  1. Right-click on the project. Then select New and click File (Figure 5)

    New file

    Figure 5
  2. Enter build.gradle to the File Name field and Click Finish (Figure 6)

    build.gradle

    Figure 6

Next we need to edit the build.gradle file as follows:

Java
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.0.0'
    }
}
apply plugin: 'com.android.application'

android {
    lintOptions {
		abortOnError false
    }

    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
    	applicationId "com.example.hellojni"
        minSdkVersion 19
		targetSdkVersion 21
		ndk {
            moduleName "hello-jni"
        }
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
            jni.srcDirs = []
    		jniLibs.srcDirs = ['libs']
        }
		
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
    
    task buildNative(type: Exec, description: 'Compile JNI source via NDK') {
        def ndkDir = android.plugin.ndkFolder
        commandLine "$ndkDir/ndk-build",
                '-C', file('src/com/example/hellojni').absolutePath,
                '-j', Runtime.runtime.availableProcessors(),
                'all',
                'NDK_DEBUG=1'
    }

    task cleanNative(type: Exec, description: 'Clean JNI object files') {
        def ndkDir = android.plugin.ndkFolder
        commandLine "$ndkDir/ndk-build",
                '-C', file('src/com/example/hellojni').absolutePath,
                'clean'
    }

    clean.dependsOn 'cleanNative'

    tasks.withType(JavaCompile) {
        compileTask -> compileTask.dependsOn buildNative
    }
}

Notice that the file contains the experimental Jack build tools as useJack = true.

Also to compile the native part of the sample we have to create a local.properties file at the root of the project and edit the file adding the next line:

ndk.dir=<path_to_ndk>

Finally to build the project we will use the next command:

gradle build

After you have seen the message "BUILD SUCCESSFUL" in the directory <path_to_project>/build/outputs/apk/ we can see apk-files.
Run hello-jni-debug.apk (Figure 7).

BUILD SUCCESSFUL

Figure 7

About The Author

Denis Smirnov is a Software Intern and has worked at Intel as a Technical Intern. Denis is getting his master's degree in Computer Science in the Nizhny Novgorod State Technical University in the department Applied Mathematics.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)