Skip to content

Commit

Permalink
Updated Android examples for gradle build and proguard configs
Browse files Browse the repository at this point in the history
  • Loading branch information
benfortuna committed Dec 2, 2023
1 parent 4757b00 commit ae21dcc
Showing 1 changed file with 164 additions and 53 deletions.
217 changes: 164 additions & 53 deletions docs/android.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,120 @@
# Android Support

You can use iCal4j directly in your Android applications. Instructions for adding third-party libraries in Eclipse available [here](http://code.google.com/android/kb/commontasks.html#addexternallibrary).

## Gradle/AndroidStudio

You can use iCal4j with your AndroidStudio/gradle buildfile.
This is a working build.gradle example taken from https://github.com/k3b/CalendarIcsAdapter

apply plugin: 'android'
This is a working build.gradle example taken from https://github.com/k3b/calef/blob/master/app/build.gradle

```groovy
apply plugin: 'com.android.application'
configurations {
// referenced in some portable lib. use android internal instead
compile.exclude group: 'commons-logging'
android {
compileSdk 34
packagingOptions {
jniLibs {
excludes += ['META-INF/groovy/**', 'zoneinfo-global/**']
}
resources {
excludes += ['META-INF/LICENSE.txt', 'META-INF/NOTICE.txt', '**/package-info.java', 'META-INF/groovy-release-info.properties', 'META-INF/INDEX.LIST', 'META-INF/groovy/**', 'zoneinfo-global/**', 'org/apache/commons/codec/language/bm/*.txt']
}
}
android {
compileSdkVersion 19
buildToolsVersion '20'
packagingOptions {
// prevent duplicate files
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
defaultConfig {
applicationId "de.k3b.android.calef"
// Android 4.0 (api14)
minSdkVersion 14
//noinspection EditedTargetSdkVersion
targetSdkVersion 34
//!!! andoird buildToolsVersion 19.0.3 does not support exclude with wildcards yet :-(
exclude('org/apache/commons/codec/language/bm/*.txt') // 124 files (uncompressed 215kb) with unused language specific rules
}
versionCode 6
versionName "1.1.2.1"
defaultConfig {
minSdkVersion 7
targetSdkVersion 19
}
// 1.1.2.1 (6) maintanance Updated libs and compileSdk
// 1.1.2 (5) Fixed Initialisation Error
// 1.1.1 (4) reordered Settings; Fixed Initialisation Error
// 1.1.0 (3) added optional message prefix ("I have added this to my calendar")
// 1.0.1 (2) fixed translation issues
// 1.0.0 (1) initial Release translated in en and de
}
dependencies {
compile 'org.mnode.ical4j:ical4j:1.0.5'
compile 'backport-util-concurrent:backport-util-concurrent:3.1'
compile 'commons-codec:commons-codec:1.8'
compile 'commons-lang:commons-lang:2.6'
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
resValue "string", "debug_title", ""
}
debug {
minifyEnabled false
applicationIdSuffix ".debug"
versionNameSuffix "-DEBUG"
resValue "string", "debug_title", "DEBUG-" + getDate()
}
}
compileOptions {
// java 8 needed for android-4.x
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
namespace 'de.k3b.android.calef'
lint {
abortOnError false
// checkReleaseBuilds false
// http://stackoverflow.com/questions/31350350/generating-signed-apk-error7-missingtranslation-in-build-generated-res-gen
// MissingTranslation : not all crowdwin translations are complete so ignore them
// ValidFragment : local (dialog-)fragment class for customized directory picker is fragile but on rotation code makes sure that dialog is closed.
// disable 'MissingTranslation','ValidFragment', 'ContentDescription', 'AndroidLintPluralsCandidate', 'AndroidLintRtlHardcoded'
// disable 'MissingTranslation','ValidFragment', 'ContentDescription', 'ExpiredTargetSdkVersion'
disable 'MissingTranslation', 'ContentDescription', 'ExpiredTargetSdkVersion'
}
}
dependencies {
implementation 'androidx.annotation:annotation:1.7.0'
// ical4j see https://ical4j.github.io/ical4j-user-guide/examples/
// implementation 'org.mnode.ical4j:ical4j:3.1.0' // requires api-26 + java11
// implementation 'org.mnode.ical4j:ical4j:3.0.29' // requires api-26 + java11
// implementation 'org.mnode.ical4j:ical4j:1.0.8' // most recent version 1.x: compiles with api-7 + java6
//noinspection GradleDependency
// most recent version 2.x: compiles with api-14 + java8
implementation 'org.mnode.ical4j:ical4j:2.2.7'
// required by ical4j R8-minify: must be present in order to be removed :-/
implementation 'javax.cache:cache-api:1.1.1'
implementation 'backport-util-concurrent:backport-util-concurrent:3.1'
// do not automatically update to 20041127.091804 which is much older that 1.16.0
//noinspection GradleDependency
implementation 'commons-codec:commons-codec:1.16.0'
// do not automatically update to 20030203.000129 which is much older that 2.6
//noinspection GradleDependency
implementation 'commons-lang:commons-lang:2.6'
testImplementation 'junit:junit:4.13.2'
implementation 'org.slf4j:slf4j-api:2.0.9'
testImplementation 'org.slf4j:slf4j-simple:2.0.9'
// causes warning in unittests: "SLF4J: Class path contains multiple SLF4J bindings."
implementation 'uk.uuid.slf4j:slf4j-android:2.0.9-0'
// uses app/src/main/resources/uk/uuid/slf4j/android/config.properties
}
static def getDate() {
def date = new Date()
def formattedDate = date.format('yyyyMMdd-HH:mm')
return formattedDate
}
```

## Eclipse

Expand Down Expand Up @@ -158,37 +235,71 @@ As a result I *believe* (untested) that you won't need to include these dependen

## Reducing Android-footprint by using proguard

ical4j has several groovy classes that are not supported by android
iCal4j has several groovy classes that are not supported by android,
and fortunately you can live without them under android.

You can remove unused staff from your android app using proguard.

Here is a working example from https://github.com/k3b/CalendarIcsAdapter

# proguard-rules.txt
#
## ical4j also contains groovy code which is not used in android
-dontwarn groovy.**
-dontwarn org.codehaus.groovy.**
-dontwarn org.apache.commons.logging.**
-dontwarn sun.misc.Perf

-dontnote com.google.vending.**
-dontnote com.android.vending.licensing.**

###################
# Get rid of #can't find referenced method in library class java.lang.Object# warnings for clone() and finalize()
# Warning: net.fortuna.ical4j.model.CalendarFactory: can't find referenced method 'void finalize()' in library class java.lang.Object
# Warning: net.fortuna.ical4j.model.ContentBuilder: can't find referenced method 'java.lang.Object clone()' in library class java.lang.Object
# for details see http://stackoverflow.com/questions/23883028/how-to-fix-proguard-warning-cant-find-referenced-method-for-existing-methods
-dontwarn net.fortuna.ical4j.model.**

###############
# I use proguard only to remove unused stuff and to keep the app small.
# I donot want to obfuscate (rename packages, classes, methods, ...) since this is open source
-keepnames class ** { *; }
-keepnames interface ** { *; }
-keepnames enum ** { *; }
You can remove unused classes from your android app using proguard.

Here is a working example from https://github.com/k3b/calef/blob/master/app/proguard-rules.pro

```
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
#
## ical4j also contains groovy code which is not used in android
-dontwarn groovy.**
-dontwarn org.codehaus.groovy.**
-dontwarn org.apache.commons.logging.**
-dontnote com.google.vending.**
-dontnote com.android.vending.licensing.**
## log4j: remove net.fortuna.ical4j.util.JCacheTimeZoneCache.** that requires javax.cache.**
## use MapTimeZoneCache instead
-assumenosideeffects class net.fortuna.ical4j.util.JCacheTimeZoneCache
-assumenosideeffects class javax.cache.Cache
-assumenosideeffects class javax.cache.CacheManager
-assumenosideeffects class javax.cache.Caching
-assumenosideeffects class javax.cache.configuration.Configuration
-assumenosideeffects class javax.cache.configuration.MutableConfiguration
-assumenosideeffects class javax.cache.spi.CachingProvider
-keep class net.fortuna.ical4j.util.MapTimeZoneCache
###################
# Get rid of #can't find referenced method in library class java.lang.Object# warnings for clone() and finalize()
# Warning: net.fortuna.ical4j.model.CalendarFactory: can't find referenced method 'void finalize()' in library class java.lang.Object
# Warning: net.fortuna.ical4j.model.ContentBuilder: can't find referenced method 'java.lang.Object clone()' in library class java.lang.Object
# for details see http://stackoverflow.com/questions/23883028/how-to-fix-proguard-warning-cant-find-referenced-method-for-existing-methods
-dontwarn net.fortuna.ical4j.model.**
###############
# I use proguard only to remove unused stuff and to keep the app small.
# I donot want to obfuscate (rename packages, classes, methods, ...) since this is open source
-keepnames class ** { *; }
-keepnames interface ** { *; }
-keepnames enum ** { *; }
```

Statistics:

Expand Down

0 comments on commit ae21dcc

Please sign in to comment.