How to build signed apk from Android Studio for Flutter
Asked Answered
S

10

79

Is there a way to build an apk for flutter in Android Studio?

I have seen this guideline: https://flutter.dev/docs/deployment/android
But here flutter console is used to build apk.

Superphosphate answered 5/4, 2019 at 13:35 Comment(0)
E
116

You can build the Apk/AppBundle using IDE and command line.

  • Building APK/AppBundle through IDE:

    Step-1

    In Android Studio's tab bar, click on Tools and then Flutter and then Open Android module in Android Studio:

    enter image description here

    Step-2

    Open Project it in New Window:

    enter image description here

    Step-3

    Having opened the project, click on Build and then Generate Signed Bundle / APK ...

    enter image description here


  • Building APK/AppBundle through command:

    Step-1:

    Modify your build.gradle(app) file and include your key information there:

    android {
        compileSdkVersion 31
        signingConfigs {
            release {
                storeFile file("<path-to-keys.jks>")
                storePassword "********"
                keyAlias "<key-alias>"
                keyPassword "********"
            }
        }
        buildTypes {
            release {
                signingConfig signingConfigs.release
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    

    Step-2:

    Build AppBundle:

    flutter build appbundle --target-platform android-arm,android-arm64,android-x64 --obfuscate --split-debug-info=/<directory>
    

    Build APK:

    flutter build apk --target-platform android-arm,android-arm64,android-x64 --split-per-abi --obfuscate --split-debug-info=/<directory>
    
Ensiform answered 6/4, 2019 at 12:27 Comment(9)
Thanks, the latter steps I already knew. Please edit to show how to add the android module, because of this 'generate signed bundle/apk' was is disabled.Superphosphate
Thanks, but when I open again Android Studio it disappearsWire
Aha! Mine already had Android added, but they were empty. So I removed them, and readded with + (using Flutter here)Kinghood
There is no project structure under file in Android Studio 4Footage
No "Open Android module in Android Studio" option, but I can see "Open iOS module in XCode" option.Rosenkrantz
For anyone can't find "Open Android module in Android Studio" option https://mcmap.net/q/262911/-can-39-t-find-open-for-editing-in-android-studioScandium
Google really messed up here, making things worse. They should've implemented this for the host project.Mancy
At least, you can setup "Open Android module in Android Studio" in the keymap settings giving you faster access to the Android project.Mancy
Following your method, but in the new window(Android module) when I rebuild the project, getting this error:: > Task :app:compileFlutterBuildRelease C:\Users\partha.paul\AndroidStudioProjects\jiopay_consumer\.dart_tool\package_config.json does not exist. Did you run this command from the same directory as your pubspec.yaml file?Papoose
S
55

For create/generate Signed APK using Android Studio version 3.5.1 for Flutter follow the following steps -

Step 1 :

Go to in your project & then Tools -> Flutter -> Open for Editing in Android Studio as shown below

enter image description here

Then Select New Window option as shown below

enter image description here

Step 2 :

Wait for while until project synchronization. After that
Go to Build -> GenerateSigned Bundle/APK... option from menu bar as shown bellow

enter image description here

Step 3 :

Select Android App Bundle or APK Option as per your need. (Android App Bundle is best solution) and click Next button.

enter image description here

Step 4 :

Select Create new.. option to generate new Signed key (When you release your app First Time)

enter image description here

Step 5 :

Fill all options as follow

enter image description here

Note -

Key store Path -

Path where your key store file i.e .jks file stored (as shown in above image). [Best way Select the path within your project directory.]

Key store password -

Enter password e.g. 123456

Key alias -

Enter Key alias (Name of .jks file) e.g. key

Key Password -

Enter Key password (Choose different password than Key store password) e.g. key123456

Validity(years) - Keep it as it is or change as per your requirements.

Certificate -

Fill certificate information (Not all fields are mandatory)

Then click OK and you will get following screen...

Step 6 -

enter image description here

Step 7 -

Click Next button and you will get following screen...

enter image description here

Select options

Build variants - release and

Signature versions both V1 and V2 respectively as shown above screen shot and click Finish button.

Step 8 -

Wait for a while until Gradle Build Running process complete as shown below...

enter image description here

and finally you will get the Generate Signed APK : (APKs) generated successfully . from that click on Locate option to get Location of your Generate Signed APK Key. as shown bellow.

enter image description here

That's it you generated Signed APK successfully for your Flutter project.

IMPORTANT:

Please DO NOT lose the key and its all information i.e. Key store path,Key store password, Key alias and Key password (Best way write down it in to the note book or make text file and store on your drive while generating it.). Without this, you won't be able to update your application because the new release will need to be signed with the same key.

I hope you will get detailed information.

Shakhty answered 11/2, 2020 at 6:59 Comment(10)
great round up, but this is not the exact way you do it for flutterOpalescent
@MarcelHofgesang is there an alternative - easier - way to do it?Jacintha
@PaulSZ from step 7 on, it will be different generating a signed apkOpalescent
@MarcelHofgesang thnx for suggestion.Shakhty
@PaulSZ i think this is the easiest way rather than generating signed apk using command prompt.Shakhty
@DanieleAngelini Just close the editing window and go back to your Android Studio where you developing your code....thats it.Shakhty
I am fllowed the your step. app-release.abb file in the release folder. not in apk. why that?Millihenry
@Millihenry Please check the build variant are debug so that's why it does not appears. So just choose release and it will generated the file. just like... Build - Select build variant - Release .Shakhty
how I can declare a different main file for multiple variants in the android studio while creating the apk?Gliadin
@Shakhty Following your method, but in the new window(Android module) when I rebuild the project, getting this error:: > Task :app:compileFlutterBuildRelease C:\Users\partha.paul\AndroidStudioProjects\jiopay_consumer\.dart_tool\package_config.json does not exist. Did you run this command from the same directory as your pubspec.yaml file?Papoose
O
16

Instead of using Android Studios UI you could easily also just use a command prompt. This way was way easier for me, cause I had many issues creating a signed apk by using the ui.

Before you create the signed APK you must prepare your project.

1. Add Flutter automation dependencies to your pubspec.yaml

dependencies:  
    flutter:  
        sdk: flutter  
    flutter_automation: ^1.1.4    

Currently ^1.1.4 is the newest dependency, this will eventually change.

2. Get the new dependency

Run flutter packages get in your command prompt in your projects directoy

3. Setup the signing

Run flutter pub pub run flutter_automation --android-signin your command prompt.Then you will be asked to input

  • a key alias,
  • your certificate information ( CN=First and Last Name (??), OU=Organizational Unit, O=Organization, L=City or Locality, S=State or Province, C=Country Code (XX)
  • a key password
  • and a keystore password

4. Generate your signed apk or bundle

Simply run flutter build apk --split-per-abi or flutter build appbundle to generate a signed apk/bundle of your app.

Opalescent answered 16/3, 2020 at 0:31 Comment(6)
But this will fail for a team member without the signing configuration when you decide to not commit it into your repository.Mancy
Sure, but I would recommend to have only one person thats generating the signed apk.Opalescent
Of course. My point is that it will even fail to compile (debug) when you don't commit the keystore file or the configuration file that will be generated by flutter pub pub run flutter_automation --android-signin. I wish it was possible to not break the project when using your solution without commiting the signing-related files, because as you said: only one person should generate the signed APK. I hope that makes sense.Mancy
Should it be dev dependency?Freewheel
@Freewheel I am not sure whether declaring flutter_automation under dev_dependencies will work. In my case it was definitely just dependencies. I'd recommend not using dev_dependencies for this case, as far as I understand the dart documentation for dependencies in pubspecs: "Regular dependencies are listed under dependencies:—these are packages that anyone using your package will also need. Dependencies that are only needed in the development phase of the package itself are listed under dev_dependencies."Opalescent
Adding your signing key to your git repo is a bad practice. For allowing team members to sign you should share the signing key values with them. You can do this by adding a file key.properties in your android folder and referencing it in your app/build.gradle under signingConfigs. This way you don't have to commit your signing config (values). Remember to put key.properties in your gitignore.Edmanda
L
11

For a FULL Solution that helps everyone, (including those that cant find "open for editing in android studio" option in the sdk) follow steps below..

  1. Lets say you have a project called "YourProjectName"... Copy your project name in flutter as i have censored in my picture below, and right-click the android folder to create a new file.. The name on the new file must be "YourProjectName_android.iml" step1picture

  2. After creating the file in the format ive described in step 1, you will now fully see "Open for editing in android studio" option.. enter image description here

  3. Now let it build after clicking the option in step 2 above, then you click the usual option of "Generate signed apk" as you see in the picture below last step

Lollis answered 9/9, 2021 at 0:21 Comment(1)
live saver. thank you matePea
C
10

Don't use Android Studio key generation GUI


Just Running the following at the command line:

On Mac/Linux, use the following command:

  keytool -genkey -v -keystore ~/upload-keystore.jks -keyalg RSA -keysize 2048 -validity 10000 -alias upload

On Windows, use the following command:

  keytool -genkey -v -keystore c:\Users\USER_NAME\upload-keystore.jks -storetype JKS -keyalg RSA -keysize 2048 -validity 10000 -alias upload

for next steps and publish on Google Play look at :

Cychosz answered 23/1, 2022 at 15:29 Comment(1)
Android studio GUI took my 10 minutes and did not work. This solution is just 15 sec and works like a charm. ThanksOrthorhombic
G
8

As per DanyPata For flutter you have to set the signing file and then run flutter build APK. It's quite important to run the flutter command so that all the changes/libs added in flutter are also added in the Android project.

Step 1

Go to File - Project Structure and select app under Modules. NOTE: It probably won't be called app- It may be under whatever name you gave it when creating the project. Select the Signing tab.

Screenshot of Android Studio's Project Structure Window

You need to create a certificate so click the blue plus sign and fill in the blanks. When you finish this process then you head over to the Build Types tab and make sure you have two - debug and release, if you don't have release then again press the blue plus sign and create it.

For a release build, you will want to select the Signing Config you previously created and leave the rest as default.

Then to build your release APK go to Build on the top main bar, then Build Bundle(s) / APK(s) and select Build APK(s).

Keep a close eye on the Event Log to see when its complete and the path where APK was saved to.

Step 2

Update the gradle.build file. Make sure your project visibility is set to something like Project so you can see all the files in the file tree.

Head down to yourapp/src then open build.gradle.

Under, android add:

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

IMPORTANT: As a quick reminder and precaution, DO NOT lose the key it creates. Without this, you won't be able to update your application because the new release will need to be signed with the same key.

Gooseberry answered 5/4, 2019 at 13:51 Comment(3)
FYI for flutter you have to set the signing file and then run flutter build apk. It's quite important to run the flutter command so that all the changes/libs added in flutter are also added in the android project.Coonhound
@Coonhound Updated answer to show this comment, thanks.Gooseberry
Thanks for your answer, but I only had 'sources, paths, dependencies' tab under my module. What I did is added android module to my project module and then 'generate signed apk/bundle' got enabled under build tabSuperphosphate
H
3

Every times you run your app, it will generate apks.

You can find it in your project files :

yourProject\build\app\outputs\apk

Hydrostat answered 5/4, 2019 at 13:42 Comment(2)
Sorry, but i meaned signed apk, I have edited my questionSuperphosphate
Every times you run your app will create a debug apk not a release one.Debate
C
2

Instead of using Android Studios UI, you could easily also just use a command prompt.

Follow the below steps to generate a signed apk in flutter using the command line.

  • Step 1: Generate the keystone file

    Running the following at the command line:

    On Mac/Linux,

    keytool -genkey -v -keystore ~/upload-keystore.jks -keyalg RSA -keysize 2048 -validity 10000 -alias upload
    

    On Windows,

    keytool -genkey -v -keystore c:\Users\USER_NAME\upload-keystore.jks -storetype JKS -keyalg RSA -keysize 2048 -validity 10000 -alias upload
    
  • Step 2: Modify your build.gradle(app) file

    Include your key information on the build.gradle(app) file:

    android {
      compileSdkVersion 31
      signingConfigs {
          release {
              storeFile file("<path-to-keys.jks>")
              storePassword "********"
              keyAlias "<key-alias>"
              keyPassword "********"
          }
      }
      buildTypes {
          release {
              signingConfig signingConfigs.release
              proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
          }
      }
    }
    
  • Step 3: Generate the apk

    Run the following command from the flutter root directory using the command line.

    flutter build apk --target-platform android-arm,android-arm64,android-x64 --split-per-abi --obfuscate --split-debug-info=/<directory>
    

    Provide a valid directory path as split-debug-info

    The build will be generated under the build\app\outputs\flutter-apk folder.

For more: https://docs.flutter.dev/deployment/android

Chemotropism answered 28/3, 2022 at 3:55 Comment(0)
T
0

I found a permanent solution

Just open your existing flutter app directly from the "app directly" which contain the android files like Android Manifest directory directly in android studio. This will load your project as an android project and i believe the rest is self explanatory what you need to do.

Tabber answered 17/11, 2020 at 21:55 Comment(0)
P
0

add this code and save it you app and android.iml

<?xml version="1.0" encoding="UTF-8"?>
    <module type="JAVA_MODULE" version="4">
      <component name="FacetManager">
        <facet type="android" name="Android">
          <configuration>
            <option name="ALLOW_USER_CONFIGURATION" value="false" />
            <option name="GEN_FOLDER_RELATIVE_PATH_APT" value="/gen" />
            <option name="GEN_FOLDER_RELATIVE_PATH_AIDL" value="/gen" />
            <option name="MANIFEST_FILE_RELATIVE_PATH" value="/app/src/main/AndroidManifest.xml" />
            <option name="RES_FOLDER_RELATIVE_PATH" value="/app/src/main/res" />
            <option name="ASSETS_FOLDER_RELATIVE_PATH" value="/app/src/main/assets" />
            <option name="LIBS_FOLDER_RELATIVE_PATH" value="/app/src/main/libs" />
            <option name="PROGUARD_LOGS_FOLDER_RELATIVE_PATH" value="/app/src/main/proguard_logs" />
          </configuration>
        </facet>
      </component>
      <component name="NewModuleRootManager" inherit-compiler-output="true">
        <exclude-output />
        <content url="file://$MODULE_DIR$">
          <sourceFolder url="file://$MODULE_DIR$/app/src/main/java" isTestSource="false" />
          <sourceFolder url="file://$MODULE_DIR$/app/src/main/kotlin" isTestSource="false" />
          <sourceFolder url="file://$MODULE_DIR$/gen" isTestSource="false" generated="true" />
        </content>
        <orderEntry type="jdk" jdkName="Android API 29 Platform" jdkType="Android SDK" />
        <orderEntry type="sourceFolder" forTests="false" />
        <orderEntry type="library" name="Flutter for Android" level="project" />
        <orderEntry type="library" name="KotlinJavaRuntime" level="project" />
      </component>
    </module>
Pigmentation answered 19/1, 2022 at 7:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.