As I describe here, I use a script to rewrite a header file with my current Subversion revision number. That revision number is stored in the kRevisionNumber constant. I can then access the version and revision number using something similar to the following:
[NSString stringWithFormat:@"Version %@ (%@)", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"], kRevisionNumber]
which will create a string of the format "Version 1.0 (51)".
I find a far easier solution is to forget all the if
checks all over the place and just use ProGuard to strip out any Log.d()
or Log.v()
method calls when we call our Ant release
target.
That way, we always have the debug info being output for regular builds and don't have to make any code changes for release builds. ProGuard can also do multiple passes over the bytecode to remove other undesired statements, empty blocks and can automatically inline short methods where appropriate.
For example, here's a very basic ProGuard config for Android:
-dontskipnonpubliclibraryclasses
-dontobfuscate
-forceprocessing
-optimizationpasses 5
-keep class * extends android.app.Activity
-assumenosideeffects class android.util.Log {
public static *** d(...);
public static *** v(...);
}
So you would save that to a file, then call ProGuard from Ant, passing in your just-compiled JAR and the Android platform JAR you're using.
See also the examples in the ProGuard manual.
Update (4.5 years later): Nowadays I used Timber for Android logging.
Not only is it a bit nicer than the default Log
implementation — the log tag is set automatically, and it's easy to log formatted strings and exceptions — but you can also specify different logging behaviours at runtime.
In this example, logging statements will only be written to logcat in debug builds of my app:
Timber is set up in my Application
onCreate()
method:
if (BuildConfig.DEBUG) {
Timber.plant(new Timber.DebugTree());
}
Then anywhere else in my code I can log easily:
Timber.d("Downloading URL: %s", url);
try {
// ...
} catch (IOException ioe) {
Timber.e(ioe, "Bad things happened!");
}
See the Timber sample app for a more advanced example, where all log statements are sent to logcat during development and, in production, no debug statements are logged, but errors are silently reported to Crashlytics.
Best Answer
Update
version:A.B.C+X
inpubspec.yaml
.For Android:
A.B.C
represents theversionName
such as1.0.0
.X
(the number after the+
) represents theversionCode
such as1
,2
,3
, etc.Do not forget to execute
flutter build apk
orflutter run
after this step, because: When you runflutter build apk
orflutter run
after updating thisversion
in thepubspec
file, theversionName
andversionCode
inlocal.properties
are updated which are later picked up in thebuild.gradle (app)
when you build your flutter project usingflutter build apk
orflutter run
which is ultimately responsible for setting theversionName
andversionCode
for the apk.For iOS:
A.B.C
represents theCFBundleShortVersionString
such as1.0.0
.X
(the number after the+
) represents theCFBundleVersion
such as1
,2
,3
, etc.Do not forget to execute
flutter build ipa
orflutter run
after this step