Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APK filename and buildpath hardcoded in capacitor/cli #4740

Closed
mrambow opened this issue Jun 18, 2021 · 10 comments · Fixed by #4782
Closed

APK filename and buildpath hardcoded in capacitor/cli #4740

mrambow opened this issue Jun 18, 2021 · 10 comments · Fixed by #4782

Comments

@mrambow
Copy link

mrambow commented Jun 18, 2021

Hi,

we're using ionic 5 with capacitor 3 and having difficulties to get the livereload to work for our debug app.
We get following error from capacitor run android --livereload:
native-run Caught fatal error: [Error: ENOENT: no such file or directory, open 'MyProjectPath\android\app\build\outputs\apk\debug\app-debug.apk']

After some digging (and failing to understand the gradle config build steps) I saw following lines in the capacitor run code:

const apkName = 'app-debug.apk';

const buildOutputDir = `${appDir}/build/outputs/apk/debug`;

Is there any possibility to change the name of the APK and the build path from the outside?

@jcesarmobile
Copy link
Member

Yeah, they are hardcoded because that's where capacitor apps get build with gradle command.
Did you change something in your gradle files to build somewhere else or with a different name?

@mrambow
Copy link
Author

mrambow commented Jun 19, 2021

Yes, we added productFlavours in gradle, but didn't touch the build part.
Gradle is generating the files in
MyProjectPath\android\app\build\outputs\apk\%flavorName%\debug|release\something.apk

@jcesarmobile
Copy link
Member

Can you be more specific?

@mrambow
Copy link
Author

mrambow commented Jun 21, 2021

Sure, just tell me what you want to see!

partial build.gradle:

buildTypes {
    release {
      minifyEnabled false
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
  }

  flavorDimensions 'stage'

  productFlavors {
    beta {
      dimension 'stage'
      versionNameSuffix '-beta'
      applicationId "de.com.stuff"
      signingConfig signingConfigs.beta
      manifestPlaceholders = [appLabel: "Name", customUrlScheme: "de.com.stuff"]
    }

    qa {
      dimension 'stage'
      versionNameSuffix '-beta'
      applicationId "de.com.stuff.beta"
      signingConfig signingConfigs.beta
      manifestPlaceholders = [appLabel: "Name QS", customUrlScheme: "de.com.stuff.beta"]
    }

    production {
      dimension 'stage'
      applicationId "de.com.stuff.prod"
      signingConfig signingConfigs.production
      manifestPlaceholders = [appLabel: "Name Prod", customUrlScheme: "de.com.stuff.prod"]
    }
  }
}

@gabides
Copy link

gabides commented Jun 24, 2021

I have the same issue, we have several flavors and the path to our built APKs is not the one hardcoded so none of our flavors are building with the capacitor run android command.

One solution would be to make this path customizable in the capacitor.config.json, or maybe add a --flavor argument to the CLI when running capacitor run android.

@gabides
Copy link

gabides commented Jun 24, 2021

I have opened a PR with solution 1 which solves the issue on my side (#4757), @jcesarmobile would something like this be acceptable?

with my changes @mrambow could set in the capacitor config :

config.android. buildOutputApkName = "something.apk"
config.android.buildOutputDirCustom = "build\outputs\apk\%flavorName%\debug|release"

@jcesarmobile
Copy link
Member

I've sent a similar PR that only adds flavor configuration option in the capacitor config file and build the apk name and directory based on it, I think that's simpler for users.
Tested with the flavor examples provided by mrambow and works fine.

@gabides
Copy link

gabides commented Jul 5, 2021

Nice, I have tested my project with @capacitor/cli on your branch and it works as expected, I agree it's simpler to just set the flavor inside the config.

@dragermrb
Copy link

Hi!

Sorry to comment on a closed issue.

I usually rename outputs apk with this gradle config:

// File android/app/build.gradle
android{
   // ...
   // Rename outputs apk
    applicationVariants.all {
        variant ->
            variant.outputs.each {
              output->
                  output.outputFileName  = "AppName-${variant.name}-v${variant.versionName}.apk"
            }
    }
   // ...
}

This produces apk files like MyApp-debug-v1.0.0.apk which cannot be deployed with ionic cap run android.

Capacitor could get final apkName from android/app/build/outputs/apk/debug/output-metadata.json so it will always be accurate. For example

  // File cli/src/android/run.ts, line ~32

  const metadata = require(`${config.android.buildOutputDirAbs}/output-metadata.json`);
  const apkName = metadata.elements[0].outputFile;

  const apkPath = resolve(
    config.android.buildOutputDirAbs,
    apkName
  );

  //...

  await runTask(
    `Deploying ${c.strong(apkName)} to ${c.input(target.id)}`,
    async () => runNativeRun(nativeRunArgs),
  );

I can make a PR if you want. What do you think?

@ionitron-bot
Copy link

ionitron-bot bot commented Nov 10, 2022

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.

@ionitron-bot ionitron-bot bot locked and limited conversation to collaborators Nov 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants