Android NDKã¨Proguardã¯ç¸æ§ãæªã
Proguardã¨ã¯
ä»æ´èª¬æãããã¨ãç¡ããã¨æãã¾ãããProguardã¨ããã°Androiderã«ã¨ã£ã¦ã¯æ¬ ãããªããåéã§ãã
Javaã®ã³ã¼ãã¯å®¹æã«éã³ã³ãã¤ã«ããã¦ãç°¡åã«å
容ã解æããã¦ãã¾ãã¾ãã
éã³ã³ãã¤ã«èªä½ãé²ããã¨ã¯ã§ãã¾ããããã³ã¼ãã®è§£æãé£ãããã¦ããããã¼ã«ãProguardã®å¤§ããªå½¹å²ã§ãã Proguardã使ãç®çã¯ã次ã®ç¹ã大ããã§ãããã
- ã³ã¼ããé£èªåãã¦ç¬¬ä¸è ã«ãã解æãå°é£ã«ãã
- ä¸è¦ãªã³ã¼ããåé¤ãã¦ã³ã¼ãå ¨ä½ãå°ãããã
ã©ã¡ããã¡ã¤ã³ã«ãªããã¯éçºè æ¯ã«éãã§ããããããã®ã©ã¡ãããå½ã¦ã¯ã¾ãã®ã§ã¯ç¡ãã§ããããã
Proguradã®åé¡ç¹
Android SDKãä¸è¬çï¼ããªããæãç¯å²ãä¸è¬çã§ããä»äººã®åæãå¾ãããã¨ã¯éãã¾ããï¼ã«ä½¿ã£ã¦ããéããProguardã¯ã¨ã¦ãè¯ããã¼ããã¼ã§ãã
ã§ãããä¾ãã°æ¬¡ã®ãããªä½¿ãæ¹ãããå ´åãProguardã¯æã ã«ã±ã³ã«ã売ã£ã¦ãã¾ãã
- ãªãã¬ã¯ã·ã§ã³ãå©ç¨ãã¦ã¯ã©ã¹ãã¡ã½ããããã£ã¼ã«ããåç §ããå ´å
- JNIãå©ç¨ãã¦nativeã³ã¼ããå¼ã³åºããå ´å
ãªãã¬ã¯ã·ã§ã³ã¯ä½¿ããªã人ã¯å ¨ã使ããªãJavaã®æ©è½ã§ãããç°¡åã«èª¬æããã¨ãã¯ã©ã¹åããã¡ã½ããåãããã£ã¼ã«ãåããæååã¨ãã¦åç §ããå¶å¾¡ããæ¹æ³ã§ãã
JNIã¯C/C++ã§è¨è¿°ãããé¢æ°ããæååãã¨ãã¦ã©ã³ã¿ã¤ã ã§æ¤ç´¢ããå¼ã³åºãã¾ãã
Proguardã®ããã£ãã³ã¼ãã¯ã次ã®ç¹ã§è´å½çãªè´å½å·ãåãã¾ãã
- ã¯ã©ã¹åãå¤ãããã¦ãã¾ã
- ããã«ãã£ã¦ããªãã¬ã¯ã·ã§ã³ãåå¾ããããã«æå®ãããæååãã¨ãå®éã®ã¯ã©ã¹åãã«éããçãã¦ãã¾ã
- ä¾ãã°ã"com.eaglesakura.Hello"ã¨ããã¯ã©ã¹ããProguardãéããã¨ã§"com.eaglesakura.a"ã¨ããçãã¯ã©ã¹åã«å¤ãã£ã¦ãã¾ãã¾ãã
ããã°ã©ã ä¸ã§"com.eaglesakura.Hello"ã¯ã©ã¹ãæååã§æ¤ç´¢ãã¦ãè¦ã¤ãããªããªã£ã¦ãã¾ããã©ã³ã¿ã¤ã ã¨ã©ã¼ãçºçãã¾ãã
- NDKå´ã§ã®ã¿åç
§ãã¦ããã¯ã©ã¹ãã¡ã½ãããåé¤ããã¦ãã¾ã
- Proguardã¯å©ç¨ããã¦ããªãã¡ã½ããçããã§ãã¯ããã³ã¼ãããã¾ãã¾ãåé¤ãã¦ãã¾ãã¾ãã
- ãã®ãããNDKã§ãã使ã£ã¦ããªãã¯ã©ã¹ãã¡ã½ããããã£ãå ´åããä¸è¦ãªã¯ã©ã¹ãã¨å¤æããã¦åé¤ããã¦ãã¾ãæããããã¾ãã
ä»ååé¡ã«ãªã£ãæ
å人çã«ä½ã£ã¦ããiOS/Androidåæã³ã¼ãã£ã³ã°ç¨ã®ã©ã¤ãã©ãªã§ã¯ãJavaã®ã¯ã©ã¹ã«Annotationãã¤ãããã¨ã§Javaå´ã®ã³ã¼ããå¼ã³åºãC++ã³ã¼ããèªååºåãã¦ãã¾ãã
@JCClassã¯ã³ã³ãã¼ã¿ã«ãå¤æ対象ã¯ã©ã¹ãã§ãããã¨ãç¥ãããããã®ç®å°ã§ã@JCMethodã¯ãå¤æ対象ã¡ã½ãããã§ãããã¨ã示ãããã®ç®å°ã§ãã
Javaå´ã³ã¼ã ã¯ã©ã¹ã«"JCClass"ãã¡ã½ããã«"JCMethod"ã®annotationãã¤ãã¦ãã¾ãã @JCClass class Hoge { @JCMethod static void hello() { System.out.println("hello world!!"); } }
çæãããC++ã³ã¼ãï¼Hello.cppæç²ï¼ Helloã¯ã©ã¹ãèªåçã«ä½æããã¾ãã const ::jc::charactor* Hello::CLASS_SIGNATURE = "com/eaglesakura/Hello"; static jclass class_Hello = NULL; static jmethodID methods_Hello[1]; static void initialize_Hello() { // loaded ! if (class_Hello) { return; } CALL_JNIENV(); // load class object class_Hello = env->FindClass(Hello::CLASS_SIGNATURE); class_Hello = (jclass)::ndk::change_globalref(env, class_Hello); // load methods { methods_Hello[0] = ::ndk::JniWrapper::loadMethod(class_Hello, "hello", "()V", true); } } void Hello::hello() { CALL_JNIENV(); initialize_Hello(); env->CallStaticVoidMethod(class_Hello, methods_Hello[0]); }
C++å´ã®ã³ã¼ããè¦ãã¨ã"com/eaglesakura/Hello"ã¨ããã¯ã©ã¹åã»ããã±ã¼ã¸åã示ããã·ã°ããã£ãã"loadMethod(class_Hello, "hello", "()V", true);"ã¨ããJavaã®ã¡ã½ããæ¤ç´¢é¨åãçæããã¦ããã®ããããã¾ãã
Proguardãããã¦ãã¾ãã¨ãã¯ã©ã¹åã®å´©å£ã«ãã£ã¦ãããã®æååã§ã¯å¼ã³åºããªããªã£ã¦ãã¾ãã¾ãã ã¾ããhello()ãJavaå´ã§åç §ããã¦ããªãå ´åãæé©åã¨ç§°ãã¦ã¾ãã£ã¨åé¤ããã¦ããæãããããã¾ãã
ãããå¼ã³åºãã¯ãæååãã§è¡ãªã£ã¦ããé½åä¸ãã³ã³ãã¤ã«æã«è¦ã¤ãããã¨ã¯åºæ¥ããå®è¡æã«ãããããã¾ããã
ã¿ãã®æªããã¨ã«ãAndroid JUnitã¯Debugç¶æ ã§ä½æããã¦Proguardããã¦ããªããããJenkinsã®èªåãã«ãã§ã¯çºè¦ãããã¨ãå°é£ã§ãã
åé¡è§£æ±ºã®ããã«
ãããããæ¬é¡ã§ãã
ä¸è¨ã®åé¡ã解決ããããã«ã¯Proguardã§ã¯ã©ã¹åãã¡ã½ããåãå¤ãããªãããã«ããªããã°ãªãã¾ããã
幸éã«ãä»ååé¡ã«ãªã£ãã©ã¤ãã©ãªã®å ´åã ã¨ãèªååºåã®ããã«å¿
ãç¹å®ã®annotationãã¤ãããå¿
è¦ããã£ããããã©ãã«ããç¹å®ã®annotationã®ä»ããã¯ã©ã¹ãã¡ã½ãããProguard対象ããå¤ããã¨ããã¿ã¤ãã«ã®ãããªãã¨ãç¡ããã¨è³æãæ¼ã£ã¦ã¿ã¾ããã
@sys1yagiããã®ããã°ã«Annotationæå®ã§Proguardåé¿ããè¨è¿°ããã£ãã®ã§ããããå ã«æ¬¡ã®æ¡ä»¶ã§Proguardãåé¿ããè¨å®ã追å ãã¾ããã
- @JCClassã®Annotationãã¤ããã¯ã©ã¹ã¯ãªãã¼ã ãããªã
- @JCMethodãã¤ããã¡ã½ããã¯ãªãã¼ã ãããªã
- @JCMethodãã¤ããã¡ã½ããã¯æé©åã§åé¤ãããªã
ä»åproguard.cfgã«è¿½å ããé¨å # jointcoding # keep annotations -keep class com.eaglesakura.lib.jc.annotation.jnimake.** -keep @com.eaglesakura.lib.jc.annotation.jnimake.JCClass class * -keep @com.eaglesakura.lib.jc.annotation.jnimake.JCMethod class * # keep Java class names -keepnames @com.eaglesakura.lib.jc.annotation.jnimake.JCClass class * -keep class * implements @com.eaglesakura.lib.jc.annotation.jnimake.JCClass * # keep Java unused methods -keepclassmembers @com.eaglesakura.lib.jc.annotation.jnimake.JCClass class * { @com.eaglesakura.lib.jc.annotation.jnimake.JCMethod *; } # keep Java method names -keepclassmembernames class * { @com.eaglesakura.lib.jc.annotation.jnimake.JCMethod *; }
解説
次ã®ï¼è¡ã§Annotationèªä½ãProguardããå®ãã¾ãã
-keep class com.eaglesakura.lib.jc.annotation.jnimake.** -keep @com.eaglesakura.lib.jc.annotation.jnimake.JCClass class * -keep @com.eaglesakura.lib.jc.annotation.jnimake.JCMethod class *
次ã®ï¼è¡ã§ã@JCClassã®ã¤ããã¯ã©ã¹ã®å称å¤æ´ãåé¿ãã¾ãã
-keepnames @com.eaglesakura.lib.jc.annotation.jnimake.JCClass class * -keep class * implements @com.eaglesakura.lib.jc.annotation.jnimake.JCClass *
æå¾ã«ã@JCMethodã®ã¤ããã¡ã½ãããå称å¤æ´ã»æé©ã«ããåé¤å¯¾è±¡ããå¤ãã¾ãã
# keep Java unused methods -keepclassmembers @com.eaglesakura.lib.jc.annotation.jnimake.JCClass class * { @com.eaglesakura.lib.jc.annotation.jnimake.JCMethod *; } # keep Java method names -keepclassmembernames class * { @com.eaglesakura.lib.jc.annotation.jnimake.JCMethod *; }
ã¾ã¨ã
Proguardã¯æ¬²ããæ¡ä»¶ã§ã¡ããã¨ãã£ã¦ããããã ãã©ãæå®ããããã®æ¡ä»¶ä»ããè¦ã¤ããã¾ã§ãè¦å´ãããã(´ã»Ïã»ï½)