├── .gitignore ├── .idea ├── .name ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── gradle.xml ├── misc.xml ├── scopes │ └── scope_settings.xml └── vcs.xml ├── LICENSE ├── README.md ├── app ├── build.gradle └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── example │ │ └── dex │ │ ├── LibraryInterface.java │ │ └── MainActivity.java │ └── res │ ├── drawable-hdpi │ └── icon.png │ ├── drawable-ldpi │ └── icon.png │ ├── drawable-mdpi │ └── icon.png │ ├── layout │ └── main.xml │ └── values │ └── strings.xml ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── libraries └── lib1 │ ├── build.gradle │ └── src │ └── main │ ├── AndroidManifest.xml │ └── java │ └── com │ └── example │ └── toastlib │ └── LibraryProvider.java └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # files for the dex VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # built native files 12 | *.o 13 | *.so 14 | 15 | # generated files 16 | bin/ 17 | gen/ 18 | 19 | # Ignore gradle files 20 | .gradle/ 21 | build/ 22 | 23 | # Local configuration file (sdk path, etc) 24 | local.properties 25 | 26 | # Proguard folder generated by Eclipse 27 | proguard/ 28 | 29 | # Eclipse Metadata 30 | .metadata/ 31 | 32 | # Mac OS X clutter 33 | *.DS_Store 34 | 35 | # Windows clutter 36 | Thumbs.db 37 | 38 | # Intellij IDEA (see https://intellij-support.jetbrains.com/entries/23393067) 39 | .idea/workspace.xml 40 | .idea/tasks.xml 41 | .idea/datasources.xml 42 | .idea/dataSources.ids 43 | .idea/libraries 44 | 45 | # Additionally ignore project files themselves so developers can choose their root directory name freely 46 | .idea/modules.xml 47 | *.iml 48 | 49 | # Crowdin files 50 | ankidroid.zip 51 | tools/crowdin_key.txt 52 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | gradle -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | Android API 4 Platform 14 | 15 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /.idea/scopes/scope_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * Copyright 2014 Timothy Rae perceptualchaos2@gmail.com 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Port of the work in the [following blog](http://android-developers.blogspot.jp/2011/07/custom-class-loading-in-dalvik.html) 2 | to the new Gradle based Android Studio build system, as per [this thread on StackOverflow](http://stackoverflow.com/questions/18174022/custom-class-loading-in-dalvik-with-gradle-android-new-build-system/27241083#27241083) 3 | 4 | As the Android Studio Gradle plugin now provides [native multidex support](https://developer.android.com/tools/building/multidex.html), 5 | which effectively solves the Android 65k method limit, the main motivation for using custom class loading at runtime is now 6 | extensibility. In my particular case, I'm trying to make a [plugin framework for AnkiDroid](http://stackoverflow.com/questions/10239596/plugins-architecture-for-an-android-app). 7 | 8 | Therefore the main focus of this version of the project is on building the secondary jar file in a clean and modular manner, 9 | which makes it easy to update the main project without having to update the plugins. The main apk is in the app module, and the library which shows the toast is in the libraries/lib1 module with its own namespace `com.example.toastlib` 10 | 11 | You can compile the .jar plugin file for the library using the `assembleExternalJar` task -- e.g. from the command line: 12 | 13 | `gradlew assembleExternalJar` 14 | 15 | which will generate the file `/libraries/lib1/build/outputs/com.example.toastlib.jar` which you can then copy to the sdcard on your device for the main app to import. 16 | 17 | To compile the main app it's currently necessary to make the following two changes to the build configuration: 18 | 19 | * change the first line of '/app/build.gradle' to `apply plugin: 'com.android.application'` 20 | * remove `':libraries:lib1'` from settings.gradle 21 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion Integer.parseInt(COMPILE_SDK) 5 | buildToolsVersion BUILD_TOOLS_VERSION 6 | 7 | defaultConfig { 8 | applicationId "com.example.dex" 9 | targetSdkVersion Integer.parseInt(TARGET_SDK) 10 | minSdkVersion Integer.parseInt(MIN_SDK) 11 | } 12 | 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt' 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 21 | 23 | 24 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/dex/LibraryInterface.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.example.dex; 18 | 19 | import android.content.Context; 20 | 21 | public interface LibraryInterface { 22 | public void showAwesomeToast(Context context, String message); 23 | } 24 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/dex/MainActivity.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.example.dex; 18 | 19 | import java.io.BufferedInputStream; 20 | import java.io.BufferedOutputStream; 21 | import java.io.File; 22 | import java.io.FileOutputStream; 23 | import java.io.FileInputStream; 24 | import java.io.IOException; 25 | import java.io.OutputStream; 26 | 27 | import android.app.Activity; 28 | import android.app.ProgressDialog; 29 | import android.content.Context; 30 | import android.os.AsyncTask; 31 | import android.os.Bundle; 32 | import android.os.Environment; 33 | import android.util.Log; 34 | import android.view.View; 35 | import android.widget.Button; 36 | 37 | 38 | import com.example.dex.R; 39 | 40 | import dalvik.system.DexClassLoader; 41 | 42 | 43 | public class MainActivity extends Activity { 44 | private static final String SECONDARY_DEX_NAME = "com.example.toastlib.jar"; 45 | 46 | // Buffer size for file copying. While 8kb is used in this sample, you 47 | // may want to tweak it based on actual size of the secondary dex file involved. 48 | private static final int BUF_SIZE = 8 * 1024; 49 | 50 | private Button mToastButton = null; 51 | private ProgressDialog mProgressDialog = null; 52 | 53 | @Override 54 | public void onCreate(Bundle savedInstanceState) { 55 | super.onCreate(savedInstanceState); 56 | setContentView(R.layout.main); 57 | mToastButton = (Button) findViewById(R.id.toast_button); 58 | 59 | // Before the secondary dex file can be processed by the DexClassLoader, 60 | // it has to be first copied from asset resource to a storage location. 61 | final File dexInternalStoragePath = new File(getDir("dex", Context.MODE_PRIVATE), 62 | SECONDARY_DEX_NAME); 63 | if (!dexInternalStoragePath.exists()) { 64 | mProgressDialog = ProgressDialog.show(this, 65 | getResources().getString(R.string.diag_title), 66 | getResources().getString(R.string.diag_message), true, false); 67 | // Perform the file copying in an AsyncTask. 68 | (new PrepareDexTask()).execute(dexInternalStoragePath); 69 | } else { 70 | mToastButton.setEnabled(true); 71 | } 72 | 73 | mToastButton.setOnClickListener(new View.OnClickListener() { 74 | public void onClick(View view) { 75 | // Internal storage where the DexClassLoader writes the optimized dex file to. 76 | final File optimizedDexOutputPath = getDir("outdex", Context.MODE_PRIVATE); 77 | 78 | // Initialize the class loader with the secondary dex file. 79 | DexClassLoader cl = new DexClassLoader(dexInternalStoragePath.getAbsolutePath(), 80 | optimizedDexOutputPath.getAbsolutePath(), 81 | null, 82 | getClassLoader()); 83 | Class libProviderClazz = null; 84 | 85 | try { 86 | // Load the library class from the class loader. 87 | libProviderClazz = 88 | cl.loadClass("com.example.toastlib.LibraryProvider"); 89 | 90 | // Cast the return object to the library interface so that the 91 | // caller can directly invoke methods in the interface. 92 | // Alternatively, the caller can invoke methods through reflection, 93 | // which is more verbose and slow. 94 | LibraryInterface lib = (LibraryInterface) libProviderClazz.newInstance(); 95 | 96 | // Display the toast! 97 | lib.showAwesomeToast(view.getContext(), "hello"); 98 | } catch (Exception exception) { 99 | // Handle exception gracefully here. 100 | exception.printStackTrace(); 101 | } 102 | } 103 | }); 104 | } 105 | 106 | // File I/O code to copy the secondary dex file from asset resource to internal storage. 107 | private boolean prepareDex(File dexInternalStoragePath) { 108 | BufferedInputStream bis = null; 109 | OutputStream dexWriter = null; 110 | 111 | try { 112 | 113 | //bis = new BufferedInputStream(getAssets().open(SECONDARY_DEX_NAME)); 114 | Log.i("customClassLoader", "test"); 115 | File extDir = Environment.getExternalStorageDirectory(); 116 | Log.i("customClassLoader", extDir.getAbsolutePath()); 117 | File dexExternalStorage = new File(extDir, SECONDARY_DEX_NAME); 118 | Log.i("customClassLoader", dexExternalStorage.getAbsolutePath()); 119 | bis = new BufferedInputStream(new FileInputStream(dexExternalStorage)); 120 | dexWriter = new BufferedOutputStream(new FileOutputStream(dexInternalStoragePath)); 121 | byte[] buf = new byte[BUF_SIZE]; 122 | int len; 123 | while((len = bis.read(buf, 0, BUF_SIZE)) > 0) { 124 | dexWriter.write(buf, 0, len); 125 | } 126 | dexWriter.close(); 127 | bis.close(); 128 | return true; 129 | } catch (IOException e) { 130 | if (dexWriter != null) { 131 | try { 132 | dexWriter.close(); 133 | } catch (IOException ioe) { 134 | ioe.printStackTrace(); 135 | } 136 | } 137 | if (bis != null) { 138 | try { 139 | bis.close(); 140 | } catch (IOException ioe) { 141 | ioe.printStackTrace(); 142 | } 143 | } 144 | return false; 145 | } 146 | } 147 | 148 | private class PrepareDexTask extends AsyncTask { 149 | 150 | @Override 151 | protected void onCancelled() { 152 | super.onCancelled(); 153 | if (mProgressDialog != null) mProgressDialog.cancel(); 154 | } 155 | 156 | @Override 157 | protected void onPostExecute(Boolean result) { 158 | super.onPostExecute(result); 159 | if (mProgressDialog != null) mProgressDialog.cancel(); 160 | } 161 | 162 | @Override 163 | protected Boolean doInBackground(File... dexInternalStoragePaths) { 164 | prepareDex(dexInternalStoragePaths[0]); 165 | return null; 166 | } 167 | } 168 | } 169 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/timrae/custom-class-loader/b980edf11b78c162f7ba7eb9cecb098fb167244e/app/src/main/res/drawable-hdpi/icon.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-ldpi/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/timrae/custom-class-loader/b980edf11b78c162f7ba7eb9cecb098fb167244e/app/src/main/res/drawable-ldpi/icon.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-mdpi/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/timrae/custom-class-loader/b980edf11b78c162f7ba7eb9cecb098fb167244e/app/src/main/res/drawable-mdpi/icon.png -------------------------------------------------------------------------------- /app/src/main/res/layout/main.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 20 | 25 |