Setting up Windows 10 for Android development using Flutter, Firebase database and Firebase Cloud Messaging


Dated: October, 2019

1. SETTING UP WINDOWS SYSTEM TO CREATE ANDROID APPS USING FLUTTER:

Ref: flutter.dev: Installing on Windows

Install Git (for Windows) from here: Git-scm.com

C:\>git clone -b stable https://github.com/flutter/flutter.git

C:\>flutter doctor 

# This program install Dart SDK onto the system. 

╔════════════════════════════════════════════════════════════════════╗
║                 Welcome to Flutter! - https://flutter.dev          ║
║ Use "flutter config --no-analytics" to disable analytics and crash ║
║ reporting.
    
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, v1.7.8+hotfix.4, on Microsoft Windows [Version 10.0.17134.829], locale en-US)
[X] Android toolchain - develop for Android devices
    X Unable to locate Android SDK.
      Install Android Studio from: https://developer.android.com/studio/index.html
      On first launch it will assist you in installing the Android SDK components.
      (or visit https://flutter.dev/setup/#android-setup for detailed instructions).
      If the Android SDK has been installed to a custom location, set ANDROID_HOME to that location.
      You may also want to add it to your PATH environment variable.

[!] Android Studio (not installed)
[!] Connected device
    ! No devices available

! Doctor found issues in 3 categories.
---

[!] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    X Android licenses not accepted.  To resolve this, run: flutter doctor --android-licenses
	
--- 

C:\Users\Ashish Jain>flutter doctor --android-licenses 
A newer version of the Android SDK is required. To update, run:
C:\Users\Ashish Jain\AppData\Local\Android\sdk\tools\bin\sdkmanager --update 

--------

% Install Android Studio
% Install Android SDK Components

.........

Install the Flutter and Dart plugins:

To install these:

    1. Start Android Studio.
    2. Open plugin preferences (Preferences > Plugins on macOS, File > Settings > Plugins on Windows & Linux).
    3. Select Browse repositories, select the Flutter plugin and click Install.
    4. Click Yes when prompted to install the Dart plugin.
    5. Click Restart when prompted.

###    ###    ###    ###    ###    ###    ###

2. SETTING UP THE FLUTTER APP TO WORK WITH FIREBASE DATABASE 

Add these following dependencies to the file "pubspec.yaml":

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^0.1.2
  cloud_firestore: ^0.12.9+3
  url_launcher: ^4.0.3 

.............................

For Firebase setup:
Copy-paste file "google-services.json" at this path: C:\Users\Admin\Desktop\Flutter-master\survival\android\app

.............................
Changes in file "C:\Users\Admin\Desktop\Flutter-master\survival\android\app\build.gradle":

1)
apply plugin: 'com.google.gms.google-services'

2)
Set minSdkVersion to 21.

By default, it is: 16

Otherwise, you get this error:
The number of method references in a .dex file cannot exceed 64K.
  Learn how to resolve this issue at https://developer.android.com/tools/building/multidex.html

3)
implementation 'com.google.firebase:firebase-analytics:17.0.1'

.............................

Changes in file "C:\Users\Admin\Desktop\Flutter-master\survival\android\build.gradle":

1)
classpath 'com.google.gms:google-services:4.2.0'

### ### ### ### ### ### ### ### ### ###

3. SETTING UP THE FLUTTER APP TO WORK WITH FIREBASE CLOUD MESSAGING

Add these following dependencies to the file "pubspec.yaml":

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^0.1.2
  cloud_firestore: ^0.12.9+3
  url_launcher: ^4.0.3
  firebase_messaging: ^2.0.0 

...

E:\programfiles\flutter\bin\flutter.bat --no-color packages get
E:\programfiles\flutter\bin\flutter.bat --no-color packages upgrade

................................

For Firebase setup:
Copy-paste file "google-services.json" at this path: C:\Users\Admin\Desktop\Flutter-master\survival\android\app

.................................

Changes in file "C:\Users\Admin\Desktop\Flutter-master\survival\android\app\build.gradle":

1)
apply plugin: 'com.google.gms.google-services'

2)
Set minSdkVersion to 21.

From: 16
Otherwise, you get this error:
The number of method references in a .dex file cannot exceed 64K.
  Learn how to resolve this issue at https://developer.android.com/tools/building/multidex.html

