How to get compile/build date on Flutter App? - flutter

I know how to get the version using package_info, but how to get the build timestamp on Runtime both on ios and android?

You can use a build shell script that creates/updates a Dart file in lib/... with constants holding the date before running flutter build ....
You then import that file in your code and use it.

I'll suggest that you also consider basically rolling your own version of the package_info library which includes all the information you want to get from the platform code. I use the Gradle build files to update some variables on each build, which I then retrieve from Flutter.
Examining the package_info code shows that it is really a fairly simple MethodChannel use case. You could add anything that you can get from the native side.

Expanding on Günter Zöchbauer answer...
Create a text file in your source folder and enter the following
#!/bin/sh
var = "final DateTime buildDate =
DateTime.fromMillisecondsSinceEpoch(($(date "+%s000")));"
echo "$var" > lib/build_date.dart
Save the text file as
build_date_script.sh
In Android Studio, open Edit Configurations -> Add New Configuration and add a new 'Shell Script'
Give it a name, such as 'Write build date'
Select 'Script file' and enter the location of the 'build_date_script.sh' we created it stage 2.
Download and install GitBash https://gitforwindows.org/
Back to Android Studio, change the interpreter path to 'C:\Program Files\Git\git-bash.exe'.
Uncheck 'Execute in terminal' and click 'Apply'
Then in your main flutter run configuration, add a 'Before launch' item, select 'Run another configuration' and choose the script config we named in stage 4.
Click OK.
Now every time you build it will create a new dart file in your lib folder called 'build_date.dart' with a variable called 'buildDate' for use as a build date within your application.

Related

How come "flutter create ." adds desktop support?

I created a Flutter project within Android Studio, and then later found out that Flutter supports desktop. After searching the web, I did things like switching to "dev", adding config for desktop, etc. But when I tried to ran it, I got "No Windows desktop project configured", even though I enabled the configuration.
The documentation page had
To add desktop support to an existing Flutter project, run the following command in a terminal from the root project directory:
flutter create .
This adds the necessary desktop files and directories to your existing Flutter project.
But what is the meaning of .? How come that . means "adding desktop support"?
The command to add desktop support are those one:
flutter config --enable-windows-desktop
flutter config --enable-macos-desktop
flutter config --enable-linux-desktop
Once you used them, you change your flutter config.
Then, the usage of flutter create . simply mean "Create a new flutter project in my current folder".
If you're already in a Flutter project, it will just "recreate" your project by adding the new support for Desktop since you specify in your flutter config.
flutter config --enable-[windows,linux,macos]-desktop
flutter create .
if there is some error with second command, remove - sign from parent folder name, and run this command:
flutter create --org com.example.myapp .
Run this command it works fine for me.
run
flutter config --enable-windows-desktop
then run
flutter create .
if flutter create . generate error, probably there is - in your root project directory.
for example it will generate error if your project is like below
Alewa-Stock
to work it fine rename your project directory like below
alewa_stock
then run the following command
flutter create --platforms=windows --org=com.alewa.store .
*
NB: Please change the name according to your preference.
flutter create --platforms=windows,macos,linux .
you can use this line instead

I cannot initialize Flutter for web after adding flavors to my project

I have a Flutter project that currently builds for iOS and Android. I created the project around Flutter v1.9 or so. Since creating the project, I have added flavor support to my project (dev, prod) via the guide found here
However, after having done this, it doesn't seem like I am able to add web support to my project. Following Flutter's official instructions to enable web for a pre-existing project, I try to run flutter create . in the root directory of the project. When I do this I get the output:
The Xcode project defines schemes: dev, prod You must specify a --flavor option to select one of the available schemes.
So I try to run: flutter create . --flavor=dev and get Multiple output directories specified. Try moving --flavor=dev to be immediately following create
Soooo, I try: flutter create --flavor=dev . and get Could not find an option named "flavor".
Does anyone know how I can initialize web for this project? Thanks.
You need to do the following:
Goto root folder and rename manually ios folder temporarily to something like ios1
Goto terminal and make sure you are standing in root, then run this line:
flutter create --platforms=web .
Return to project folder and rename back manually ios1 to ios
Done

