添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

In a recent webinar, we welcomed Olivier Le Goaër, Associate professor at Université de Pau et des Pays de l’Adour (Pau, France), to discuss greenIT for mobile applications (replay is available in French here ). Indeed, Olivier started in 2019 to work on a project to make native mobile apps more sustainable, environmentally, and socially acceptable.

He aims to build a repository of best practices for developers that can be detected in the source code so they’re all actionable and low-level. So he voluntarily excluded rules and best practices that can’t be detected, for instance, if they’re too high level. At this date (Feb 2023), this knowledge base only targets Android , but an announcement was made about iOS support, meaning new rules will come on iOS. He came up with a repository of 42 coding rules .

Note: make sure register to our next live sessions for more best coding practices.

Overview of the best coding practices for sustainable Android apps

To provide insights into the content of this repository, let’s browse some of the code smells you might find in your Android code.

Tune Constrained Workers

You can request tasks to be executed in the app with the WorkRequest class:

Constraints constraints = new Constraints.Builder().setRequiresCharging(true).build(); WorkRequest myWorkRequest = new OneTimeWorkRequest.Builder(MyWork.class).setConstraints(constraints).build(); WorkManager.getInstance(myContext).enqueue(myWorkRequest); import android.location.LocationManager; LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); import com.google.android.gms.location.FusedLocationProviderClient; FusedLocationProviderClient fusedClient = LocationServices.getFusedLocationProviderClient(this); fusedClient.requestLocationUpdates(mRequest, mCallback, null); sensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE); accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); sensorManager.registerListener(this, accelerometer, 20000); sensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE); accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); sensorManager.registerListener(this, accelerometer, 20000);

The documentation describes this as the “ Maximum time in microseconds that events can be delayed before being reported to the application. A large value allows reducing the power consumption associated with the sensor”. A value of 0 means calling the first method above. Think of sending events as a single batch instead of multiple small batches.

Avoid wide version range

To avoid “software obesity”, meaning embedding too many unnecessary components, you should try to restrict the version ranges of the SDK versions:

Share your best coding practices now

This knowledge base of best practices can be theoretically implemented in static analysis tools. For instance, an implementation called ecoCode Mobile exists for SonarQube., if you want to give it a try. This catalog is also available on our public Hub of best practices, and can be used in Promyze.

If, in your context, greenIT is a significant concern and your organization aims at raising developers’ skills on that topic, Promyze can be an excellent alley to centralize and share your best greenIT practices in your context.

Also, make sure register to our next live sessions for more best coding practices.

NEW: Introducing AI for generating coding practices and discussions for your Promyze workshops

Read more