Android-解放双手之Gradle自动化打包实战

转载文章

转载自

https://www.jianshu.com/p/38eb97d3477e#comment-27341369

参考文章

https://blog.csdn.net/bzlj2912009596/article/details/78188570
https://blog.csdn.net/yujihu989/article/details/54589684

这样配置下来就可以用命令行编译

但是第一篇文章中的那个脚本不是很通用

我修改了下



@echo off

cd .
set SIG_FILE="..\app\signing.properties"
del /F /Q %SIG_FILE%

echo #KEYSTORE_FILE=你的keystore文件位置> %SIG_FILE%
echo #KEYSTORE_PASSWORD= 你的keystore文件密码>> %SIG_FILE%
echo #KEY_ALIAS= 你的keystore文件用到的别名>> %SIG_FILE%
echo #KEY_PASSWORD= 你的keystore文件用到的别名的密码>> %SIG_FILE%
echo.>> %SIG_FILE%
echo.>> %SIG_FILE%

if "%1" equ "" goto LOOP1
echo "key_path=%1"
echo STORE_FILE = %1>> %SIG_FILE%
goto LOOP3

:LOOP1
echo "key_path=D:/key/home_cloud.jks"
echo STORE_FILE = D:/key/home_cloud.jks>> %SIG_FILE%
goto LOOP3


:LOOP3
echo STORE_PASSWORD = homecloudapp>> %SIG_FILE%
echo KEY_ALIAS = saming>> %SIG_FILE%
echo KEY_PASSWORD = homecloudapp>> %SIG_FILE%


echo "start package..."
cd ..
gradle a

echo "end package."

这些已经够用了,

大佬又做了一些修改

修改了下项目路径

App的gradle

apply plugin: 'com.android.application'


android {

    compileSdkVersion 27
    defaultConfig {
        applicationId "com.saming.homecloud"
        minSdkVersion 21
        targetSdkVersion 27
        versionCode 15
        versionName "5.3.1.0.15"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        manifestPlaceholders = [qqappid: "1106789123"]
        ndk {
            // 设置支持的SO库架构
            abiFilters 'armeabi', 'x86', 'armeabi-v7a', 'x86_4', 'arm64-v8a'
        }
        multiDexEnabled true
    }

    //签名信息配置
    signingConfigs {
        debug {
            v1SigningEnabled true
            v2SigningEnabled true
        }
        release {
            storeFile
            storePassword
            keyAlias
            keyPassword
            v1SigningEnabled true
            v2SigningEnabled true
        }
    }

    getSigningProperties()

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

        debug {
            debuggable true  //启用debug的buildType配置
        }

        //输出类型
        applicationVariants.all {
            //判断是release还是debug版本
            def buildType = it.buildType.name
            //获取当前时间的"YYYY-MM-dd"格式。
            def createTime = new Date().format("YYYY-MM-dd", TimeZone.getTimeZone("GMT+08:00"))
            //如果是正式包,将其输入到指定文件夹
            if (buildType == "release") {
                it.getPackageApplication().outputDirectory = new File('out_apk')
            }
        }
    }

    compileOptions {
        targetCompatibility 1.7
        sourceCompatibility 1.7
    }
    productFlavors {

    }
}
//读取签名配置文件
def getSigningProperties() {

    def propFile = file('signing.properties')
    if (propFile.canRead()) {
        def Properties props = new Properties()
        props.load(new FileInputStream(propFile))
        if (props != null && props.containsKey('STORE_FILE') && props.containsKey('STORE_PASSWORD') &&
                props.containsKey('KEY_ALIAS') && props.containsKey('KEY_PASSWORD')) {

            android.signingConfigs.release.storeFile = file(props['STORE_FILE'])
            android.signingConfigs.release.storePassword = props['STORE_PASSWORD']
            android.signingConfigs.release.keyAlias = props['KEY_ALIAS']
            android.signingConfigs.release.keyPassword = props['KEY_PASSWORD']

        } else {

            println 'signing.properties found but some entries are missing'
            android.buildTypes.release.signingConfig = null
        }
    } else {
        println 'signing.properties not found'
        android.buildTypes.release.signingConfig = null
    }
}

