简体   繁体   English

当我使用自定义 build.gradle 文件时,Android Studio 使用错误的文件路径到 apk

[英]Android studio uses wrong file path to apk when I use custom build.gradle file

So Im trying to use a custom build file that generates an unsigned apk then runs a task on it and then zipaligns it.因此,我尝试使用生成未签名 apk 的自定义构建文件,然后在其上运行任务,然后对其进行 zipalign。 I havent gotten that working yet.我还没有得到那个工作。 However I do have it creating and properly signing the apk but when I try to just push the play button to run it in debug on a device using my custom debug set up studio/gradle generates my apk and places it in the directory I want but it fails to push it on to the device because it uses a file path that doesnt exist.但是,我确实创建并正确签署了 apk,但是当我尝试按下播放按钮以在设备上使用我的自定义调试设置在调试中运行它时,studio/gradle 会生成我的 apk 并将其放置在我想要的目录中,但是它无法将其推送到设备上,因为它使用了不存在的文件路径。 I/O Error: C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Android Studio\\apk\\V2.5-10-debug-20140922-2335.apk (The system cannot find the path specified). I/O 错误:C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Android Studio\\apk\\V2.5-10-debug-20140922-2335.apk(系统找不到指定的路径)。 Is this hard coded into the studio build because I cant find a setting to change it.这是否硬编码到工作室版本中,因为我找不到更改它的设置。 Its really making the whole process a real pain lol.它真的让整个过程变得非常痛苦,哈哈。 Infact that path doesnt exist anywhere on my machine.事实上,我的机器上的任何地方都不存在该路径。

EDIT heres my build.gradle file编辑这里我的 build.gradle 文件

import java.text.SimpleDateFormat
import java.util.regex.Pattern

apply plugin: 'com.android.application'


def buildTime() {
    def df = new SimpleDateFormat("yyyyMMdd'-'HHmm")
    df.setTimeZone(TimeZone.getDefault())
    return df.format(new Date())
}

def apkName = "MyApp"
def apkLocation

task (runApkSigTool , dependsOn: android, type: JavaExec) {

    classpath files('apksigtool.jar')
    main 'com.widevine.tools.android.apksigtool.ApkSigTool'
    args[0] = apkLocation
    args[1] = 'private_key.der'
    args[2] = 'my.crt'
    System.out.println(apkLocation);
}



android {
    compileSdkVersion 19
    buildToolsVersion "19.1.0"


signingConfigs{

    debug{
        storeFile file("debug.keystore")
        storePassword "android"
        keyAlias "androiddebugkey"
        keyPassword "android"

    }

    release{
        storeFile file("debug.keystore")
        storePassword "android"
        keyAlias "androiddebugkey"
        keyPassword "android"

    }

}


buildTypes {

    debug{

    }

    release {

    signingConfig signingConfigs.release
    runProguard false
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

    assembleDebug.doLast{
       runApkSigTool.execute()
    }

    zipAlign true

}

debug{

    signingConfig signingConfigs.debug
    runProguard false
    proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

    assembleDebug.doLast{
        runApkSigTool.execute()
    }

    zipAlign false

}


android.applicationVariants.all { variant ->

    def manifestFile = file("C:\\path\\app\\src\\main\\AndroidManifest.xml")
    def pattern = Pattern.compile("versionName=\"(.+)\"")
    def manifestText = manifestFile.getText()
    def matcher = pattern.matcher(manifestText)
    matcher.find()
    def versionName = matcher.group(1)
    pattern = Pattern.compile("versionCode=\"(.+)\"")
    matcher = pattern.matcher(manifestText)
    matcher.find()
    def versionCode = matcher.group(1)

    if (variant.zipAlign) {
        variant.outputFile = new File("apk/"+apkName+"-V"+versionName+"-"+versionCode+"-"+variant.name+"-"+buildTime()+"-unaligned.apk");
        variant.zipAlign.inputFile = variant.outputFile
        variant.zipAlign.outputFile = new File("apk/"+apkName+"-V"+versionName+"-"+versionCode+"-"+variant.name+"-"+buildTime()+".apk");
    } else {

        apkLocation = "apk/"+apkName+"-V"+versionName+"-"+versionCode+"-"+variant.name+"-"+buildTime()+".apk";
        variant.outputFile = new File("apk/"+apkName+"-V"+versionName+"-"+versionCode+"-"+variant.name+"-"+buildTime()+".apk");

        System.out.println("CREATED UNSIGNED APK---------------")

    }

}

}
lintOptions {
    abortOnError false
    ignoreWarnings true
    checkAllWarnings false
 }



}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:20.0.0'
compile 'com.android.support:mediarouter-v7:20.0.0'
compile 'com.google.android.gms:play-services:5.0.89'
}

installDebug 在准备 adb push 命令时也会调用 buildTime() ,因此日期每分钟都会不匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM