4 |
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 |
29 |
30 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 | Secondary Dex Sample
19 | Wait
20 | Processing dex file...
21 | Press button to generate an awesome Toast!\n(Method in the secondary dex file is executed)
22 | Toast!
23 |
24 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 | buildscript {
3 | repositories {
4 | jcenter()
5 | }
6 | dependencies {
7 | classpath 'com.android.tools.build:gradle:0.14.4'
8 | }
9 | }
10 |
11 | allprojects {
12 | repositories {
13 | jcenter()
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | #
2 | # Copyright 2014 Timothy Rae
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 | BUILD_TOOLS_VERSION=19.1.0
18 | COMPILE_SDK=19
19 | TARGET_SDK=19
20 | MIN_SDK=3
21 | PLUGIN_NAMESPACE=com.example.toastlib
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/timrae/custom-class-loader/b980edf11b78c162f7ba7eb9cecb098fb167244e/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Nov 26 00:10:16 JST 2014
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.2-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # For Cygwin, ensure paths are in UNIX format before anything is touched.
46 | if $cygwin ; then
47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
48 | fi
49 |
50 | # Attempt to set APP_HOME
51 | # Resolve links: $0 may be a link
52 | PRG="$0"
53 | # Need this for relative symlinks.
54 | while [ -h "$PRG" ] ; do
55 | ls=`ls -ld "$PRG"`
56 | link=`expr "$ls" : '.*-> \(.*\)$'`
57 | if expr "$link" : '/.*' > /dev/null; then
58 | PRG="$link"
59 | else
60 | PRG=`dirname "$PRG"`"/$link"
61 | fi
62 | done
63 | SAVED="`pwd`"
64 | cd "`dirname \"$PRG\"`/" >&-
65 | APP_HOME="`pwd -P`"
66 | cd "$SAVED" >&-
67 |
68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
69 |
70 | # Determine the Java command to use to start the JVM.
71 | if [ -n "$JAVA_HOME" ] ; then
72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
73 | # IBM's JDK on AIX uses strange locations for the executables
74 | JAVACMD="$JAVA_HOME/jre/sh/java"
75 | else
76 | JAVACMD="$JAVA_HOME/bin/java"
77 | fi
78 | if [ ! -x "$JAVACMD" ] ; then
79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
80 |
81 | Please set the JAVA_HOME variable in your environment to match the
82 | location of your Java installation."
83 | fi
84 | else
85 | JAVACMD="java"
86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
87 |
88 | Please set the JAVA_HOME variable in your environment to match the
89 | location of your Java installation."
90 | fi
91 |
92 | # Increase the maximum file descriptors if we can.
93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
94 | MAX_FD_LIMIT=`ulimit -H -n`
95 | if [ $? -eq 0 ] ; then
96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
97 | MAX_FD="$MAX_FD_LIMIT"
98 | fi
99 | ulimit -n $MAX_FD
100 | if [ $? -ne 0 ] ; then
101 | warn "Could not set maximum file descriptor limit: $MAX_FD"
102 | fi
103 | else
104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
105 | fi
106 | fi
107 |
108 | # For Darwin, add options to specify how the application appears in the dock
109 | if $darwin; then
110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
111 | fi
112 |
113 | # For Cygwin, switch paths to Windows format before running java
114 | if $cygwin ; then
115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158 | function splitJvmOpts() {
159 | JVM_OPTS=("$@")
160 | }
161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163 |
164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
165 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/libraries/lib1/build.gradle:
--------------------------------------------------------------------------------
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 | */
17 |
18 | import org.apache.tools.ant.taskdefs.condition.Os
19 |
20 | apply plugin: 'com.android.library'
21 |
22 | android {
23 | compileSdkVersion Integer.parseInt(COMPILE_SDK)
24 | buildToolsVersion BUILD_TOOLS_VERSION
25 |
26 | defaultConfig {
27 | targetSdkVersion Integer.parseInt(TARGET_SDK)
28 | minSdkVersion Integer.parseInt(MIN_SDK)
29 | }
30 | }
31 | // Add the main project as a dependency for our library
32 | dependencies {
33 | compile project(':app')
34 | }
35 | // Define some tasks which are used in the build process
36 | task copyClasses(type: Copy) { // Copy the assembled *.class files for only the current namespace into a new directory
37 | // get directory for current namespace
38 | def namespacePath = PLUGIN_NAMESPACE.replaceAll("\\.","/")
39 | // set source and destination directories
40 | from "build/intermediates/classes/release/${namespacePath}/"
41 | into "build/intermediates/dex/${namespacePath}/"
42 |
43 | // exclude classes which don't have a corresponding entry in the source directory
44 | def remExt = { name -> name.lastIndexOf('.').with {it != -1 ? name[0..
46 | def thisFile = new File("${projectDir}/src/main/java/${namespacePath}/", remExt(details.name)+".java")
47 | if (!(thisFile.exists())) {
48 | details.exclude()
49 | }
50 | }
51 | }
52 |
53 | task assembleExternalJar << {
54 | // Get the location of the Android SDK
55 | ext.androidSdkDir = System.env.ANDROID_HOME
56 | if(androidSdkDir == null) {
57 | Properties localProps = new Properties()
58 | localProps.load(new FileInputStream(file('local.properties')))
59 | ext.androidSdkDir = localProps['sdk.dir']
60 | }
61 | // Make sure no existing jar file exists as this will cause dx to fail
62 | new File("${buildDir}/intermediates/dex/${PLUGIN_NAMESPACE}.jar").delete();
63 | // Use command line dx utility to convert *.class files into classes.dex inside jar archive
64 | String cmdExt = Os.isFamily(Os.FAMILY_WINDOWS) ? '.bat' : ''
65 | exec {
66 | commandLine "${androidSdkDir}/build-tools/${BUILD_TOOLS_VERSION}/dx${cmdExt}", '--dex',
67 | "--output=${buildDir}/intermediates/dex/${PLUGIN_NAMESPACE}.jar",
68 | "${buildDir}/intermediates/dex/"
69 | }
70 | copyJarToOutputs.execute()
71 | }
72 |
73 | task copyJarToOutputs(type: Copy) {
74 | // Copy the built jar archive to the outputs folder
75 | from 'build/intermediates/dex/'
76 | into 'build/outputs/'
77 | include '*.jar'
78 | }
79 |
80 |
81 | // Set the dependencies of the build tasks so that assembleExternalJar does a complete build
82 | copyClasses.dependsOn(assemble)
83 | assembleExternalJar.dependsOn(copyClasses)
--------------------------------------------------------------------------------
/libraries/lib1/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
20 |
--------------------------------------------------------------------------------
/libraries/lib1/src/main/java/com/example/toastlib/LibraryProvider.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.toastlib;
18 |
19 | import android.content.Context;
20 | import android.widget.Toast;
21 |
22 | import com.example.dex.LibraryInterface;
23 |
24 | public class LibraryProvider implements LibraryInterface {
25 | public void showAwesomeToast(Context context, String message) {
26 | if (context == null) {
27 | return;
28 | }
29 | Toast.makeText(context,
30 | String.format("++ %s ++", message),
31 | Toast.LENGTH_LONG).show();
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':libraries:lib1'
--------------------------------------------------------------------------------