1. 程式人生 > >cocos2d-x 3.X eclipse gradle 多渠道多SDK打包配置教程(二)

cocos2d-x 3.X eclipse gradle 多渠道多SDK打包配置教程(二)

根據上一篇的指導,應該已經可以出一個同資源同SDK的包了,現在看下在eclipse gradle下如何像Android studio一樣一鍵打出不同SDK接入的渠道包吧

首先一個專案一個專案的複製貼上到工程下,大概是這樣的

每個渠道都建立好build.gradle,裡面的配置資訊是這樣的

apply plugin: 'android'

//獲取當前時間的函式(年月日)
def releaseTime() {
    return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
}

//包入的jar包和libs
dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile project(':cocos2d-x:cocos:platform:android:java')
}

android {
    //簽名信息
	signingConfigs{
		myConfig{
			keyAlias 'xxxx'
			keyPassword 'xxxx'
			storeFile file('xxxx')
			storePassword 'xxxx'
		}
	}
    
    //簽名和輸出包的目錄和格式,這裡輸出的是test_v1_2018_10_29_test.apk到D:/xxxx/package目錄下
	buildTypes{
		release {
			signingConfig  signingConfigs.myConfig
			applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    def outputFile = output.outputFile
                    if (outputFile != null && outputFile.name.endsWith('.apk')) {
                        def fileName = "test_v${defaultConfig.versionName}_${releaseTime()}_test.apk"
                        output.outputFile = new File("D:/xxxx/package", fileName)
                    }
                }
            }
		}
	}
	
    compileSdkVersion 20
    buildToolsVersion "25.0.0"
    enforceUniquePackageName = false
	
    //配置在公共部分的包版本資訊
	defaultConfig {
    	versionCode project.VERSION_CODE as int
        versionName project.VERSION_NAME
    }
    
    lintOptions {
        checkReleaseBuilds false
        // Or, if you prefer, you can continue to check for errors in release builds,
        // but continue the build even when errors are found:
        abortOnError false
    }
	
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
            //這個很重要哦
            main.jniLibs.srcDirs = ['libs'] 
        }

        // Move the tests to tests/java, tests/res, etc...
        instrumentTest.setRoot('tests')

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
}

就這樣每個包都做這個操作。

如果要配置比如友盟的渠道資訊

在AndroidManifest.xml中

<meta-data
            android:name="UMENG_CHANNEL"
            android:value="${CHANNEL_VALUE}" />

替換,這裡CHANNEL_VALUE就是一個變數,在打包時候賦值,想要什麼渠道號就是什麼渠道號了

使用的話也簡單

android {
	productFlavors {
		_360Extend{}
		baiduExtend{}
    }
    productFlavors.all { flavor ->
    	flavor.manifestPlaceholders = [CHANNEL_VALUE: name]
    }

或者

android {
	productFlavors {
        //這裡渠道名不能是數字開頭,所以只能加一個下劃線了
		_360Extend{
			manifestPlaceholders = [CHANNEL_VALUE: "_360Extend"]
		}
		baiduExtend{
			manifestPlaceholders = [CHANNEL_VALUE: "baiduExtend"]
		}
    }

打包出來就行了

然後剛才公共部分的版本資訊是配置在這的

自己建立一個gradle.properties

裡面新增

就可以使用啦!

編譯的話,如果是所有專案一起編譯出apk的話,在根目錄也就是framework目錄下的settings.gradle 修改為

!!!這裡很重要!!!!

是settings.gradle!!!!

這個檔案是你編譯的配置資訊,你include的所有專案全在裡面,我也研究了好半天

我的是這樣的:

一個專案和一個專案用逗號隔開,然後最下面2個是cocos2d-x java的公共部分,每次編譯都需要的

騰訊應用寶有額外的library,我沒什麼辦法就一起逗號隔開進去了,壞處就是會多編譯一次library但是不會再apk目錄下有輸出

如果有什麼好的方法的話可以告訴我,謝謝

然後在根目錄framwork下,開啟命令列,執行gradle build

就會開始漫長的編譯過程了,結束就會這樣啦

大功告成!