您的位置:首页 > 编程语言

安卓 gradle代码混淆与打包

2018-01-05 00:26 405 查看
S1.使用默认方式打包签名APK

AndroidStudio选择Build => Generate Signed APK

总体步骤流程如下:

详细操作步骤:

S2. 多渠道打包APK

属性配置
productFlavors

    productFlavors {

        aVer {

            resValue("string","strKey","myKey")

            applicationId "com.simple.pkg.debug"

        }

        bVer {

            applicationId "com.simple.pkg.beta"

        }

        cVer {

            applicationId "com.simple.pkg.res"

        }

    }

signingConfigs

    signingConfigs {

        release {

            storeFile file('E:/XXX/app.jks') //
签名证书文件


            storePassword 'you password' //
证书密码


            keyAlias 'test'

            keyPassword 'you password '

        }

    }

buildTypes

    buildTypes {

        mbeta {

            signingConfig android.signingConfigs.release

            minifyEnabled false

        }

    }

 

S3.gradle打包自动化-定制apk文件名

gradle属性及变量定义的几种方式

变量定义
方式1(for gradle)

工程目录下gradle.properties定义属性:appName = MyAppName

App目录下build.gradle读取自定义属性:project.appName

方式2(for java)

App目录下build.gradle定义属性:

    productFlavors {

        aVer {

            // gradle定义全局变量方式2-代码使用

            buildConfigField "int", "varInt", "0"

            buildConfigField "String", "varString", '"abc"'

        }

}

Java代码读取属性:BuildConfig.varInt; BuildConfig.varString;

方式3(for java)

App目录下build.gradle定义属性:

    productFlavors {

        aVer {

            resValue("string","strKey","myKey")

        }

}

ava代码读取属性:context.getString(R.string.strKey);

方式4

App目录下build.gradle定义属性:

def flavorName = "undefine"

def indexFlavor(String str) {

    return str.substring(0,str.length())

}

方式5

App目录AndroidManifest.xml定义变量

        <meta-data

            android:name="env_key0"

            android:value="${env_val0}" />

build.gradle修改env_val0

    buildTypes {

        debug {

            minifyEnabled false

            manifestPlaceholders = [env_val0: "VAL0", env_val1: "VAL1"]

        }

    }

Java代码中获取meta属性定义

    public static String getMetaValue(Context context,String key){

        ApplicationInfo applicationInfo = null;

        try {

            applicationInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);

        } catch (PackageManager.NameNotFoundException e) {

            e.printStackTrace();

        }

        if (applicationInfo == null){

            return "";

        }

        return applicationInfo.metaData.getString(key);

    }

Ref:

    
androidstudio gradle 多版本多apk打包(打包系列教程之五)

    
Gradle自定义构建全局变量

    
androidstudio 自定义gradle变量

    
Android GradlemanifestPlaceholders 占位符详解

 

定制apk文件名

变量定义
自定义生成APK文件名

applicationVariants.all { variant ->

variant.outputs.each { output ->

def outputFile = output.outputFile

         // appName + version + createTime + svnVersion + flavor + buildType

         def fileName = "fileName.apk"

         output.outputFile = new File(outputFile.parent, fileName)

       }

    }

获取相关属性

AppName:自定义方式或getName()

Version:版本, defaultConfig.versionName

CreateTime:创建时间,new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))

SvnVersion :SVN提交版本号

Flavor :编译渠道版本


BuildType:debug/beta/release版本

def fileName = "${project.appName}_v${defaultConfig.versionName}_${releaseTime()}_" +

                            "s${getSvnRevision()}_${indexFlavor(productFlavors.name)}_${getName()}.apk"

 

 

Ref:

    
AndroidGradle实用技巧——APK文件名中加上SVN版本号,日期等

     Android使用Gradle打包APP名称和版本号

 

参考问题:

     buildTypes中加上signingConfigsigingConfigs.release运行就报错

   

build.gradle多渠道打包完整示例

// app/build.gradle文件

import org.tmatesoft.svn.core.wc.* // for svn version

apply plugin: 'com.android.application'

def releaseTime() {

    return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))

}

def indexFlavor(String str) {

    return str.substring(0,str.length())

}

def getSvnRevision() { // for svn

    ISVNOptions options = SVNWCUtil.createDefaultOptions(true);

    SVNClientManager clientManager = SVNClientManager.newInstance(options);

    SVNStatusClient statusClient = clientManager.getStatusClient();

    SVNStatus status = statusClient.doStatus(projectDir, false);

    SVNRevision revision = status.getCommittedRevision();

    return revision.getNumber();

}

android {

    signingConfigs {

    }

    compileSdkVersion 25

    buildToolsVersion "25.0.2"

    defaultConfig {

        applicationId "com.simple.res"

        minSdkVersion 15

        targetSdkVersion 21

        versionCode 1

        versionName "1.0"

        multiDexEnabled true    // dex突破65535的限制

    }

    productFlavors {

        aVer {

            // gradle定义全局变量方式2-代码使用

            buildConfigField "int", "varInt", "0"

            buildConfigField "String", "varString", '"abc"'

            //

            resValue("string","strKey","myKey")

            applicationId "com.simple.res.debug"

        }

        bVer {

            applicationId "com.simple.res.beta"

        }

    }

    signingConfigs {

        release {

            storeFile file('E:/XXX/App.jks')

            storePassword 'you password'

            keyAlias 'TEST'

            keyPassword 'you password'

        }

    }

    buildTypes {

        debug {

            minifyEnabled false

        }

        mbeta {

            signingConfig android.signingConfigs.release

            minifyEnabled false

        }

        release {

            signingConfig android.signingConfigs.release

            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

            minifyEnabled true

            shrinkResources true

            // 指定APK文件名

            applicationVariants.all { variant ->

                variant.outputs.each { output ->

                    def outputFile = output.outputFile

                    // appName + version + createTime + svnVersion + flavor + buildType

                    def fileName = "${project.appName}_v${defaultConfig.versionName}_${releaseTime()}_" + "s${getSvnRevision()}_${indexFlavor(productFlavors.name)}_${getName()}.apk"

                    output.outputFile = new File(outputFile.parent, fileName)

                }

            }

        }

    }

}

// project/build.gradle文件

// 添加classpath group: 'org.tmatesoft.svnkit', name: 'svnkit', version: '1.8.11'支持获取// SVN提交版本号

buildscript {

    repositories {

        jcenter()

    }

    dependencies {

        classpath 'com.android.tools.build:gradle:2.2.3'

        classpath group: 'org.tmatesoft.svnkit', name: 'svnkit', version: '1.8.11'

}

// project/build.gradle文件

appName = NewApp

Ref:

    
多渠道打包,生成不同包名的包

    
org.tmatesoft.svnkit
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  安卓 gradle 打包