how to get build/version code and name of your Android application android programmatically

Version your app

You can define default values for these settings by including them in the defaultConfig {} block, nested inside the android {} block of your module's build.gradle file. You can then override these default values for different versions of your app by defining separate values for individual build types or product flavors. The following build.gradle file shows the versionCode and versionName settings in the defaultConfig {} block, as well as the productFlavors {} block.

android {
  ...
  defaultConfig {
    ...
    versionCode 2
    versionName "1.1"
  }
  productFlavors {
    demo {
      ...
      versionName "1.1-demo"
    }
    full {
      ...
    }

versionCode

A positive integer used as an internal version number. This number is used only to determine whether one version is more recent than another, with higher numbers indicating more recent versions.
The versionCode is integer value used to easily differentiate between new and old app versions. App developers must increment versionCode value when they release updates to their apps in Android Market, so it can determine if users are using an old version of the app, and offer them to update it.

versionName

A string used as the version number shown to users. This setting can be specified as a raw string or as a reference to a string resource. It is a string containing a regular “release version” as seen in other desktop applications, such as “1.4.5” or “3.7”. The versionName is just a “human readable” version code.


android get app version code programmatically

Check android version name for your application using below code like. Version 1.0 .
private String getApplicationVersion() {
            String versionStr;
        try {
            PackageInfo pInfo = context.getPackageManager().getPackageInfo(getPackageName(), 0);
            versionStr= "Version " + pInfo.versionName;
} catch (Exception e) {
            e.printStackTrace();
        }
return versionStr;
    }

Use the below function to get android app version code:
private String getApplicationVersionCode() {
            String verCode;
        try {
            PackageInfo pInfo = context.getPackageManager().getPackageInfo(getPackageName(), 0);
            int verCode = pInfo.versionCode;
        } catch (Exception e) {
            e.printStackTrace();
        }
return verCode ;
    }

'versionCode' is deprecated as of API 28: Android 9.0 (Pie)


Android get screen width and height programmatically


Get bitmap from url android glide android

Get bitmap from Image using glide V4
Android  Code:
Glide.with(cxt)
  .asBitmap().load(imageUrl)
  .listener(new RequestListener<Bitmap>() {
      @Override
      public boolean onLoadFailed(@Nullable GlideException e, Object o, Target<Bitmap> target, boolean b) {
          Toast.makeText(cxt,getResources().getString(R.string.unexpected_error_occurred_try_again),Toast.LENGTH_SHORT).show();
          return false;
      }

      @Override
      public boolean onResourceReady(Bitmap bitmap, Object o, Target<Bitmap> target, DataSource dataSource, boolean b) {
          zoomImage.setImage(ImageSource.bitmap(bitmap));
          return false;
      }
  }
).submit();

Kotlin Code:
 Glide.with(this)
                .asBitmap().load("ImaegUrltODownload")
                .listener(object : RequestListener<Bitmap> {
                    override fun onResourceReady(
                            resource: Bitmap?,
                            model: Any?,
                            target: Target<Bitmap>?,
                            dataSource: DataSource?,
                            isFirstResource: Boolean
                    ): Boolean {

                        YourimageView.setImageBitmap(resource)

                        return false;
                    }

                    override fun onLoadFailed(
                            e: GlideException?,
                            model: Any?,
                            target: Target<Bitmap>?,
                            isFirstResource: Boolean
                    ): Boolean {
                        return false;
                    }
                }
                ).submit()

java.lang.NoClassDefFoundError Failed resolution of: Lorg/apache/http/ProtocolVersion Android (9) Pie

 I built App on Android studio targetSdkVersion 28 which targets Android P. The APK can be build ,but when I used it on device with OS Android (9) Pie, it throws below crash and see below detailed crash log for more details.

ANDROID CRASHLYTICS ERROR LOG:
Fatal Exception: java.lang.NoClassDefFoundError
Failed resolution of: Lorg/apache/http/ProtocolVersion;
com.google.android.gms.common.data.zac.b (:com.google.android.gms.dynamite_adsdynamite@14799081@14.7.99 (100400-223214910):3)
com.google.android.gms.common.data.zab.a (:com.google.android.gms.dynamite_adsdynamite@14799081@14.7.99 (100400-223214910):3)
com.google.android.gms.common.internal.AccountAccessor.a (:com.google.android.gms.dynamite_adsdynamite@14799081@14.7.99 (100400-223214910):19)
com.google.android.gms.ads.internal.util.aq.a (:com.google.android.gms.dynamite_adsdynamite@14799081@14.7.99 (100400-223214910):15)
com.google.android.gms.common.api.internal.TaskUtil.a (:com.google.android.gms.dynamite_adsdynamite@14799081@14.7.99 (100400-223214910):19)
com.google.android.gms.common.api.internal.TaskUtil.run (:com.google.android.gms.dynamite_adsdynamite@14799081@14.7.99 (100400-223214910):8)
Caused by java.lang.ClassNotFoundException
Didn't find class "org.apache.http.ProtocolVersion" on path: DexPathList[[zip file "/data/user_de/0/com.google.android.gms/app_chimera/m/00000055/AdsDynamite.apk"],nativeLibraryDirectories=[/data/user_de/0/com.google.android.gms/app_chimera/m/00000055/AdsDynamite.apk!/lib/arm64-v8a, /system/lib64]]
dalvik.system.BaseDexClassLoader.findClass (BaseDexClassLoader.java:169)
java.lang.ClassLoader.loadClass (ClassLoader.java:379)
com.bumptech.glide.load.engine.EngineKeyFactory.loadClass (:com.google.android.gms.dynamite_dynamiteloader@14799081@14.7.99 (100400-223214910):4)
java.lang.ClassLoader.loadClass (ClassLoader.java:312)
com.google.android.gms.common.data.zac.b (:com.google.android.gms.dynamite_adsdynamite@14799081@14.7.99 (100400-223214910):3)
com.google.android.gms.common.data.zab.a (:com.google.android.gms.dynamite_adsdynamite@14799081@14.7.99 (100400-223214910):3)
com.google.android.gms.common.internal.AccountAccessor.a (:com.google.android.gms.dynamite_adsdynamite@14799081@14.7.99 (100400-223214910):19)
com.google.android.gms.ads.internal.util.aq.a (:com.google.android.gms.dynamite_adsdynamite@14799081@14.7.99 (100400-223214910):15)
com.google.android.gms.common.api.internal.TaskUtil.a (:com.google.android.gms.dynamite_adsdynamite@14799081@14.7.99 (100400-223214910):19)
com.google.android.gms.common.api.internal.TaskUtil.run (:com.google.android.gms.dynamite_adsdynamite@14799081@14.7.99 (100400-223214910):8)

SOLUTION:
This link android-9.0-changes-28-->Apache HTTP client deprecation explains reason for adding the following to your AndroidManifest.xml:
<uses-library android:name="org.apache.http.legacy" android:required="false"/>

Similar errors : Need to specify requirement for Apache HTTP Legacy library
You can also face same error while using play-services-maps library. There are two solutions as below:

Solution 1: If you are working with com.google.android.gms:play-services-maps:16.0.0 or below then update the com.google.android.gms:play-services-maps library to the latest version. This is handled for you if you are using com.google.android.gms:play-services-maps:16.1.0 and is not necessary if your app is targeting a lower API level.
Or
Solution  2:  Include the below declaration within the <application> element of AndroidManifest.xml.
<uses-library
      android:name="org.apache.http.legacy"
      android:required="false" />
Refer the same included in the official docs: Specify requirement for Apache HTTP Legacy library
It's reported on issuetracker (Android bug tracker) :https://issuetracker.google.com/issues/79478779

More about Apache HTTP client deprecation:
With Android 6.0, we removed support for the Apache HTTP client. Beginning with Android 9, that library is removed from the bootclasspath and is not available to apps by default. To continue using the Apache HTTP client, apps that target Android 9 and above can add the following to their AndroidManifest.xml:
<uses-library android:name="org.apache.http.legacy" android:required="false">

Things to Note
 The android:required="false" attribute is required for apps that have a minimum SDK of 23 or lower, because on devices with API levels lower than 24, the org.apache.http.legacy library is not available. (On those devices, the Apache HTTP classes are available on the bootclasspath.) As an alternative to using the runtime Apache library, apps can bundle their own version of the org.apache.http library in their APK. If you do this, you must repackage the library (with a utility like Jar Jar) to avoid class compatibility issues with the classes provided in the runtime.

Fatal Exception: android.app.RemoteServiceException can't deliver broadcast

ANDROID App CRASH CRASHLYTICS LOG:
Fatal Exception: android.app.RemoteServiceException
can't deliver broadcast
android.app.ActivityThread$H.handleMessage (ActivityThread.java:1867)
android.os.Handler.dispatchMessage (Handler.java:106)
android.os.Looper.loop (Looper.java:164)
android.app.ActivityThread.main (ActivityThread.java:6753)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:482)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:807)
This error: android.app.remoteserviceexception android 8 was first visible on my App on device ONEPLUS A5000 with Android 8.1 Oreo

