読者です 読者をやめる 読者になる 読者になる

GooglePlayService(LibraryProject)のMultiDex対策メモ

ローカルでRoboSpock/テストライブラリ 辺りを試そうかなとおもったら

  • 容易にMultiDexが必要になる*1
  • testComple時のDex処理がクソ時間かかる

なのでなんでか調べてみたら、
ADT開発環境と共用しているGooglePlayService(以下GPSと略)のLibraryProjectの関数数の問題

ここらへんはQitta辺り見ても同じ話になっているようで


> 現在の仕事場の開発環境

  • 基本周りが開発しているのはADT
  • 個人的にローカル環境に以下の環境を構築している
    • git-svn
    • AS共用環境(GPSはLibaryProjectとして参照)

GPS

  • 地図とか表示しないなら基本Push通知しか使わないよなー

というのが結構定番な使われ方だったりする



試した方法1)

Qittaとかによく出てくる方法

上記サイトにリンクが有る記載元gistの方はだいぶ変更されていて
LibraryProjectとして導入されている場合は既に動かせない*2

  • strip_play_services.gradle
afterEvaluate { project ->
    android.applicationVariants.each { variant ->
        variant.javaCompile.dependsOn stripPlayServices
    }
}

task stripPlayServices << {
//    def playServiceRootFolder = new File(rootProject.buildDir, "intermediates/exploded-aar/com.google.android.gms/play-services/")
    def playServiceRootFolder = new File(rootProject.buildDir, "intermediates/exploded-aar/XXXX/unspecified")//☆環境確認の上修正


    playServiceRootFolder.list().each { versionName ->
        def versionFolder = new File(playServiceRootFolder, versionName)
        //TODO: classes.jarがないフォルダで作業するとエラーになるのでSkipする
        if(!new File(versionFolder, "classes.jar").exists())return;
        copy {
            from (file(new File(versionFolder, "classes.jar")))
            into (file(versionFolder))
            rename { fileName ->
                fileName = "classes_orig.jar"
            }
        }
        tasks.create(name: "strip" + versionName, type: Jar) {
            destinationDir = versionFolder
            archiveName = "classes.jar"
            from (zipTree(new File(versionFolder, "classes_orig.jar"))) {
                exclude "com/google/ads/**"
                exclude "com/google/android/gms/analytics/**"
                exclude "com/google/android/gms/games/**"
                exclude "com/google/android/gms/plus/**"
                exclude "com/google/android/gms/drive/**"
                exclude "com/google/android/gms/ads/**"
            }
        }.execute()
        delete file(new File(versionFolder, "classes_orig.jar"))//☆deleteタスクが落ちるので記述修正
    }
}



解決した方法)

上記参照のgistのコメントにも書いてあるけど

compile ('com.google.android.gms:play-services:7.3.+') {
        exclude group: "com/google/ads/**"
        exclude group: "com/google/android/gms/ads/**"
        exclude group: "com/google/android/gms/analytics/**"
        exclude group: "com/google/android/gms/appindexing/**"
        exclude group: "com/google/android/gms/appinvite/**"
        exclude group: "com/google/android/gms/appstate/**"
        exclude group: "com/google/android/gms/cast/**"
        exclude group: "com/google/android/gms/drive/**"
        exclude group: "com/google/android/gms/fitness/**"
        exclude group: "com/google/android/gms/games/**"
        exclude group: "com/google/android/gms/identity/**"
        exclude group: "com/google/android/gms/location/**"
        exclude group: "com/google/android/gms/maps/**"
        exclude group: "com/google/android/gms/nearby/**"
        exclude group: "com/google/android/gms/panorama/**"
        exclude group: "com/google/android/gms/plus/**"
        exclude group: "com/google/android/gms/safetynet/**"
        exclude group: "com/google/android/gms/wallet/**"
        exclude group: "com/google/android/gms/wearable/**"
    }

と書いてある記述を流用して下記と書けばOK。

compile (project(':google_play_services_froyo')) {
        exclude group: "com/google/ads/**"
        exclude group: "com/google/android/gms/ads/**"
        exclude group: "com/google/android/gms/analytics/**"
        exclude group: "com/google/android/gms/appindexing/**"
        exclude group: "com/google/android/gms/appinvite/**"
        exclude group: "com/google/android/gms/appstate/**"
        exclude group: "com/google/android/gms/cast/**"
        exclude group: "com/google/android/gms/drive/**"
        exclude group: "com/google/android/gms/fitness/**"
        exclude group: "com/google/android/gms/games/**"
        exclude group: "com/google/android/gms/identity/**"
        exclude group: "com/google/android/gms/location/**"
        exclude group: "com/google/android/gms/maps/**"
        exclude group: "com/google/android/gms/nearby/**"
        exclude group: "com/google/android/gms/panorama/**"
        exclude group: "com/google/android/gms/plus/**"
        exclude group: "com/google/android/gms/safetynet/**"
        exclude group: "com/google/android/gms/wallet/**"
        exclude group: "com/google/android/gms/wearable/**"
    }

ビルドの高速化も考えるなら

  • build.gradle
ext.findFirstFile = {rootDir,filter ->
	println "rootDir=$rootDir"
	println "filter=$filter"
	if(rootDir==null || !rootDir.exists()){
		return null
	}
	File result
	rootDir.traverse(
		type : groovy.io.FileType.FILES,
		nameFilter : filter
	) { it -> 
		//println it
		result = it
		groovy.io.FileVisitResult.TERMINATE
	}
	result
}

def aarDir = new 
File(project(':google_play_services_froyo').buildDir,"outputs/aar")
def hit_f = false
if(ndFirstFile(aarDir, ~/.*\.aar/)!=null){
    hit_f = true
    repositories{                                             
        flatDir{                                              
            dirs aarDir                                      
        }                                                     
    }        
}

dependencies {                                            

   // ==省略==

    if (hit_f) {
println "==== aar reference  ===="
       releaseCompile(name:'google_play_services_froyo-release', ext:'aar')         
       debugCompile(name:'google_play_services_froyo-debug', ext:'aar')             
    }
    else {
println "==== library project ===="
        compile (project(':google_play_services_froyo')) {
            exclude group: "com/google/ads/**"
            exclude group: "com/google/android/gms/ads/**"
            exclude group: "com/google/android/gms/analytics/**"
            exclude group: "com/google/android/gms/appindexing/**"
            exclude group: "com/google/android/gms/appinvite/**"
            exclude group: "com/google/android/gms/appstate/**"
            exclude group: "com/google/android/gms/cast/**"
            exclude group: "com/google/android/gms/drive/**"
            exclude group: "com/google/android/gms/fitness/**"
            exclude group: "com/google/android/gms/games/**"
            exclude group: "com/google/android/gms/identity/**"
            exclude group: "com/google/android/gms/location/**"
            exclude group: "com/google/android/gms/maps/**"
            exclude group: "com/google/android/gms/nearby/**"
            exclude group: "com/google/android/gms/panorama/**"
            exclude group: "com/google/android/gms/plus/**"
            exclude group: "com/google/android/gms/safetynet/**"
            exclude group: "com/google/android/gms/wallet/**"
            exclude group: "com/google/android/gms/wearable/**"
        }
    }
}        
  • settings.gradle
include ':google_play_services_froyo'
project(':google_play_services_froyo').projectDir = new File(settingsDir,'../google_play_services_lib')

*1:groovy-allとかでっかいライブラリつけているから当然

*2:libaryProjectにはバージョン番号なんて無いので 「unspecified」というフォルダが作成される為