mlkit icon indicating copy to clipboard operation
mlkit copied to clipboard

Brand new app with `mlkit-core` fails to build for iOS

Open andrewm-mitchells opened this issue 1 year ago • 3 comments

Steps to reproduce:

  1. Generate new project with drawer template NG ns create ml-kit-test --template @nativescript/template-drawer-navigation-ng
  2. Run npm i @nativescript/mlkit-core
  3. Run ns build iOS

At this point you'd get the following error: Command xcodebuild failed with exit code 65

ns info:

Component nativescript has 8.8.0 version and is up to date.
✔ Component @nativescript/core has 8.8.1 version and is up to date.
✔ Component @nativescript/ios has 8.8.1 version and is up to date.
✖ Component @nativescript/android is not installed.

package.json:

{
  "name": "ml-kit-test",
  "main": "src/main.ts",
  "version": "1.0.0",
  "private": true,
  "dependencies": {
    "@angular/animations": "~18.0.0",
    "@angular/common": "~18.0.0",
    "@angular/compiler": "~18.0.0",
    "@angular/core": "~18.0.0",
    "@angular/forms": "~18.0.0",
    "@angular/platform-browser": "~18.0.0",
    "@angular/platform-browser-dynamic": "~18.0.0",
    "@angular/router": "~18.0.0",
    "@nativescript/angular": "^18.0.0",
    "@nativescript/core": "~8.8.0",
    "@nativescript/mlkit-core": "^2.1.0",
    "@nativescript/theme": "~3.0.2",
    "nativescript-ui-sidedrawer": "~15.2.0",
    "rxjs": "~7.8.0",
    "zone.js": "~0.14.0"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~18.0.0",
    "@angular/compiler-cli": "~18.0.0",
    "@nativescript/ios": "8.8.1",
    "@nativescript/types": "~8.8.0",
    "@nativescript/webpack": "~5.0.0",
    "@ngtools/webpack": "~18.0.0",
    "typescript": "~5.4.0"
  }
}

andrewm-mitchells avatar Jul 22 '24 15:07 andrewm-mitchells

This bug makes it impossible to use a barcode scanner on nativescript currently as other solutions like https://market.nativescript.org/plugins/@nstudio/nativescript-barcodescanner/ are also out of date and won't function properly

andrewm-mitchells avatar Jul 22 '24 15:07 andrewm-mitchells

same issue. Any updates?

ShimonKh avatar Nov 03 '24 09:11 ShimonKh

I tried using version 1.0.6 for iOS and 3.0.0 for Android, and it works perfectly. To test, you can add a script before compiling in the nativescript.config.ts file, for example:

hooks: [
    {
      type: 'before-prepare',
      script: 'scripts-hook/mlkit-before-prepare.js',
    },
  ]

script:

module.exports = function ($logger, $platformsDataService, $projectData, hookArgs) {
  return new Promise(async (resolve, reject) => {
    const { execSync } = require('child_process')

    const platform = hookArgs.prepareData.platform
    const currentEnvironment = hookArgs.prepareData.env.prod || hookArgs.prepareData.env.production ? 'prod' : 'dev'

    $logger.info(`[MLKit Hook] before-prepare ${platform} - environment: ${currentEnvironment}`)

    try {
      const platformVersions = {
        android: {
          '@nativescript/mlkit-barcode-scanning': '3.0.0',
          '@nativescript/mlkit-core': '3.0.0',
        },
        ios: {
          '@nativescript/mlkit-barcode-scanning': '1.0.6',
          '@nativescript/mlkit-core': '1.0.6',
        },
      }

      const versions = platformVersions[platform]
      if (!versions) {
        $logger.info(`[MLKit Hook] No specific versions defined for platform: ${platform}`)
        resolve(true)
        return
      }

      $logger.info(`[MLKit Hook] Installing MLKit dependencies for ${platform}...`)

      Object.entries(versions).forEach(([packageName, version]) => {
        try {
          $logger.info(`[MLKit Hook] Installing ${packageName}@${version} for ${platform}...`)
          execSync(`npm install ${packageName}@${version} --legacy-peer-deps`, {
            cwd: $projectData.projectDir,
            stdio: 'inherit',
          })
          $logger.info(`[MLKit Hook] Successfully installed ${packageName}@${version}`)
        } catch (error) {
          $logger.error(`[MLKit Hook] Failed to install ${packageName}@${version}: ${error.message}`)
        }
      })

      $logger.info(`[MLKit Hook] MLKit setup completed for ${platform}`)
      resolve(true)
    } catch (error) {
      $logger.error(`[MLKit Hook] Error: ${error.message}`)
      reject(error)
    }
  })
}


omarmfs98 avatar Jul 04 '25 21:07 omarmfs98