Dex Lazy Loader

Android library and plugin to load dexed jars on runtime

Introduction

Dex Lazy Loader (DYL) library and plugin were born as a result of reading Macarse's Lazy Loading Dex files article. If you haven't read it yet, I strongly recommend you do before considering using DYL.

The library provides a LazyLoader to load dexed jars on runtime.

The plugin takes care of dexing provided dependencies and jars located at dyl folder into your assets folder.

Using Dex Lazy Loader

We'll demonstrate Dex Lazy Loader using a basic sample based on Macarse's dexLazyLoad sample. For the complete sample code that you can download, compile and run, see Dex Lazy Loader's sample.

Using dyl gradle plugin

First, we apply the plugin and declare our provided dependencies:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'io.github.guicamest.dexlazyloader:plugin:(insert latest version)'
    }
}

// Make sure to apply this plugin *after* the Android plugin
apply plugin: 'io.github.guicamest.dexlazyloader'

...

dependencies {
    compile 'io.github.guicamest.dexlazyloader:library:(insert latest version)'
    provided 'com.squareup.picasso:picasso:2.5.2'
}

This will take provided dependencies and dex them into your assets folder.

If you want to use your own compiled version of that dependency, you can place it under the dyl folder. For example:

src/
    main/
        assets/
        java/
        dyl/
            picasso-2.5.2.jar

Using dyl library

Among the declared dependencies, we included the library:

dependencies {
    ...
    compile 'io.github.guicamest.dexlazyloader:library:(insert latest version)'
    ...
}

With the library, we can use LazyLoader, SimpleLazyLoadAsyncTask or LazyLoadService classes to load the dexed jars, generated in the previous step, dinamically. In our MainActivity code we have a few examples:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = (ImageView) findViewById(R.id.image_view);

        new SimpleLazyLoadAsyncTask(this){
            @Override
            protected void onPostExecute(Boolean success) {
                super.onPostExecute(success);
                if ( success ){
                    final PicassoWrapper picassoWrapper = PicassoWrapper.get(mContext);
                    Log.d("TAG", "loading image");
                    picassoWrapper.load("https://i.imgur.com/CqmBjo5.jpg", imageView);
                }
            }
        }.execute("picasso-2.5.2.dex.jar");
    }

    public void onLoadImageClick(View v) {
        Log.d("TAG", "onLoadImageClick");

        // TODO: Yes ,this should not be done in the main thread. Sorry Kittens.
        LazyLoader.loadModulesWithContext(this, "picasso-2.5.2.dex.jar");
        Log.d("TAG", "module loaded");

        final PicassoWrapper picassoWrapper = PicassoWrapper.get(this);
        Log.d("TAG", "loading image");
        picassoWrapper.load("https://www.memecreator.org/static/images/memes/3767015.jpg", imageView);
    }

Download

v0.1.0 Library AAR

FAQ

Including/Excluding provided dependencies from dexing

If you use provided dependencies that you don't want to dex to your assets folder, you can configure the dyl extension either by inclusion or exclusion, or both:

dyl {
    exclude=['**/butter**']
}

Eclipse

If you are using eclipse, DYL plugin hooks the eclipse gradle task. So, basically, you need to run ./gradlew yourproject:eclipse to generate dexed jars based on your provided dependencies.

Contributing

If you would like to contribute code you can do so through GitHub by forking the repository and sending a pull request.

When submitting code, please make every effort to follow existing conventions and style in order to keep the code as readable as possible. Please also make sure your code compiles by running gradle clean build.

License

Copyright 2015 guicamest

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Fork me on GitHub