Programming For Mobile Applicationsss

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

Programming For Mobile Applications

Assignment – 4

CASE STUDY

Name: Vignesh Vasu


Reg No.:727822TUCS252
Dept: III CSE C

Android APK Build Process and Optimization Guide


1. Build Process Overview

1.1 Basic Build Steps


Gradle:
android {
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-
android-optimize.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
}
}

1.2 Complete Build Pipeline


1. Compile Source Code
- Java/Kotlin → .class files
- AIDL → .java → .class

2. Transform Class Files


- Dex compiler → .dex files
- Bytecode optimization

3. Package Resources
- Compile resources (aapt2)
- Generate R.java
- Create resources.arsc

4. Bundle Assets
- Copy raw assets
- Compress if configured

5. Create APK
- Combine DEX, resources, assets
- Sign APK
- Zipalign optimization
2. Key Configuration Files
2.1 build.gradle
Gradle:
android {
compileSdkVersion 34
defaultConfig {
applicationId "com.example.app"
minSdkVersion 24
targetSdkVersion 34
versionCode 1
versionName "1.0"

// Multidex settings
multiDexEnabled true

// Vector drawable support


vectorDrawables.useSupportLibrary = true
}

// Build type specific configurations


buildTypes {
release {
// Optimization flags
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-
android-optimize.txt'),
'proguard-rules.pro'
}
}
// Build variants
flavorDimensions "version"
productFlavors {
free {
dimension "version"
applicationIdSuffix ".free"
}
paid {
dimension "version"
applicationIdSuffix ".paid"
}}}
2.2 ProGuard Rules
Proguard:
# Keep important classes
-keep class com.example.app.api.** { *; }
-keep interface com.example.app.api.** { *; }

# Keep model classes


-keepclassmembers class com.example.app.models.** {
*;
}

# Keep custom view constructors


-keepclassmembers class * extends android.view.View {
<init>(android.content.Context);
<init>(android.content.Context, android.util.AttributeSet);
}

# Remove logging in release


-assumenosideeffects class android.util.Log {
public static *** d(...);
public static *** v(...);
}
3. Optimization Strategies
3.1 Code Optimization
```gradle
android {
buildTypes {
release {
// Enable R8 optimization
minifyEnabled true

// Remove unused resources


shrinkResources true

// Enable optimization passes


proguardFiles getDefaultProguardFile('proguard-
android-optimize.txt')
}
}
}

3.2 Resource Optimization


Xml:
<!-- res/xml/network_security_config.xml -->
<network-security-config>
<base-config cleartextTrafficPermitted="false">
<trust-anchors>
<certificates src="system" />
</trust-anchors>
</base-config>
</network-security-config>
```

3.3 Native Library Optimization


Gradle:
android {
defaultConfig {
ndk {
// Specify ABI filters
abiFilters 'armeabi-v7a', 'arm64-v8a'
}
}

splits {
abi {
enable true
reset()
include 'armeabi-v7a', 'arm64-v8a'
universalApk false
}
}
}
```

4. Performance Optimization Checklist

4.1 Build Configuration


- [x] Enable minification
- [x] Enable resource shrinking
- [x] Configure ABI splits
- [x] Enable multidex if needed
- [x] Configure proper ProGuard rules
- [x] Set up signing configs
- [x] Enable vector drawable support
- [x] Configure build variants

4.2 Resource Management


- [x] Optimize image assets
- [x] Remove unused resources
- [x] Use appropriate image formats
- [x] Implement density-specific resources
- [x] Minimize raw resource size

4.3 Code Quality


- [x] Remove debug logs
- [x] Optimize dependencies
- [x] Remove unused libraries
- [x] Implement proper ProGuard rules
- [x] Handle multidex configuration

5. Common Issues and Solutions


5.1 Build Issues
gradle:
android {
// Fix Dalvik 64K method limit
defaultConfig {
multiDexEnabled true
}

// Fix resource merging conflicts


sourceSets {
main {
res.srcDirs = ['src/main/res']
}
}
}
```

5.2 ProGuard Issues


proguard
# Fix reflection issues
-keepattributes *Annotation*
-keepattributes Signature
-keepattributes Exception

# Fix serialization issues


-keepclassmembers class * implements java.io.Serializable {
private static final java.io.ObjectStreamField[]
serialPersistentFields;
private void writeObject(java.io.ObjectOutputStream);
private void readObject(java.io.ObjectInputStream);
java.lang.Object writeReplace();
java.lang.Object readResolve();
}
```

6. Final Testing Steps

1. Build Release APK


```bash
./gradlew assembleRelease
```

2. Test APK
- Install on various devices
- Test all features
- Verify ProGuard configuration
- Check resource loading
- Validate signing

3. Performance Testing
- Launch time analysis
- Memory usage monitoring
- Battery consumption tests
- Network usage validation

You might also like