How to enable Swift support for existing project in flutter

I want to know if there is a way to enable swift support for flutter project. I only enabled Kotlin support while creating the project. I need to enable Swift too. Is there a command I can execute or any setting in flutter plugin for Android studio where I can enable or is there is an option to enable in Xcode?
This is what I want to do but for existing Flutter project
Delete existing ios folder from root of flutter project.
Run this command flutter create -i swift .
This command will create only ios directory with swift support.
Well, I search the same thing now, also I enable the kotlin support...
so, how to enable swift or kotlin support for a existing proyect?
For swift support, you need to move your ios folder to outside of your project folder, for kotlin move outside the android folder, also check your package name in your manifest, or your PRODUCT_BUNDLE_IDENTIFIER
Run the below flutter command in your terminal on root folder of your proyect (I'm using com.custom_name.my_proyect for the package name of this example)
-i swift is for swift
-a kotlin is for kotlin.
--org is to set the first two words of your package, in this case com.custom_name
--project-name is to set the last word of your package in this case my_proyect
You can use only switf/kotlin or both, (Don't forget the period '.' at the end of the command)
flutter create -i swift -a kotlin --org com.custom_name --project-name my_proyect .
*Apply again your previous custom changes on the ios folder (E.g: info.plist, custom splash screen, etc.), now in Runner folder you don't have main.m and AppDelegate.h files, instead you have only the AppDelegate.swift file in swift language, so if you need to put API_KEYs there, the code is different.
*If you apply -a kotlin line, is the same logic that swift in your android folder, so your MainActivy.java file is now a MainActivity.kt file in kotlin language, and you need to apply again your previous custom changes in Android folder (E.g: build.gradle, res folder, android_manifest, etc.).
Little known secret -- you can run flutter create . in your Flutter app directory and it will repair the project, recreating any files that are missing. So if you already have a project created with Objective-C and Java, you can run:
flutter create -i swift -a kotlin .
to convert the host app to Kotlin and Swift.
(Ignore Kotlin you you want, but my experience is that just leave it there)
Bridging Header must be created.
Open the project with XCode. Then choose File -> New -> File -> Swift File.
A dialog will be displayed when creating the swift file(Since this file is deleted, any name can be used.). XCode will ask you if you wish to create Bridging Header, click yes.
Make sure you have use_frameworks! in the Runner block, in ios/Podfile。
Make sure you have SWIFT_VERSION 4.2 selected in you XCode -> Build Settings
Do flutter clean
Go to your ios folder, delete Podfile.lock and Pods folder and then execute pod install --repo-update
For the scenario where you already have an ios module partially written in Objective-C, and now just want to use Swift code alongside, than I suggest you to right click on ios in project window and choose Open iOS module in XCode this from context menu
Than you can just follow those instructions:
https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/importing_swift_into_objective-c
If your plan is to rewrite module in swift than I'd create a new project with the same name as your original one and turned on Swift support. Than I'd just copy the whole ios module to your original project.
First delete AppDelegate.h, AppDelegate.m, main.m files
And also remove main.m from build phases -> compile sources
After that run this command
flutter create -i swift .

How could I use pytest-html in Pycharm?

I have installed pytest-html and want to use it generate html report after run pytest in pycharm.
like this:
but it pops error:
ERROR: file not found: html=report\result.html
after run. So how to modify the arguments?
use
--html=Reports/Report.html --self-contained-html
in additional argument section of pycharm
If your using terminal to run the test try this instead this will work .
py.test --html=demoreport.html
If you want to run via pycharm, just by Right click on the project, follow below steps:
Add new configuration in Pycharm: At Configuration popup: Click '+', Python tests->pytest, provide "working directory" and "target" values with the path of the project.
Then after, Create a new file pytest.ini in the root folder of the project and provide the below values:
[pytest]
addopts = --html=reports/Report.html
python_files = tests/*.py
where, tests/*.py is a folder which contains all tests.
Now, just right click on your project and select Run 'pytest in ProjectFolderName' and you will find that report is created inside reports folder with name Report.html

Changing the project name

Is it possible changing the project name of a Flutter project? With project name I mean the name you provide when creating a flutter project flutter create name.
That depends on what you are trying to achieve. If you want to change the name of the app which is displayed in the mobile phones menu together with the app icon, you have to change the android:label in the android/app/src/main/AndroidManifest.xml (Code Sample 1) and the CFBundleName in the ios/Runner/Info.plist (Code Sample 2).
Last time I did this it took me ages to find out, why the name of the app was not changed in the list of currently running apps on Android. For that you also need to change the title of your MaterialApp (Code Sample 3).
For renaming it everywhere I would suggest to use search and replace in the project. If you do this, you have to choose a name without spaces or special characters. A project name 'new project' in the pubspec.yaml for example will break the build.
Code Sample 1:
<application
android:name="io.flutter.app.FlutterApplication"
android:label="New Name"
android:icon="#mipmap/ic_launcher">
Code Sample 2:
<key>CFBundleName</key>
<string>New Name</string>
Code Sample 3:
return new MaterialApp(
title: 'New Name'
...);
To change project name , you need to change it for all platforms .
You can use rename package to make it easy .
Run this command inside your flutter project root
flutter pub global run rename --appname "Application Name"
You can also specify the platform you want to apply the change
flutter pub global run rename --appname yourappname -t macOS // support also ios and android
or
Edit AndroidManifest.xml for Android and info.plist for iOS
Do a flutter clean and restart your application if you have a problem.
You can let flutter do this for you:
Using Terminal:
cd /path/to/existing/flutter_project
flutter create --project-name newprojname --org com.yourorgdomain .
Next change the name on line 1 of pubspec.yaml
Trash all generated folders to remove all references of the old project name.
NB - Some import statements will need to have their package references
updated manually to the new name. Review the project structure (in
your IDE) for any references to the old project name inside of IDE
generated files.
Recreate the trashed generated files by using flutter create . (including the space and period at the end) from within the project folder.
Now run flutter clean then debug as normal
Gotchas
You should expect that the project name still resides in all the generated folders for the various environments and in comments/strings. You can use a tool to search all files in the project or folder structure (e.g Cmd + Shift + F in Android Studio or ag {the_silver_searcher in Homebrew}) to find the old name and trash any generated folders including ios, windows, linux. After trashing the generated folders remember to re-create them by running flutter create .
To add to Rainer's answer, you may also have to change the com.example.myprojectname file under android/app/build.gradle
Also for the com.example.myprojectname, do not use underscores (ie:
com.example.my_project_name)
You may also have to find and replace the com.example.myproject name in the files project.pbxproj and MainActivity.java.
If you want to change the project name, simply change it in all these files.
build.gradle (app)
AndroidManifest.xml
MainActivity.java
Rebuild your app and you should be good to go.
1-flutter clean
2- Search and replace all OLD_PROJECT_NAME with NEW_PROJECT_NAME
3- Search for OLD_PROJECT_NAME in the project folder, and replace them with NEW_PROJECT_NAME one by one. Some of them are in the text files, make sure that you have changed them too.
If you are using Android Studio, follow these steps:
Right-click your project in the Project window (Alt+1 to show).
Click Refactor > Rename.
Enter new name.
This will rename the project's name on all required places except in the test directory.
Steps to rename your project: (For Editors that has search & replace across project feature)
Use VSCode's any other editor's search and replaceAll across entire project feature. But always turn on the match cases (for vscode: located on right side).
You have to do this two time as for the first you have to replace com.orgname.currentname with com.orgname.newname then
currentname with newname.
Rename the folders inside /android/app/src/main/kotlin/com/orgname/currentname to new /android/app/src/main/kotlin/com/orgname/newname.
If you're using only Java with flutter than just replace the kotlin with java.
rename the currentname.iml of your root project folder to newname.iml
Ctrl+Shift+R then Replace for one by one replace
or Replace All for all at a time. Be cautious , if your project name is similar with other variable or class name then don't do that. Before doing that check twice.
If you are using Android Studio, from the Menu > Edit > Find > Replace in files > Enter old and new name...
If you want to change the project name, simply change it in all these files.
build.gradle (app)
AndroidManifest.xml
MainActivity.java
Rebuild your app and you should be good to go.