Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Friday, March 7, 2014

ClassLoader in Android

PathClassLoader:
Load class from Android system. So those classes have to be registered in system. They may be a installable APK. And the class(dex files) would storage in /data/dalvik-cache


        String packageName = "com.android.calculator2";
        String className = "com.android.calculator.sample.class";

        String apkSourcePath;
        apkSourcePath =getPackageManager().getApplicationInfo(packageName, 0).sourceDir;

         PathClassLoader pathClassLoader = new dalvik.system.PathClassLoader(apkSourcePath,
                    ClassLoader.getSystemClassLoader());
          Class clazz = pathClassLoader.loadClass(className);
          Method[] methods = clazz.getMethods();
          for (Method m : methods) {
                Log.i("","method name:" + m.getName());
           }


DexClassLoader:
Load class from jar, apk file. Those file is stored in accessible storage, such as internal storage or sd card. When class loader loads jar file, it will generate dex file. Then load class from dex file.

Example:
           Put  com.android.calculator.jar in /data/data/MY_APP_PACKAGE/files    

            File jarFile = new File(getFilesDir().getAbsoluteFile() + File.separator
                    + "com.android.calculator.jar");
            final File optimizedDexOutputPath =getDir("outdex", Context.MODE_PRIVATE);
            DexClassLoader dexClassLoader = new DexClassLoader(jarFile.getAbsolutePath(),
                    optimizedDexOutputPath.getAbsolutePath(),
                    null,
                    ClassLoader.getSystemClassLoader());
            Class clazz = dexClassLoader.loadClass("com.android.calculator.sample.class");
            Method[] methods = clazz.getMethods();
            for (Method m : methods) {
                Log.i("","method name:" + m.getName());
            }
            Constructor constructor = clazz.getConstructor(Context.class);
            Object instance = constructor.newInstance();

Sunday, January 11, 2009

安裝android 開發環境

1.下載eclipse jee 3.41, android 1 R2, java SDK 6 update 11

2. 解壓縮eclipse jee 3.41, android 1 R2, 安裝java SDK 6 update 11

3. 在系統變數的path加入;<sdk_dir>/tools

4. 在eclipse中加入android的plug-in, plugin的網址為https://dl-ssl.google.com/android/eclipse或是直接下載ADT的壓縮檔

5. 在eclipse中window=>preference選 android 設定SDK location

6. 如果之前有安裝過eclipse,並安裝先前版本的JDK,還需要在window->preference選java->installed JREs 更改為JDK1.6

DDMS

1. 在eclipse右上角點選"Open persepctive"->other->DDMS,之後在啟動模擬之後,就可以在DDMS觀看狀況了

DDMS可以透過USB連上hardware device,需要安裝USB driver(android_usb_windows.zip)

android sample

1. eclipse中開新專案。開完,在專案中File-> import, 選General->File System,路徑設定為<sdk_dir>/samples/AmpDemos,即可。

2. 執行的時候可以在Run Configurations設定launch action為哪一隻程式碼

3. 如果出現Re-installation fail due to different application signature的錯誤訊息,可以在adroid-sdk底下的tools中執行"adb uninstall com.example.android.apis"。

March. 2011
1. updated 完 android sdk後,在Eclipse中執行AVD manager時出現"location of android sdk has not been set up in the preferences", 點選eclipse->preferences->android SDK location時出現"could not find folder'tools' inside sdk"

確定sdk以安裝,且設定安裝路徑後仍有此錯誤。在update ADT plugin for eclipse後,問題解決。所以SDK與ADT都需要一同更新。