shwenzhang / AndResGuard

proguard resource for Android by wechat team
Apache License 2.0
8.54k stars 1.54k forks source link

请教个关于finalApkBackupPath的问题 #202

Closed aolphn closed 7 years ago

aolphn commented 7 years ago

这个apk名字怎么把flavor值给配置进去呢?

simpleton commented 7 years ago

@zhangkehu 不是特别明确是的问题,你是想自己重新赋值这个BackupPath并带有flavorName么?如果是得到当前的flavorName可以参考

如果不配置这个值,本身AndResGuard也会根据flavors来生成不同的apk的。

aolphn commented 7 years ago

@simpleton 多谢了,我重新制定了finalApkBackupPath的全路径,就是不知道怎么在那里获取flavor。在你给出的链接的基础上稍微改了一下,我验证了一下可以获取到flavor了,这里贴一下,希望其他需要的人也能用到:

def getCurrentFlavor() {
    Gradle gradle = getGradle()
    String  tskReqStr = gradle.getStartParameter().getTaskRequests().toString()
    Pattern pattern;
    if( tskReqStr.contains( "assemble" ) ){
        pattern = Pattern.compile("assemble(\\w+)(Release|Debug)")
    }else if(tskReqStr.contains( "resguard" )){
        pattern = Pattern.compile("resguard(\\w+)(Release|Debug)")
    }
    else
        pattern = Pattern.compile("generate(\\w+)(Release|Debug)")

    Matcher matcher = pattern.matcher( tskReqStr )
    if( matcher.find() )
        return matcher.group(1).toLowerCase()
    else
    {
        println "NO MATCH FOUND"
        return "";
    }
}

上面是获取flavor的方法,需要在build.gradle文件里导入两个java的包,如下,写在文件开头部分即可:

import java.util.regex.Matcher
import java.util.regex.Pattern

具体使用:

def currentFlavor = getCurrentFlavor()
    finalApkBackupPath = "${project.rootDir.getParentFile()}/AndResGuard/" +
            "${rootProject.ext.appVersionName}-${rootProject.ext.gitVersionCode}-${currentFlavor}-AG.apk"
simpleton commented 7 years ago

十分感谢给出完整方案。

On Wed, Sep 27, 2017 at 7:15 PM, zhangkehu notifications@github.com wrote:

@simpleton https://github.com/simpleton 嗯,我重新制定了finalApkBackupPath的全路径, 就是不知道怎么在那里获取flavor。在你给出的链接的基础上稍微改了一下,我验证了一下可以获取到flavor了,这里贴一下, 希望其他需要的人也能用到:

`def getCurrentFlavor() { Gradle gradle = getGradle() String tskReqStr = gradle.getStartParameter().getTaskRequests().toString()

Pattern pattern;

if( tskReqStr.contains( "assemble" ) ){ pattern = Pattern.compile("assemble(\w+)(Release|Debug)") }else if(tskReqStr.contains( "resguard" )){ pattern = Pattern.compile("resguard(\w+)(Release|Debug)") } else pattern = Pattern.compile("generate(\w+)(Release|Debug)")

Matcher matcher = pattern.matcher( tskReqStr )

if( matcher.find() ) return matcher.group(1).toLowerCase() else { println "NO MATCH FOUND" return ""; }

}` 上面是获取flavor的方法,需要在build.gradle文件里导入两个java的包,如下,写在文件开头部分即可:

import java.util.regex.Matcher import java.util.regex.Pattern

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/shwenzhang/AndResGuard/issues/202#issuecomment-332707574, or mute the thread https://github.com/notifications/unsubscribe-auth/AAbvYnNwoB2_BOtUbcHyIfQQ1HLttC6Vks5smwFJgaJpZM4PlhhU .