《Gradle权威指南》--自定义Android Gradle工程

Posted 嘉禾世兴

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了《Gradle权威指南》--自定义Android Gradle工程相关的知识,希望对你有一定的参考价值。

No1:

minSdkVersion

public void minSdkVersion(int minSdkVersion){
    setMinSdkVersion(minSdkVersion);
}
-----------------------
public void setMinSdkVersion(@Nullable String minSdkVersion){
    setMinSdkVersion(getApiVersion(minSdkVersion));
}

public void minSdkVersion(@Nullable String minSdkVersion){
    setMinSdkVersion(minSdkVersion);
}

No2:

android代号版本对应表

No3:

versionCode-内部版本号

@NonNull
public ProductFlavor setVersionCode(Integer versionCode){
    mVersionCode = versionCode;
    return this;
}

@Override
@Nullable
publid Integer getVersionCode(){
    return mVersionCode;
}

No4:

versionName-版本名称

@NonNull
public ProductFlavor setVersionName(String versionName){
    mVersionName = versionName;
    return this;
}

@Override
@Nullable
public String getVersionName(){
    return mVersionName;
}

No5:

testApplicationId-app的包名

@NonNull
public ProductFlavor setTestApplicationId(String applicationId){
    mTestApplicationId = applicationId;
    return this;
}

@Override
@Nullable
public String getTestApplicationId(){
    return mTestApplicationId;
}

No6:

testInstrumentationRunner-配置单元测试时使用的Runner

@NonNull
public ProductFlavor setTestInstrumentationRunner(String testInstrumentationRunner){
    mTestInstrumentationRunner = testInstrumentationRunner;
    return this;
}

@Override
@Nullable
public String getTestInstrumentationRunner(){
    return mTestInstrumentationRunner();
}

No7:

signingConfig-配置默认的签名信息

@Override
@Nullable
public SigningConfig getSigningConfig(){
    return mSigningConfig;
}

@NonNull
public ProductFlavor setSigningConfig(SigningConfig signingConfig){
    mSigningConfig = signingConfig;
    return this;
}

No8:

proguardFile-混淆所使用的配置文件

public void projuardFile(@NonNull Object projuardFile){
    getProjuardFiles().add(project.file(projuaardFile));
}

No9:

proguardFiles-同时接受多个配置文件

public void proguardFiles(@NonNull Object...proguardFileArray){
    getProguardFiles().addAll(project.files(proguardFileArray).getFiles());
}

No10:

配置签名信息

signingConfigs{
    release{
        storeFile file("myreleasekey.keystore")
        storePassword "password"
        keyAlias "MyReleaseKey"
        keyPassword "password"
    }
    debug{
        storeFile file("mydebugkey.keystore")
        storePassword "password"
        keyAlias "MyDebugKey"
        keyPassword "password"
    }
}

storeFile:签名证书文件

storePassword:签名证书文件的密码

storeType:签名证书的类型

keyAlias:签名证书中秘钥别名

keyPassword:签名证书中该秘钥的密码

注:debug模式的签名已经被配置好了,使用的就是android sdk自动生成的debug证书,一般位于../.android/debug.keystore

调用

defaultConfig{
    applicationId "org.flysnow.app.example82"
    minSdkVersion 14
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
    signingConfig signingConfig.debug
}

或者

buildType{
    release{
        signingConfig signingConfigs.release
    }
    debug{
        signingConfig signingConfig.debug
    }
}

No11:

applicationIdSuffix-用于配置基于默认applicationId的后缀

@NonNull
public BaseConfigImpl setApplicationIdSuffix(@Nullable String applicationIdSuffix){
    mApplicationIdSuffix = applicationIdSuffix;
    return this;
}

@Override
@Nullable
public String getApplicationIdSuffix(){
    return mApplicationIdSuffix;
}

No12:

debuggable-用于配置是否生成一个可供调试的apk

@NonNull
public BuildType setDebuggable(boolean debuggable){
    mDebuggable = debuggable;
    return this;
}

@Override
public boolean isDebuggable(){
    return mDebuggable || mTestCoverageEnabled;
}

No13:

jniDebuggable-用于配置是否生成一个可供调试Jni代码的apk

@NonNull
public BuildType setJniDebuggable(boolean jniDebugBuild){
    mJniDebuggable = jniDebugBuild;
    return this;
}

@Override
public boolean isJniDebuggable(){
    return mJniDebuggable;
}

No14:

minifyEnabled-用于配置是否启用Proguard混淆

@NonNull
public BuildType setMinifyEnabled(boolean enabled){
    mMinifyEnabled = enabled;
    return this;
}

@Override
public boolean isMinifyEnabled(){
    return mMinifyEnabled;
}

No15:

multiDexEnabled-用于配置该BuildType是否启用自动拆分多个Dex的功能

@Override
@Nullable
public Boolean getMultiDexEnabled(){
    return mMultiDexEnabled;
}

public void setMultiDexEnabled(@Nullable Boolean multiDex){
    mMultiDexEnabled = multiDex;
}

No16:

proguardFile

@NonNull
public BuildType projuardFile(@NonNull Object proguardFile){
    getProguardFiles().add(project.file(proguardFile));
    return this;
}

No17:

proguardFiles

@NonNull
public BuildType projuardFiles(@NonNull Object... proguardFileArray){
    getProguardFiles().addAll(project.files(proguardFileArray).getFiles());
    return this;
}

No18:

shrinkResources

@Override
public boolean isShrinkResources(){
    return thrinkResources;
}

public void setShrinkResources(boolean shrinkResources){
    this.shrinkResources = shrinkResources;
}

No19:
signingConfig

@NonNull
public BuildType setSigningConfig(@Nullable SigningConfig signingConfig){
    mSigningConfig = signingConfig;
    return this;
}

@Override
@Nullable
public SigningConfig getSigningConfig(){
    return mSigningConfig;
}

No20:

每一个BuildType都会生成一个SourceSet,还会生成相应的assemble任务,比如:assembleRelease或assembleDebug

No21:

代码混淆不仅能优化我们的代码,让apk包变得更小,还可以混淆我们原来的代码

No22:

public File getDefaultProjuardFile(String name){
    File sdkDir = sdkHandler.getAndCheckSdkFolder();
    return new File(sdkDir,SdkConstants.FD_TOOLS + File.separatorChar + SdkConstants.FD_PROGUARD + File.separatorChar + name);
}

android sdk默认提供两个Proguard配置文件,proguard-android.txt和proguard-android-optimize.txt,一个没有优化,一个有优化。

No23:

android{
    buildTypes{
        release{
            zipAlignEnabled true
        }
        debug{
            
        }
    }
}
@NonNull
public BuildType setZipAlignEnabled(boolean zipAlign){
    mZipAlignEnabled = zipAlign;
    return this;
}

@Override
public boolean isZipAlignEnabled(){
    return mZipAlignEnabled;
}

 

以上是关于《Gradle权威指南》--自定义Android Gradle工程的主要内容,如果未能解决你的问题,请参考以下文章

自定义 Android Gradle -- 启动 zipalign 优化

《Gradle权威指南》--Android Gradle多渠道构建

读书笔记--Android Gradle 权威指南(上)

Android Gradle开发指南

《Gradle权威指南》--Gradle任务

实现闭包(摘录自 权威指南184页)