//获取版本号:
def getVersionCode() {
    def versionFile = file('version.properties')
    if (versionFile.canRead()) {
        def Properties versionProps = new Properties()
        versionProps.load(new FileInputStream(versionFile))
        def versionCode = versionProps['VERSION_CODE'].toInteger()
        def runTasks = gradle.startParameter.taskNames
        //仅在assembleRelease任务是增加版本号
        if ('assembleRelease' in runTasks) {
            versionProps['VERSION_CODE'] = (++versionCode).toString()
            versionProps.store(versionFile.newWriter(), null)
        }
        return versionCode
    } else {
        throw new GradleException("Could not find version.properties!")
    }
}

//获取当前时间
def getCurrentTime() {
    return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
}

dependencies {
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:recyclerview-v7:27.1.0'
    implementation 'com.android.support:design:27.1.0'
    implementation 'com.android.support:cardview-v7:27.1.0'
    implementation 'com.android.support:support-v4:27.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    //gson
    implementation 'com.google.code.gson:gson:2.8.2'
    //okhttp3
    implementation 'com.squareup.okhttp3:okhttp:3.8.1'
    implementation 'com.squareup.okio:okio:1.13.0'
    //glide
    implementation 'com.github.bumptech.glide:glide:4.6.1'
    //黄油刀
    implementation 'com.jakewharton:butterknife:8.8.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
    //jiaozi播放器
    implementation 'cn.jzvd:jiaozivideoplayer:6.2.8'
    //图片选择器
    implementation 'com.github.LuckSiege.PictureSelector:picture_library:v2.2.3'
    //文件选择器
    implementation 'com.leon:lfilepickerlibrary:1.7.0'
    //photoview
    implementation 'com.github.chrisbanes:PhotoView:1.2.6'
    //litepal数据库
    implementation 'org.litepal.android:core:2.0.0'
    //bugly
    implementation 'com.tencent.bugly:crashreport:2.1.5'
    //友盟分享
    implementation 'com.umeng.sdk:share-core:6.9.1'
    implementation 'com.umeng.sdk:share-qq:6.9.1.1'
    implementation 'com.umeng.sdk:share-wechat:6.9.1'
    implementation 'com.umeng.sdk:share-sina:6.9.1'
    implementation 'com.umeng.sdk:common:1.5.1'
    //leakcanary
    debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.5.4'
    releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4'
    debugImplementation 'com.github.whataa:pandora:v1.0.3'
    releaseImplementation 'com.github.whataa:pandora-no-op:v1.0.3'

    implementation 'com.google.android.exoplayer:exoplayer:2.7.1'
    implementation 'tv.danmaku.ijk.media:ijkplayer-java:0.8.8'
    implementation 'tv.danmaku.ijk.media:ijkplayer-armv7a:0.8.8'
    implementation 'tv.danmaku.ijk.media:ijkplayer-armv5:0.8.8'
    implementation 'tv.danmaku.ijk.media:ijkplayer-arm64:0.8.8'
    implementation 'tv.danmaku.ijk.media:ijkplayer-x86:0.8.8'
    implementation 'tv.danmaku.ijk.media:ijkplayer-x86_64:0.8.8'
    implementation 'tv.danmaku.ijk.media:ijkplayer-exo:0.8.8'

    implementation 'com.android.support:multidex:1.0.3'
}

新脚本代码

@echo off

cd .
set VER=Android_V5.3.1.0.%1
set OUTPUT=out_apk
set SIG_FILE="app\signing.properties"
del /F /Q %SIG_FILE%
rd /S /Q out_apk
rd /S /Q app\out_apk

:: Android_V5.3.1.0.15
if "%1" equ "" goto LOOP1
echo "version=%VER%"

@echo.>> %SIG_FILE%
@echo.>> %SIG_FILE%
@echo STORE_FILE = home_cloud.jks>> %SIG_FILE%
@echo STORE_PASSWORD = homecloudapp>> %SIG_FILE%
@echo KEY_ALIAS = saming>> %SIG_FILE%
@echo KEY_PASSWORD = homecloudapp>> %SIG_FILE%


echo "start package..."
call gradle a
goto LOOP_SUCCESS

:LOOP1
@echo "version is error!"
goto LOOP_END

:LOOP_SUCCESS
move /Y %OUTPUT%\app-release.apk %OUTPUT%\%VER%.apk
@echo "success"

:LOOP_END

在同级目录下,用命令行 就可生产apk

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值