본문 바로가기
Mobile/Android

Android 64K 메서드제한 오류 해결

by supdev 2017. 5. 13.

 구글, 페이스북 SNS 로그인을 구현하며 이 세가지 라이브러리만 추가했음에도 불구하고

얼마나 많은 메서드가 들어있는건지 64K 메서드제한에 걸린다는 에러 메시지가 출력되었습니다.

compile 'com.facebook.android:facebook-android-sdk:4.+'
compile 'com.google.android.gms:play-services-auth:9.0.0'
compile 'com.google.android.gms:play-services:9.0.0'

 이 에러를 해결하기 위해 에러 메시지와 함께 적혀있던 안드로이드 공식 사이트를 뒤적거리기 시작했습니다.


https://developer.android.com/studio/build/multidex.html#mdex-gradle


 페이지를 살펴보니 단일 DEX(Dalvik Executable) 바이트코드 파일 내에서 코드가 호출할 수 있는 참조의 총 개수가 65,536개이기 때문에 64K라는 에러를 뱉는다고 합니다. 이 페이지에서는 이를 해결하기 위해 (1)MultiDex를 활성화하여 메서드 제한을 늘리는 방법과 (2)코드를 축소를 활성화하여 64K 제한을 피하는 방법을 제시하고 있습니다. (안드로이드는 기본적으로 단일 DEX로 구현되는 듯 합니다.)



1. MultiDex 활성화하기


이 방법은 구글링을 통해 쉽게 접할 수 있는 해결책이었습니다.



1) minSdkVersion이 21 이상일 경우


MultiDex를 활성화하기 위해 minSdkVersion이 21 이상일 경우 모듈 수준의 build.gradle 파일에서 multiDexEnabled를 true로 설정하기만 하면 됩니다.


android {
    defaultConfig
{
       
...
        minSdkVersion
21
        targetSdkVersion
25
       
multiDexEnabled true
   
}
   
...
}


2) minSdkVersion이 20 이하일 경우


minSdkVersion이 20 이하로 설정되어 있을 경우 다음과 같이 multidex 지원 라이브러리를 추가로 설정해줍니다.

android {
    defaultConfig
{
       
...
        minSdkVersion
15
        targetSdkVersion
25
       
multiDexEnabled true
   
}
   
...
}

dependencies
{
 
compile 'com.android.support:multidex:1.0.1'
}


또한 매니페스트 파일을 편집하여 <application>태그에서 android:name을 다음과 같이 설정합니다.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   
package="com.example.myapp">
   
<application
           
android:name="android.support.multidex.MultiDexApplication" >
        ...
   
</application>
</manifest>


제 경우 이 방법이 적용되지 않아 코드 축소를 활성화하는 방법을 적용시켰습니다.



2. 코드 축소 활성화하기


(1) ProGuard로 코드 축소


ProGuard는 패키징된 앱에서 미사용 클래스, 필드, 메서드 및 특성을 감지하여 제거하고 여기에는 포함된 코드 라이브러리의 미사용 클래스, 필드, 메서드 및 특성도 포함됩니다.


ProGuard로 코드 축소를 활성화하려면 minifyEnabled true를 build.gradle 파일의 적절한 빌드 유형에 추가하면 됩니다.


유의할 점은, 코드 축소가 빌드 시간을 느리게 하므로 가능하면 디버그 빌드에서는 사용하지 말아야 합니다. 


또한, Android Studio는 Instant Run을 사용할 때 ProGuard를 비활성화 합니다. 증분 빌드에 코드 축소가 필요할 경우 실험용 Gradle 축소기를 사용해보라고 합니다.


android {
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'),
                    'proguard-rules.pro'
        }
    }
    ...
}


(2) 실험용 Gradle 축소기로 코드 축소


현재 저는 Android Studio로 Instant Run 기능을 이용하고 있기 때문에 ProGuard 대신 실험용 Gradle 축소기를 사용하였습니다.


Android 플러그인 축소기를 활성화하려면 '디버그;빌드 유형에서 useProguard를 false로 설정합니다.


android {
    buildTypes {
        debug {
            minifyEnabled true
            useProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'),
                    'proguard-rules.pro'
        }
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'),
                    'proguard-rules.pro'
        }
    }
}


'Mobile > Android' 카테고리의 다른 글

Android로 Node.js 서버에 GET, POST 요청하기  (2) 2017.05.14