You can find the comment old code and the new added code from below snippet.

Send Broadcast(from Sender activity):
         try {
                                Intent broadcastIntent = new Intent();
                                broadcastIntent.setAction(mBroadcastStringAction);
//                                sendBroadcast(broadcastIntent);     //old code to send broadcast
   LocalBroadcastManager.getInstance(Application.get()).sendBroadcast(broadcastIntent);
                 }catch (Exception e){
                     e.printStackTrace();
                }

  Deceleration in Receiver Activity or fragment:
//declared the intent filter and the broadcast receiver instance
private IntentFilter mIntentFilter;
//Brroadcast-Action-String decleration
public static final String mBroadcastStringAction = "com.example.apppackage.broadcast.string";

private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent!=null && intent.getAction().equals(mBroadcastStringAction)) {
                Log.e("BaseActivity","Your broadcast recived");
// set functionalities
      }
        }
    };

in onCreate of Receiver Activity or fragment:
//define the intent filter action
        mIntentFilter = new IntentFilter();
        mIntentFilter.addAction(mBroadcastStringAction);
//register broadcast reciver
//        getActivity().registerReceiver(mReceiver, mIntentFilter);   // old code
        LocalBroadcastManager.getInstance(Application.get()).registerReceiver(mReceiver, mIntentFilter);

in OnPuase() method of Receiver Activity:
//unregister broadcast reciver
//        getActivity().unregisterReceiver(mReceiver); // old code
        LocalBroadcastManager.getInstance(Application.get()).unregisterReceiver(mReceiver);

To find more on LocalBroadcastManager Refer:
LocalBroadcastManager to send and receive broadcast in Android

Popular Posts