Last active
February 25, 2025 06:01
-
-
Save jhy/d5a7fda3f0ca529cce86290f3311bf7e to your computer and use it in GitHub Desktop.
Example Android app build.gradle, showing desugaring support to enable jsoup on API level 21
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<ScrollView | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<TextView | |
android:id="@+id/textView" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:padding="16dp" | |
android:text="Loading..." | |
android:textSize="16sp" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
</ScrollView> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'com.android.application' | |
allprojects { | |
repositories { | |
maven { | |
url "https://maven.google.com" | |
} | |
mavenCentral() | |
} | |
} | |
android { | |
defaultConfig { | |
// Required when setting minSdkVersion to 20 or lower | |
multiDexEnabled true | |
} | |
compileOptions { | |
compileSdkVersion 33 | |
coreLibraryDesugaringEnabled true | |
sourceCompatibility JavaVersion.VERSION_1_8 | |
targetCompatibility JavaVersion.VERSION_1_8 | |
} | |
defaultConfig { | |
applicationId "org.jsoup.benchmark.android" | |
minSdkVersion 21 | |
targetSdkVersion 33 | |
} | |
buildTypes { | |
release { | |
minifyEnabled false | |
} | |
} | |
namespace 'org.jsoup.benchmark.android' | |
lint { | |
abortOnError false | |
} | |
} | |
dependencies { | |
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs_nio:2.1.4' | |
implementation ( | |
'com.squareup:otto:1.3.8', | |
'com.android.support:support-core-utils:28.0.0', | |
'com.android.support:support-annotations:28.0.0', | |
files('/Users/jhy/.m2/repository/org/jsoup/jsoup/1.19.1-SNAPSHOT/jsoup-1.19.1-SNAPSHOT.jar'), | |
//'org.jsoup:jsoup:1.15.3', | |
//'org.jsoup:jsoup:1.11.3' | |
'com.google.android.material:material:1.4.0', | |
) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.jsoup.benchmark.android; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.os.Bundle; | |
import android.text.method.ScrollingMovementMethod; | |
import android.util.Log; | |
import android.widget.TextView; | |
import org.jsoup.Jsoup; | |
import org.jsoup.nodes.Document; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.util.concurrent.ExecutorService; | |
import java.util.concurrent.Executors; | |
public class LoadGzActivity extends Activity { | |
// Tests for Android Desugar NIO. https://github.com/jhy/jsoup/issues/2270 | |
private TextView textView; | |
private static final String TAG = "LoadGzActivity"; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_load_gz); | |
textView = findViewById(R.id.textView); | |
textView.setMovementMethod(new ScrollingMovementMethod()); | |
ExecutorService executor = Executors.newSingleThreadExecutor(); | |
executor.execute(() -> { | |
try { | |
File gzFile = getGzFile(getApplicationContext()); | |
Path gzPath = gzFile.toPath(); | |
Document doc = Jsoup.parse(gzPath, "UTF-8"); | |
runOnUiThread(() -> textView.setText(doc.outerHtml())); | |
} catch (Throwable e) { | |
Log.e(TAG, "Error loading or parsing file", e); | |
runOnUiThread(() -> textView.setText("Error loading or parsing file: " + e.getMessage())); | |
} | |
}); | |
executor.shutdown(); | |
} | |
public static File getGzFile(Context context) throws IOException { | |
InputStream in = context.getAssets().open("unicode-chars.html.bin"); | |
File outFile = new File(context.getFilesDir(), "unicode-chars.html.gz"); | |
if (!outFile.exists()) { | |
// Copy once | |
try (OutputStream out = Files.newOutputStream(outFile.toPath())) { | |
byte[] buffer = new byte[8192]; | |
int len; | |
while ((len = in.read(buffer)) != -1) { | |
out.write(buffer, 0, len); | |
} | |
} finally { | |
in.close(); | |
} | |
} | |
return outFile; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment