Es gab bereits einen ähnlichen
Artikel auf Habr.com, der beweist, dass Sie eine APK-Datei von 1,5 MB auf 1757 Byte oder weniger verkleinern können. Der Zweck dieses Artikels besteht darin, die Größe der Anwendung auf ein angemessenes Maß zu reduzieren, ihre Funktionalität beizubehalten und einige der Feinheiten und impliziten Momente hervorzuheben.
Starten Sie
Erstellen Sie ein Projekt in Android Studio und wählen Sie Leere Aktivität. Ersetzen Sie dann in der Datei styles.xml Activity durch ActionBar
Theme.AppCompat.Light.DarkActionBar
auf Aktivität ohne ActionBar
Theme.AppCompat.Light.NoActionBar
Das Ergebnis:

Im APK-Analysator sehen wir Folgendes:

Das APK wiegt also 1,5 MB, obwohl nur die Worte „Hallo Welt!“ Angezeigt werden.
Stufe Eins (Minimierung)
In die Datei build.gradle schreiben wir:
android { buildTypes { debug { minifyEnabled true shrinkResources true proguardFiles getDefaultProguardFile ('proguard-android.txt'), 'proguard-rules.pro' } } }
Wir werden Android Studio synchronisieren, damit die Änderungen wirksam werden.
Erklärung:
minifyEnabled true
Entfernen Sie unnötigen Code in der Anwendung
shrinkResources true
Entfernen Sie nicht verwendete Ressourcen aus der APK.
Das APK-Gewicht betrug 960 KB, keine Änderung der Leistung.
Stufe zwei (Funktionalität hinzufügen)
Fügen Sie Funktionen hinzu, damit die Anwendung sinnvoll ist, z. B. einen Clicker.
Activity_main.xml-Code <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:textSize="20dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <ImageButton android:id="@+id/imageButton" android:layout_width="90dp" android:layout_height="90dp" android:layout_marginBottom="32dp" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:background="#000000FF" android:cropToPadding="false" android:scaleType="fitXY" android:visibility="visible" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:srcCompat="@mipmap/ic_launcher_round" /> </android.support.constraint.ConstraintLayout>
Code MainActivity.java import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.MotionEvent; import android.view.View; import android.widget.ImageButton; import android.widget.TextView; public class MainActivity extends AppCompatActivity { SharedPreferences Settings; ImageButton button; TextView text; int num = 31; View.OnTouchListener on = new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { num--; if(num > 0) { text.setText(Integer.toString(num)); } else { num = 31; text.setText(", "); } } return false; } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Settings = getSharedPreferences("settings", Context.MODE_PRIVATE); if (Settings.contains("left")) num = Settings.getInt("left", 0); button = findViewById(R.id.imageButton); button.setOnTouchListener(on); text = findViewById(R.id.number); if(num > 0) { text.setText(Integer.toString(num)); } else { num = 31; text.setText(", "); } } @Override protected void onPause() { super.onPause(); SharedPreferences.Editor editor = Settings.edit(); editor.putInt("left", num); editor.apply(); } }
Der Antrag hat das folgende Formular erhalten:

Anwendungsgröße 1,1 MB, eine Zunahme von 140 KB.
Stufe drei (entfernen Sie android.support und AppCompat)
Derzeit zeigt der APK-Analysator Folgendes an:

public class MainActivity extends AppCompatActivity
durch die
public class MainActivity extends Activity
in MainActivity.java. Drücken Sie Alt + Eingabetaste, damit Android Studio die Bibliotheken importiert.
Die Anwendungsgröße hat sich nicht geändert, aber ...

Wo ist der Knopf?
In der Tat ist alles in Ordnung, sie blieb anklickbar. Aber sie hat einfach kein Bild.
Gehen Sie zu activity_main.xml und suchen Sie die folgende Zeile
app:srcCompat="@mipmap/ic_launcher_round"
und ändern Sie es in
android:src="@mipmap/ic_launcher_round"
Jetzt ist alles in Ordnung:

Die Größe hat sich aber nicht geändert.
Wir gehen erneut zu build.gradle und löschen den Abhängigkeitsblock:
dependencies { }
Und synchronisiere Android Studio ... mit Fehlern.
1. Wechseln Sie zur Datei res / values / styles.xml und ersetzen Sie den gesamten Inhalt durch den folgenden Code:
<resources> <style name="AppTheme" parent="android:Theme.DeviceDefault.NoActionBar"> </style> </resources>
2. ConstraintLayout, das in Android Studio verwendet wird, hängt von android.support ab, das bereits aus dem
dependencies
entfernt wurde. Daher werden wir ConstraintLayout durch RelativeLayout ersetzen, das nicht von android.support abhängig ist.
Activity_main.xml Code: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFF"> <TextView android:id="@+id/number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="Hello World!" android:textSize="20dp" /> <ImageButton android:id="@+id/imageButton" android:layout_width="90dp" android:layout_height="90dp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="32dp" android:background="#000000FF" android:scaleType="fitXY" android:src="@mipmap/ic_launcher_round" android:visibility="visible" /> </RelativeLayout>
Wir kompilieren die APK und sehen das Ergebnis:

Unsere APK wiegt 202 KB und ist damit 7,5-mal kleiner als ihre ursprüngliche Größe.
Die meisten Ressourcen sind belegt, und wir werden uns darum kümmern.
1. Löschen Sie die Dateien im Ordner res / drawable und löschen Sie den Ordner res / mipmap
2. Zeichnen Sie Ihr Symbol und Ihre Schaltfläche und verkleinern Sie sie mit ImageOptim.
Button:

Symbol:
3. Laden Sie sie in Android Studio herunter. Wählen Sie sie dazu im Ordner aus, drücken Sie Strg + C, gehen Sie zu Android Studio, wählen Sie den Ordner res / drawable aus und drücken Sie Strg + V, wonach Andoid Studio verschiedene Optionen zum Übertragen der Bilder anbietet von ihrer Erlaubnis.
Im Zeichenordner können Sie die Datei umbenennen, indem Sie Refactor -> Rename auswählen.
Das Anwendungssymbol hat den Namen i.png, das Bild für die Schaltfläche b.png
4. Wechseln Sie nun zur Datei AndroidManifest.xml, Zeilen
android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round"
ersetzen durch
android:icon="@drawable/i" android:roundIcon="@drawable/i"
Ersetzen Sie in der Datei activity_main.xml das Feld in ImageButton
android:src="@mipmap/ic_launcher_round"
auf
android:src="@drawable/b"
Zusammenfassung
Die Gesamtgröße der Datei beträgt 13,4 KB, was 112-mal weniger ist als die ursprüngliche Größe!
Endgültiger Code MainActivity.java import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.widget.ImageButton; import android.widget.TextView; public class MainActivity extends Activity { SharedPreferences Settings; ImageButton button; TextView text; int num = 31; View.OnTouchListener on = new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { num--; if(num > 0) { text.setText(Integer.toString(num)); } else { num = 31; text.setText(", "); } } return false; } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.m); Settings = getSharedPreferences("settings", Context.MODE_PRIVATE); if (Settings.contains("left")) num = Settings.getInt("left", 0); text = findViewById(R.id.number); button = findViewById(R.id.button); button.setOnTouchListener(on); if(num > 0) { text.setText(Integer.toString(num)); } else { num = 31; text.setText(", "); } } @Override protected void onPause() { super.onPause(); SharedPreferences.Editor editor = Settings.edit(); editor.putInt("left", num); editor.apply(); } }
Ergebniscode activity_main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFF"> <TextView android:id="@+id/number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="Hello World!" android:textSize="20dp" /> <ImageButton android:id="@+id/button" android:layout_width="90dp" android:layout_height="90dp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="32dp" android:background="#000000FF" android:scaleType="fitXY" android:src="@drawable/b" android:visibility="visible" /> </RelativeLayout>
Dies beendet eine vernünftige Verringerung der APK-Datei, dann gibt es eine Anweisung, die Anwendung zum Nachteil der einfachen Entwicklung weiter zu reduzieren.
Ressourcen löschen
Löschen Sie den Ordner res / values. In der Datei AndroidManifest.xml ersetzen wir den Anwendungsblock durch den folgenden Code:
<application android:icon="@drawable/i" android:roundIcon="@drawable/i" android:label="Clicker" android:theme="@style/android:Theme.DeviceDefault.NoActionBar"> <activity android:name=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /></intent-filter> </activity> </application>
Wir ersetzen auch die Bezeichner durch Einbuchstaben und benennen die Datei activity_main.xml in m.xml um
Ändern Sie die Klickverarbeitung:
Löschen Sie die Zeile
button.setOnTouchListener(on);
in MainLayout.java.
Funktion ersetzen
View.OnTouchListener on = new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { num--; if(num > 0) { text.setText(Integer.toString(num)); } else { num = 31; text.setText(", "); } } return false; } };
auf
public void o(View v) { num--; if(num > 0) { text.setText(Integer.toString(num)); } else { num = 31; text.setText(", "); } }
Fügen Sie in der Datei m.xml (ehemals activity_main) in der ImageButton-Struktur die Zeile hinzu
android:onClick="o"
Die Gesamtgröße betrug 10,2 KB und war 147-mal kleiner als die ursprüngliche Größe. Ich denke, das ist ein gutes Ergebnis.

Code MainActivity.java import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.widget.ImageButton; import android.widget.TextView; public class MainActivity extends Activity { SharedPreferences Settings; ImageButton button; TextView text; int num = 31; public void o(View v) { num--; if(num > 0) { text.setText(Integer.toString(num)); } else { num = 31; text.setText(", "); } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.m); Settings = getSharedPreferences("settings", Context.MODE_PRIVATE); if (Settings.contains("left")) num = Settings.getInt("left", 0); text = findViewById(R.id.n); button = findViewById(R.id.b); if(num > 0) { text.setText(Integer.toString(num)); } else { num = 31; text.setText(", "); } } @Override protected void onPause() { super.onPause(); SharedPreferences.Editor editor = Settings.edit(); editor.putInt("left", num); editor.apply(); } }
M.xml-Code <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFF"> <TextView android:id="@+id/n" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:textColor="#000000" android:textSize="20dp" /> <ImageButton android:id="@+id/b" android:layout_width="90dp" android:layout_height="90dp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="32dp" android:background="#000000FF" android:scaleType="fitXY" android:src="@drawable/b" android:onClick="o" android:visibility="visible" /> </RelativeLayout>