github.com/pojntfx/hydrapp/hydrapp@v0.0.0-20240516002902-d08759d6ca9f/pkg/builders/apk/entrypoint.sh (about)

     1  #!/bin/bash
     2  
     3  set -e
     4  
     5  # Setup PGP
     6  echo "${PGP_KEY_PASSWORD}" | base64 -d >'/tmp/pgp-pass'
     7  mkdir -p "${HOME}/.gnupg"
     8  cat >"${HOME}/.gnupg/gpg.conf" <<EOT
     9  yes
    10  passphrase-file /tmp/pgp-pass
    11  pinentry-mode loopback
    12  EOT
    13  
    14  echo "${PGP_KEY}" | base64 -d >'/tmp/private.pgp'
    15  gpg --import /tmp/private.pgp
    16  
    17  # Prepare build environment
    18  export BASEDIR="${PWD}/${GOMAIN}"
    19  
    20  # Configure Go
    21  export GOPROXY='https://proxy.golang.org,direct'
    22  
    23  # Generate dependencies
    24  GOFLAGS="${GOFLAGS}" sh -c "${GOGENERATE}"
    25  
    26  mkdir -p '/tmp/out'
    27  bash -O extglob -c 'cd /tmp/out && rm -rf -- !(*.jar)'
    28  mkdir -p '/tmp/out/drawable'
    29  
    30  # Build native libraries
    31  CGO_ENABLED=1 GOOS=android GOARCH=386 CC="${ANDROID_HOME}/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/i686-linux-android${ANDROID_NDK_VERSION}-clang" CXX="${ANDROID_HOME}/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/i686-linux-android${ANDROID_NDK_VERSION}-clang++" go build -buildmode='c-shared' -o='/tmp/out/lib/x86/libbackend.so' "${GOMAIN}"
    32  CGO_ENABLED=1 GOOS=android GOARCH=amd64 CC="${ANDROID_HOME}/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/x86_64-linux-android${ANDROID_NDK_VERSION}-clang" CXX="${ANDROID_HOME}/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/x86_64-linux-android${ANDROID_NDK_VERSION}-clang++" go build -buildmode='c-shared' -o='/tmp/out/lib/x86_64/libbackend.so' "${GOMAIN}"
    33  CGO_ENABLED=1 GOOS=android GOARCH=arm GOARM=5 CGO_LDFLAGS="--sysroot ${ANDROID_LEGACY_HOME}/platforms/android-${ANDROID_LEGACY_API_VERSION}/arch-arm" CGO_CFLAGS="--sysroot ${ANDROID_LEGACY_HOME}/platforms/android-${ANDROID_LEGACY_API_VERSION}/arch-arm" CC="${ANDROID_LEGACY_HOME}/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc" CXX="${ANDROID_LEGACY_HOME}/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin/arm-linux-androideabi-g++" go build -tags "netgo,androiddnsfix,tlscertembed" -buildmode='c-shared' -o='/tmp/out/lib/armeabi/libbackend_compat.so' "${GOMAIN}"
    34  CGO_ENABLED=1 GOOS=android GOARCH=arm GOARM=5 CC="${ANDROID_HOME}/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi${ANDROID_LEGACY_NDK_VERSION}-clang" CXX="${ANDROID_HOME}/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi${ANDROID_LEGACY_NDK_VERSION}-clang++" go build -tags "netgo,androiddnsfix,tlscertembed" -buildmode='c-shared' -o='/tmp/out/lib/armeabi-v7a/libbackend_compat.so' "${GOMAIN}"
    35  CGO_ENABLED=1 GOOS=android GOARCH=arm CC="${ANDROID_HOME}/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi${ANDROID_NDK_VERSION}-clang" CXX="${ANDROID_HOME}/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/armv7a-linux-androideabi${ANDROID_API_VERSION}-clang++" go build -buildmode='c-shared' -o='/tmp/out/lib/armeabi-v7a/libbackend.so' "${GOMAIN}"
    36  CGO_ENABLED=1 GOOS=android GOARCH=arm64 CC="${ANDROID_HOME}/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android${ANDROID_NDK_VERSION}-clang" CXX="${ANDROID_HOME}/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android${ANDROID_NDK_VERSION}-clang++" go build -buildmode='c-shared' -o='/tmp/out/lib/arm64-v8a/libbackend.so' "${GOMAIN}"
    37  
    38  # Sign native libraries with PGP
    39  gpg --detach-sign --armor "/tmp/out/lib/"*/*
    40  
    41  # Create package
    42  cd '/tmp/out' || exit 1
    43  cp "${BASEDIR}"/*.java "${BASEDIR}/AndroidManifest.xml" .
    44  cp "${BASEDIR}/icon.png" 'drawable'
    45  javac -source "1.8" -target "1.8" -cp *.jar -cp "${ANDROID_HOME}/build-tools/${ANDROID_BUILD_TOOLS_VERSION}/core-lambda-stubs.jar" -bootclasspath "${ANDROID_HOME}/platforms/android-${ANDROID_API_VERSION}/android.jar" *.java
    46  "${ANDROID_HOME}/build-tools/${ANDROID_BUILD_TOOLS_VERSION}/d8" *.class --release
    47  "${ANDROID_HOME}/build-tools/${ANDROID_BUILD_TOOLS_VERSION}/aapt2" compile 'drawable/icon.png' -o .
    48  "${ANDROID_HOME}/build-tools/${ANDROID_BUILD_TOOLS_VERSION}/aapt2" link -o "${APP_ID}.unsigned" -I "${ANDROID_HOME}/platforms/android-${ANDROID_API_VERSION}/android.jar" *.flat --manifest 'AndroidManifest.xml'
    49  zip -ur "${APP_ID}.unsigned" 'lib' 'classes.dex'
    50  mkdir -p "/tmp/out/android-certs" # Append *.jar here to use an external library
    51  
    52  # Sign package with Android certificate
    53  echo "${JAVA_KEYSTORE}" | base64 -d >"/tmp/out/android-certs/${APP_ID}.keystore"
    54  
    55  export ANDROID_CERT_CN="$(keytool -noprompt -storepass $(echo ${JAVA_KEYSTORE_PASSWORD} | base64 -d) -keypass $(echo ${JAVA_CERTIFICATE_PASSWORD} | base64 -d) -v -list -keystore /tmp/out/android-certs/${APP_ID}.keystore | grep -oP 'Owner: CN=\K\w(.*)')"
    56  export ANDROID_CERT_ALIAS="$(keytool -noprompt -storepass $(echo ${JAVA_KEYSTORE_PASSWORD} | base64 -d) -keypass $(echo ${JAVA_CERTIFICATE_PASSWORD} | base64 -d) -v -list -keystore /tmp/out/android-certs/${APP_ID}.keystore | grep -oP 'Alias name: \K\w(.*)')"
    57  
    58  "${ANDROID_HOME}/build-tools/${ANDROID_BUILD_TOOLS_VERSION}/zipalign" -f -p 4 "${APP_ID}.unsigned" "${APP_ID}.apk"
    59  "${ANDROID_HOME}/build-tools/${ANDROID_BUILD_TOOLS_VERSION}/apksigner" sign --ks "/tmp/out/android-certs/${APP_ID}.keystore" --ks-pass pass:"$(echo ${JAVA_KEYSTORE_PASSWORD} | base64 -d)" --key-pass pass:"$(echo ${JAVA_CERTIFICATE_PASSWORD} | base64 -d)" "${APP_ID}.apk"
    60  
    61  # Sign package with PGP and stage
    62  gpg --detach-sign --armor "${APP_ID}.apk"
    63  
    64  # Setup repository
    65  rm -rf "/hydrapp/dst/"*
    66  cd "/hydrapp/dst" || exit 1
    67  
    68  fdroid init
    69  cat >'config.yml' <<EOT
    70  ---
    71  sdk_path: "${ANDROID_HOME}"
    72  repo_url: "${BASE_URL}"
    73  repo_name: hydrapp F-Droid Repo
    74  repo_description: >-
    75    Android apps for hydrapp.
    76  repo_icon: icon.png
    77  repo_keyalias: ${ANDROID_CERT_ALIAS}
    78  keystore: keystore.p12
    79  keystorepass: "$(echo ${JAVA_KEYSTORE_PASSWORD} | base64 -d)"
    80  keypass: "$(echo ${JAVA_CERTIFICATE_PASSWORD} | base64 -d)"
    81  keydname: CN=${ANDROID_CERT_CN}
    82  apksigner: /usr/bin/apksigner
    83  EOT
    84  
    85  cp "/tmp/out/${APP_ID}.apk" 'repo/'
    86  cp "${BASEDIR}/icon.png" .
    87  cp "/tmp/out/android-certs/${APP_ID}.keystore" 'keystore.p12'
    88  
    89  fdroid update --create-metadata
    90  fdroid gpgsign
    91  
    92  cat >'.gitignore' <<'EOT'
    93  *.ks
    94  *.jks
    95  *.keystore
    96  *.crt
    97  *config.py
    98  tmp
    99  EOT
   100  
   101  if [ "${DST_UID}" != "" ] && [ "${DST_GID}" != "" ]; then
   102    chown -R "${DST_UID}:${DST_GID}" /hydrapp/dst
   103  fi