Android Studio fails to start app after build when using a timestamp in the name

前端 未结 1 1028
谎友^
谎友^ 2021-02-03 10:26

I am having an issue with gradle and Android Studio, which only appears when building in Android Studio (BuildServer and Commandline work just fine)

applicationV         


        
1条回答
  •  清歌不尽
    2021-02-03 11:19

    This is normal, though unfortunate.

    When Studio opens the project, it queries Gradle for the model of the project. This includes the list of modules and for each module, their source folders, their dependencies, and the output of their build. In this case, the APK.

    So when Studio queries Gradle for the model, our plugin will build the model, which includes running your custom code that renames the APK filename. This is then sent to Studio which records it.

    However whenever you build, Studio will tell Gradle to build but not pass it any other information. Gradle will actually re-create the model again, and run your code again.

    This means that at every build the APK file name is different (Since your APK filename contains the date to the second), but none of them match the filename created during the project import. This makes the deployment fail.

    There is no way right now to have Gradle send Studio the filename of the generated APK.

    Edit: A better way to do this would be to keep the current output but copy it in a new file for safekeeping.

    I would do something like this:

    android.applicationVariants.all { variant ->
       def file = variant.output
    
       // create the new task
       def copyTask = project.tasks.create("copy${variant.name}Apk", Copy)
       copyTask.from = file
       copyTask.into = file.parent
       copyTask.rename("app-", getDate() + "_myapp_" + getGitCommit() +"_")
    
       // set up task dependencies
       // first make the assemble task depend on copyTask to make sure it gets called.
       variant.assemble.dependsOn copyTask
    
       // then make copyTask depend on the actual packaging task.
       copyTask.dependsOn variant.packageApplication
    }
    

    Note that the copy task expect a folder as destination so we have to use a rename rule.

    0 讨论(0)
提交回复
热议问题