2014年3月27日 星期四

Compile APK with static java lib or java lib in AOSP

1. Create a folder("Myproject") under ANDROID_ROOT/packages/apps/
2. Before compile APK. Make sure compile all reference library first.
3.1 Part of make file example with local_java_library
 
     LOCAL_JAVA_LIBRARIES := com.sample.mylocallib

3.2 Part of make file example with local_static_java_library
   
    LOCAL_STATIC_JAVA_LIBRARIES := com.sample.mystaticlib
    LOCAL_PROGUARD_ENABLED := disabled

4. Proguard will remove unreferenced code by default. So if APK want keep all code, it may need to set proguard disabled. Or enable proguard and keep class in proguard setting
 
     Make file example:
     LOCAL_PROGUARD_ENABLED := custom
     LOCAL_PROGUARD_FLAG_FILES := proguard.flags

     proguard.flags example:
     -keep class com.sample.mystaticlib.** { *; }
     -keep interface com.sample.mystaticlib.interface.** { *; }

5. If get error "can't find xxxx/classes.jar" when compile APK. It may add classpath in make file
   
   Make file example:
    LOCAL_CLASSPATH := out/target/common/obj/JAVA_LIBRARIES/libmyinterface_intermediates/classes.jar

create static JAVA lib jar for other APK reference

static_java_library 和 java_library不同點:
1. java_library 編譯完會output到 ANDROID_ROOT/out/target/product/generic/system/framework,而static_java_library不會。
2. 當APK reference到java_library時,不會把jar source編譯進APK。但是static_java_library會編譯進去, 所以執行該APK的機器不需要有static_java_library存在。
3. 編譯兩種library的方法一樣,只差在make file

Make file example:

ifneq ($(TARGET_BUILD_JAVA_SUPPORT_LEVEL),)
LOCAL_PATH := $(call my-dir)
# the library
# ============================================================
include $(CLEAR_VARS)
LOCAL_SRC_FILES := $(call all-subdir-java-files)
LOCAL_MODULE_TAGS := optional
# This is the target being built.
LOCAL_MODULE:= com.mylib.staticlibrary
include $(BUILD_STATIC_JAVA_LIBRARY)
# the documentation
# ============================================================
include $(CLEAR_VARS)
LOCAL_SRC_FILES := $(call all-subdir-java-files) $(call all-subdir-html-files)
LOCAL_MODULE:= mystaticlibraryDoc
LOCAL_DROIDDOC_OPTIONS := com.mylib.staticlibrary
LOCAL_MODULE_CLASS := JAVA_LIBRARIES
LOCAL_DROIDDOC_USE_STANDARD_DOCLET := true
include $(BUILD_DROIDDOC)
endif # JAVA_SUPPORT

2014年3月14日 星期五

custom JAVA library JAR in Android devices

Use custom library JAR in Android devices(Kitkat)
1. Create "MySystemLib"folder under AOSP_ROOT/device/sample/frameworks/MySystemLib
2. Put JAR source code in  AOSP_ROOT/device/sample/frameworks//MySystemLib
    Should include forder "java", Android.mk, com.my.internal.lib.xml
 
    Android.mk:
ifneq ($(TARGET_BUILD_JAVA_SUPPORT_LEVEL),)
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := \
            $(call all-subdir-java-files)
LOCAL_MODULE_TAGS := optional
# This is the target being built.
LOCAL_MODULE:= com.my.internal.lib
include $(BUILD_JAVA_LIBRARY)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := $(call all-subdir-java-files) $(call all-subdir-html-files)
LOCAL_MODULE:= LauncherFacadeInterface
LOCAL_DROIDDOC_OPTIONS := com.sprint.internal.idinterface
LOCAL_MODULE_CLASS := JAVA_LIBRARIES
LOCAL_DROIDDOC_USE_STANDARD_DOCLET := true
include $(BUILD_DROIDDOC)
endif # JAVA_SUPPORT

3. "make  com.my.internal.lib" or "mm -B"
4.  com.my.internal.lib.jar is generated in 'out/target/product/generic/system/framework'
5. Put com.my.internal.lib.jar  in /system/framework in android devices
6. Put com.my.internal.lib.xml in /system/framework/etc/permission in android devices
7.  Reboot device to refresh system lib
8. Declare lib in manifest.xml
   
            android:name="com.my.internal.lib"
             />

2014年3月7日 星期五

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();