출처: https://arincblossom.wordpress.com/2015/07/15/multidex를-사용할-수-없는-eclipse에서-jar에-dex를-추가하여-메소드-65536/
흔한 라이브러리 왕창 쓰면 나는 오류
Conversion to Dalvik format failed: |
해결법 출처
http://stackoverflow.com/questions/24135158/how-to-load-a-jar-from-assets-folder-at-runtime
I got the answer. I am adding this answer here. Because this may be helpful to some others searching.
There are steps to accomplish this.
You have to make a copy of your JAR file into the private internal storage of your aplication.
Using the dx tool inside the android folder, you have to generate a classes.dex file associated with the JAR file. The dx tool will be at the location /android-sdks/build-tools/19.0.1 (this file is needed by the Dalvik VM, simply jar can not be read by the dalvik VM))
Using the aapt tool command which is also inside the same location, you have to add the classes.dex to the JAR file.
This JAR file could be loaded dynamically using DexClassLoader.
If you are making a JAR from any one your own library, you have to do this steps (1-4) every time when there is a change in your library source code. So you can automate this steps by creating a shell script(in Mac/Linux/Ubuntu) or batch scripts(in Windows). You can refere this link to understand how to write shell scripts.
Note : one situation for implementing this method is, when it is impossible to add the JAR files directly to the build path of core project and need to be loaded dynamically at run time. In normal cases the JAR files could be added to the build path.
please check this link for the detailed code and implementation.
How to load a jar file at runtime
ClassLoader loader = URLClassLoader.newInstance(
|
Android: How to dynamically load classes from a JAR file?
Class myClass = ClassLoader.getSystemClassLoader().loadClass("android.app.admin.DevicePolicyManager") Object DPMInstance = myClass.newInstance(); Method myMethod = myClass.getMethod("setPasswordQuality", new Class[] { ComponentName.class, int.class }); myMethod.invoke(DPMInstance, new Object[] { myComponentName, PASSWORD_QUALITY_NUMERIC }); |
1. 안드로이드 sdk 내에서 build-tools/버전/dx.bat라는 툴이 있음.
이를 사용하여 dex파일을 생성한다.
dx --dex --output="output 파일 경로 및 이름" "대상 파일 경로 및 이름" (상대 경로도 가능) |
2. aapt.exe 툴을 사용하여 기존 jar에 dex파일을 추가
3. 이렇게 만들어진 jar은 DexClassLoader를 통해 동적으로 로드가 가능해진다
여기서 만든 jar들은 assets 폴더에 들어가야 한다.
답변글 아래에 보면 ClassLoader 사용법이 링크로 있음
'IT_Programming > Android_Java' 카테고리의 다른 글
안드로이드 여러 APK 를 하나의 어플리케이션으로 관리하기 (0) | 2015.06.23 |
---|---|
Android Studio로 버전코드/버전네임 관리하기 (0) | 2015.06.23 |
[Android] jar, apk의 메소드 아이디 카운트 체크 (0) | 2015.06.23 |
[펌] 안드로이드 회전하는 뷰 만들기 (0) | 2015.06.08 |
JNI Local Reference Changes in ICS ( ICS 부터 바뀌는 JNI Local Reference ) (0) | 2015.06.05 |