Search code examples
reactjsandroid-studioionic-frameworkionic4ionic-react

Is it possible to create an apk in Ionic 4 - (React) via CLI only?


I want to generate an apk for an Ionic-React app, only using the command-line.

I am able to use Ionic Capacitor to make an apk via Android-Studio, but I want to purely use the CLI to generate the same apk. Is this possible to do? Thanks.


Solution

  • I have since solved this issue and am now able to build an Ionic React app apk on Azure dev-ops without having to interact with Android studio at all. Here is the YAML -

    Firstly - Building the Ionic project

    trigger:
    - master
    
    variables:
      scheme: ''
      sdk: 'iphoneos'
      configuration: 'Release'
    pool:
      vmImage: 'macos-latest'
    
    steps:
    - task: UseNode@1
      inputs:
        checkLatest: true
    - task: Npm@1
      inputs:
        command: 'install'
    - script: npm run citest
    - task: Npm@1
      inputs:
        command: 'custom'
        customCommand: 'install -g ionic'
    - task: CmdLine@2
      inputs:
        script: 'ionic build --prod'
    - task: PublishBuildArtifacts@1
      inputs:
        PathtoPublish: 'build'
        ArtifactName: 'drop'
        publishLocation: 'Container'
    

    Secondly - Building .apk

    - task: CmdLine@2
      inputs:
        script: 'ionic capacitor add android'
    - task: PythonScript@0
      inputs:
        scriptSource: 'filePath'
        scriptPath: 'changeGradle.py'
        arguments: '--version=$(Build.BuildId)'
    
    - task: CopyFiles@2
      inputs:
        SourceFolder: 'resources/androidResources/res'
        Contents: '**'
        TargetFolder: 'android/app/src/main/res'
        OverWrite: true
    - task: Gradle@2
      inputs:
        workingDirectory: 'android'
        gradleWrapperFile: 'android/gradlew'
        gradleOptions: '-Xmx3072m'
        javaHomeOption: 'JDKVersion'
        jdkVersionOption: '1.8'
        jdkArchitectureOption: 'x64'
        publishJUnitResults: true
        testResultsFiles: '**/TEST-*.xml'
        tasks: 'build'
    - task: AndroidSigning@3
      inputs:
        apkFiles: 'android/app/build/outputs/apk/release/*.apk'
        apksignerKeystoreFile: XXX.jks
        apksignerKeystorePassword: 'XXX'
        apksignerKeystoreAlias: 'XXX'
        apksignerKeyPassword: 'XXX'
        zipalign: true
    - task: CopyFiles@2
      inputs:
        Contents: '**/*'
        TargetFolder: '$(build.artifactStagingDirectory)'
    - task: PublishPipelineArtifact@1
      inputs:
        targetPath: '$(Build.artifactStagingDirectory)'
        publishLocation: 'pipeline'
    

    Publishing the .apk to Google Play internal track is handled in the Azure release pipeline (not build). The PythonScript command is a custom script I wrote to change the Gradlew build no. as no other solutions seemed to work for me.

    I hope this helps someone as this was a nightmare to figure out on my own!