3)
implementation 'com.google.firebase:firebase-analytics:17.0.1'
implementation 'com.google.firebase:firebase-core:17.2.0' 
.................................

Changes in file "C:\Users\Admin\Desktop\Flutter-master\survival\android\build.gradle":

1)
classpath 'com.google.gms:google-services:4.3.2'

============================

Connecting the phone: https://javatutorial.net/connect-android-device-android-studio

============================

For FCM, following code goes in AndroidManifest.xml file:

<intent-filter>
	<action android:name="FLUTTER_NOTIFICATION_CLICK" />
	<category android:name="android.intent.category.DEFAULT" />
</intent-filter> 

=============================

Reviewing the app manifest [Ref: flutter.dev]

Review the default App Manifest file, AndroidManifest.xml, located in [app dir]/android/app/src/main and verify that the values are correct, especially:

    [APPLICATION] TAG: Edit the android:label in the application tag to reflect the final name of the app.

    uses-permission: Add the android.permission.INTERNET permission if your application code needs Internet access. The standard template does not include this tag but allows Internet access during development to enable communication between Flutter tools and a running app.

### ### ### ### ### ### ### ### ### ###

SOME USEFUL TIPS:

Tip 1: 

YOU CAN GENERATE KEY-PAIR AND SELF-SIGNED CERTIFICATE FROM COMMAND LINE AS WELL.

C:\Users\Ashish Jain\AndroidStudioProjects\survival>keytool -genkey -v -keystore e:/key.jks -storetype JKS -keyalg RSA -keysize 2048 -validity 10000 -alias key0 
Enter keystore password:
Re-enter new password:
What is your first and last name?
...
What is the name of your organizational unit?
...
What is the name of your organization?
...
What is the name of your City or Locality?
...
What is the name of your State or Province?
...
What is the two-letter country code for this unit?
...

Generating 2,048 bit RSA key pair and self-signed certificate (SHA256withRSA) with a validity of 10,000 days
...
Enter key password for [key0]
        (RETURN if same as keystore password):
[Storing e:/key.jks]

Warning:
The JKS keystore uses a proprietary format. It is recommended to migrate to PKCS12 which is an industry standard format using "keytool -importkeystore -srckeystore e:/key.jks -destkeystore e:/key.jks -deststoretype pkcs12". 

............................

Tip 2: 

FINDING THE LOCATION OF THE 'zipalign' PROGRAM ON WINDOWS

C:\Users\Ashish Jain>dir /s *zipalign* 
 Volume in drive C is Windows
 Volume Serial Number is 8139-90C0

 Directory of C:\Users\Ashish Jain\AppData\Local\Android\Sdk\build-tools\28.0.3

09/14/2019  09:43 PM           744,960 zipalign.exe
               1 File(s)        744,960 bytes

 Directory of C:\Users\Ashish Jain\AppData\Local\Android\Sdk\build-tools\29.0.2

09/08/2019  11:35 PM           731,648 zipalign.exe
               1 File(s)        731,648 bytes

     Total Files Listed:
               2 File(s)      1,476,608 bytes
               0 Dir(s)  75,927,285,760 bytes free 
			   
............................

Tip 3: 

GENERATING .APK FILE FOR FLUTTER CODE FROM COMMAND LINE:

C:\Users\Ashish Jain\AndroidStudioProjects\survival>flutter build apk
You are building a fat APK that includes binaries for android-arm, android-arm64.
If you are deploying the app to the Play Store, it's recommended to use app bundles or split the APK to reduce the APK size.
    To generate an app bundle, run:
        flutter build appbundle --target-platform android-arm,android-arm64
        Learn more on: developer.android.com
    To split the APKs per ABI, run:
        flutter build apk --target-platform android-arm,android-arm64 --split-per-abi
        Learn more on:  https://developer.android.com/studio/build/configure-apk-splits#configure-abi-split
Initializing gradle...                                              4.0s
Resolving dependencies...                                          15.5s
Running Gradle task 'assembleRelease'...

Running Gradle task 'assembleRelease'... Done                     221.9s (!)
Built build\app\outputs\apk\release\app-release.apk (14.3MB). 

............................

No comments:

Post a Comment