在Dalvik中加载Android自定义类

时间:2014-04-04 13:03:39

标签: java android dalvik

我有一个包含许多库的大型项目 所有库都放在/ libs文件夹中。我需要将它们分成多个dex文件,关于每个dex的方法计数限制,否则我遇到了这个错误:

Error:Android Dex: [android-custom-class-loading-sample]   java.lang.IllegalArgumentException: method ID not in [0, 0xffff]: 65536

这是我的build.xml的一部分

<!-- This is a modified version of "-dex" target taken from $SDK/tools/ant/main_rules.xml -->
    <!-- Converts this project's .class files into .dex files -->
    <target name="-dex" depends="-compile, -post-compile, -obfuscate"
            unless="do.not.compile">
        <if condition="${manifest.hasCode}">
            <then>
                <!-- Create staging directories to store .class files to be converted to the -->
                <!-- default dex and the secondary dex. -->
                <mkdir dir="${out.classes.absolute.dir}.1"/>
                <mkdir dir="${out.classes.absolute.dir}.2"/>

                <!-- Primary dex to include everything but the concrete library implementation. -->
                <copy todir="${out.classes.absolute.dir}.1" >
                    <fileset dir="${out.classes.absolute.dir}" >
                        <exclude name="com/example/dex/lib/**" />
                    </fileset>
                </copy>
                <!-- Secondary dex to include the concrete library implementation. -->
                <copy todir="${out.classes.absolute.dir}.2" >
                    <fileset dir="${out.classes.absolute.dir}" >
                        <include name="com/example/dex/lib/**" />
                    </fileset>
                </copy>

                <!-- Compile .class files from the two stage directories to the apppropriate dex files. -->
                <dex-helper-mod input-dir="${out.classes.absolute.dir}.1"
                    output-dex-file="${out.absolute.dir}/${dex.file.name}" />
                <mkdir dir="${out.absolute.dir}/secondary_dex_dir" />
                <dex-helper-mod input-dir="${out.classes.absolute.dir}.2"
                    output-dex-file="${out.absolute.dir}/secondary_dex_dir/classes.dex" />
                <!-- Jar the secondary dex file so it can be consumed by the DexClassLoader. -->
                <!-- Package the output in the assets directory of the apk. -->
                <jar destfile="${asset.absolute.dir}/secondary_dex.jar"
                     basedir="${out.absolute.dir}/secondary_dex_dir" includes="classes.dex" />
            </then>
            <else>
                <echo>hasCode = false. Skipping...</echo>
            </else>
        </if>
    </target>

如何正确拆分库以摆脱该错误?如果有人有经验,请分享。

非常感谢。

0 个答案:

没有答案