name: vntapp-Android

on:
   workflow_dispatch:
    inputs:
      password:
        description: '请输入签名证书密码：（不支持中文密码，且最少6位字符）'
        required: true
        default: 'password'
      so:
        description: '是否重新打包安卓.so'
        required: true
        default: false
        type: boolean
      repo:
        description: '请输入vntAPP源码仓库'
        required: true
        default: 'vnt-dev/VntApp'
      tag:
        description: '请输入仓库分支或版本号'
        required: true
        default: 'master'
env:
  CARGO_TERM_COLOR: always
  TZ: Asia/Shanghai
  password: "${{ github.event.inputs.password }}"
  repo: "${{ github.event.inputs.repo }}"
  tag: "${{ github.event.inputs.tag }}"
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: 设置JDK环境
        uses: actions/setup-java@v4
        with:
          distribution: 'zulu'
          java-version: 17
          
      - name: 设置NDK环境
        if: github.event.inputs.so == 'true'
        run: |
           wget -q https://dl.google.com/android/repository/android-ndk-r26d-linux.zip
           unzip -q android-ndk-r26d-linux.zip -d /opt/ndk
          
      - name: 设置Flutter环境
        uses: subosito/flutter-action@v2
        with:
         channel: stable  

      - name: 设置cargo环境
        if: github.event.inputs.so == 'true'
        run: |
           rustup target add aarch64-linux-android
           rustup target add armv7-linux-androideabi
           rustup target add x86_64-linux-android
           rustup target add i686-linux-android
         
      - name: 克隆源码
        run: git clone -b "${{ env.tag }}" "https://github.com/${{ env.repo }}" /opt/vnt
           
      - name: 构建安卓so库
        if: github.event.inputs.so == 'true'
        run: |
           cd /opt/vnt/rust
           rm -rf .cargo/config
           export PATH=/opt/ndk/android-ndk-r26d:/opt/ndk/android-ndk-r26d/toolchains/llvm/prebuilt/linux-x86_64/bin:$PATH  
           cat >>./.cargo/config <<EOF
            [target.armv7-linux-androideabi]
            ar = "/opt/ndk/android-ndk-r26d/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-ar"
            linker = "/opt/ndk/android-ndk-r26d/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi21-clang"

            [target.aarch64-linux-android]
            ar = "/opt/ndk/android-ndk-r26d/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-ar"
            linker = "/opt/ndk/android-ndk-r26d/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android21-clang"

            [target.i686-linux-android]
            ar = "/opt/ndk/android-ndk-r26d/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-ar"
            linker = "/opt/ndk/android-ndk-r26d/toolchains/llvm/prebuilt/linux-x86_64/bin/i686-linux-android21-clang"

            [target.x86_64-linux-android]
            ar = "/opt/ndk/android-ndk-r26d/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-ar"
            linker = "/opt/ndk/android-ndk-r26d/toolchains/llvm/prebuilt/linux-x86_64/bin/x86_64-linux-android21-clang"
           EOF
           
           cargo build --target aarch64-linux-android --release
           mv -f /opt/vnt/rust/target/aarch64-linux-android/release/*.so /opt/vnt/android/app/src/main/jniLibs/arm64-v8a/librust_lib_vnt_app.so
          
           cargo clean
           cargo build --target x86_64-linux-android --release
           mv -f /opt/vnt/rust/target/x86_64-linux-android/release/*.so /opt/vnt/android/app/src/main/jniLibs/x86_64/librust_lib_vnt_app.so
          
           cargo clean
           export CC_i686_linux_android="/opt/ndk/android-ndk-r26d/toolchains/llvm/prebuilt/linux-x86_64/bin/i686-linux-android21-clang"
           export CC="/opt/ndk/android-ndk-r26d/toolchains/llvm/prebuilt/linux-x86_64/bin/i686-linux-android21-clang"
           cargo build --target i686-linux-android --release 
           mv -f /opt/vnt/rust/target/i686-linux-android/release/*.so /opt/vnt/android/app/src/main/jniLibs/x86/librust_lib_vnt_app.so

           cargo clean
           export CC_armv7_linux_androideabi="/opt/ndk/android-ndk-r26d/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi21-clang"
           export CC="/opt/ndk/android-ndk-r26d/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi21-clang"
           cargo build --target armv7-linux-androideabi --release 
           mv -f /opt/vnt/rust/target/armv7-linux-androideabi/release/*.so /opt/vnt/android/app/src/main/jniLibs/armeabi-v7a/librust_lib_vnt_app.so

      - name: 构建安卓APK
        run: |
         cd /opt/vnt

         sed -i 's|7.6.3|8.0|' ./android/gradle/wrapper/gradle-wrapper.properties
         sed -i 's|7.3|8.1|' ./rust_builder/android/build.gradle
         sed -i 's|flutter.compileSdkVersion|34|' ./android/app/build.gradle
         sed -i 's|flutter.targetSdkVersion|34|' ./android/app/build.gradle

         flutter pub get
         flutter upgrade
         flutter build apk --release
         find /opt/vnt -type f -name "*.apk"
         mkdir -p /opt/vntAPP
         mv -f /opt/vnt/build/app/outputs/flutter-apk/app-release.apk /opt/vntAPP/app-release.apk
         
      - name: 签名APK
        run: |
         cd /opt/vntAPP
         #生成签名文件keystore.jks
         keytool -genkey -v -keystore keystore.jks -storepass $password -alias key -keypass $password -keyalg RSA -keysize 2048 -validity 10000 -dname "CN=Android, OU=Android, O=Android, L=BeiJing, ST=BeiJing, C=CN"
         #启用谷歌v1 v2 v3 v4 签名方式同时签名
         /usr/local/lib/android/sdk/build-tools/34.0.0/apksigner sign --ks keystore.jks --ks-key-alias key --ks-pass pass:$password --key-pass pass:$password --out vntApp-android.apk --in app-release.apk --v1-signing-enabled true --v2-signing-enabled true --v3-signing-enabled true --v4-signing-enabled true
         rm app-release.apk
         
      - name : 上传
        uses: actions/upload-artifact@master
        if: always()
        with:
         name: vntAPP-Android
         path: /opt/vntAPP/*
