Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for split files built by Instant Run #739

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
10 changes: 5 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ task jacocoRootReport(type: JacocoReport, group: 'Coverage reports') {
description = 'Generates an aggregate report from all subprojects'
dependsOn(subprojects.jacocoTestReport)

additionalSourceDirs = files('library/src/main/java')
sourceDirectories = files('library/src/main/java')
classDirectories = files('library/build/intermediates/classes/debug')
executionData = files(subprojects.jacocoTestReport.executionData)
getAdditionalSourceDirs().setFrom(files('src/main/java'))
getSourceDirectories().setFrom(files('src/main/java'))
getClassDirectories().setFrom(files('build/intermediates/classes/debug'))
getExecutionData().setFrom(files(subprojects.jacocoTestReport.executionData))

reports {
html.enabled = true
Expand All @@ -90,7 +90,7 @@ task jacocoRootReport(type: JacocoReport, group: 'Coverage reports') {
}

coveralls {
sourceDirs = files('library/src/main/java').flatten()
sourceDirs = files('src/main/java').flatten()
jacocoReportPath = "${buildDir}/reports/jacoco/jacocoRootReport/jacocoRootReport.xml"
}

Expand Down
20 changes: 12 additions & 8 deletions library/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
apply plugin: 'com.android.library'
apply from: '../maven_push.gradle'
//apply from: '../maven_push.gradle'

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
compileSdkVersion 30
buildToolsVersion '30.0.3'

defaultConfig {
minSdkVersion 9
targetSdkVersion 23
minSdkVersion 16
targetSdkVersion 30
}
buildTypes {
release {
Expand All @@ -20,15 +20,19 @@ android {
lintOptions {
abortOnError false
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
}

dependencies {
testCompile 'junit:junit:4.12'
testCompile 'org.robolectric:robolectric:3.0'
testImplementation 'androidx.test:core:1.4.0'
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.robolectric:robolectric:4.5.1'
}

task libraryJar(type: Jar) {
dependsOn assembleRelease
from android.sourceSets.main.java.srcDirs,
['build/intermediates/classes/release/'] // Add the release classes into the jar
baseName 'sugar'
Expand Down
4 changes: 1 addition & 3 deletions library/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.orm.dsl"
android:versionCode="1"
android:versionName="1.3.1">
package="com.orm.dsl">

</manifest>
30 changes: 26 additions & 4 deletions library/src/main/java/com/orm/SugarRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,29 @@ public static <T> Iterator<T> findAsIterator(Class<T> type, String whereClause,
return new CursorIterator<>(type, cursor);
}

public static <T> List<Long> findIds(Class<T> type, String whereClause, String... whereArgs) {
return findIds(type, whereClause, whereArgs, null, null, null);
}

public static <T> List<Long> findIds(Class<T> type, String whereClause, String[] whereArgs, String groupBy, String orderBy, String limit) {
List<Long> result = new ArrayList<>( );

Cursor cursor = getSugarDataBase().query(NamingHelper.toTableName(type), new String[]{"id"}, whereClause, whereArgs,
groupBy, null, orderBy, limit);
if ( cursor.moveToFirst() )
{
do
{
result.add( cursor.getLong(0) );
}
while (cursor.moveToNext());
}

cursor.close( );

return result;
}

public static <T> List<T> find(Class<T> type, String whereClause, String... whereArgs) {
return find(type, whereClause, whereArgs, null, null, null);
}
Expand Down Expand Up @@ -229,8 +252,7 @@ public static <T> List<T> getEntitiesFromCursor(Cursor cursor, Class<T> type, St
try {
while (cursor.moveToNext()) {
entity = type.getDeclaredConstructor().newInstance();
new EntityInflater()
.withCursor(cursor)
new EntityInflater().withCursor(cursor)
.withObject(entity)
.withEntitiesMap(getSugarContext().getEntitiesMap())
.withRelationFieldName(relationFieldName)
Expand Down Expand Up @@ -282,7 +304,7 @@ public static <T> long count(Class<T> type, String whereClause, String[] whereAr
}

public static <T> long sum(Class<T> type, String field) {
return sum(type, field, null, null);
return sum(type, field, null, (String) null );
}

public static <T> long sum(Class<T> type, String field, String whereClause, String... whereArgs) {
Expand All @@ -296,7 +318,7 @@ public static <T> long sum(Class<T> type, String field, String whereClause, Stri
return result;
}

if (whereArgs != null) {
if (whereClause != null) {
for (int i = whereArgs.length; i != 0; i--) {
sqLiteStatement.bindString(i, whereArgs[i - 1]);
}
Expand Down
23 changes: 18 additions & 5 deletions library/src/main/java/com/orm/helper/MultiDexHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;

Expand Down Expand Up @@ -46,8 +48,8 @@ private static SharedPreferences getMultiDexPreferences() {
* get all the dex path
*
* @return all the dex path, including the ones in the newly added instant-run folder
* @throws PackageManager.NameNotFoundException
* @throws IOException
* @throws PackageManager.NameNotFoundException ...
* @throws IOException ...
*/
public static List<String> getSourcePaths() throws PackageManager.NameNotFoundException, IOException {
ApplicationInfo applicationInfo = getPackageManager().getApplicationInfo(getPackageName(), 0);
Expand Down Expand Up @@ -79,15 +81,23 @@ public static List<String> getSourcePaths() throws PackageManager.NameNotFoundEx
}
}

// handle split files built by instant run
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
String[] splitSourceDirs = applicationInfo.splitSourceDirs;
if ((splitSourceDirs!=null) && (splitSourceDirs.length > 0)) {
sourcePaths.addAll(Arrays.asList(splitSourceDirs));
}
}

return sourcePaths;
}

/**
* get all the classes name in "classes.dex", "classes2.dex", ....
*
* @return all the classes name
* @throws PackageManager.NameNotFoundException
* @throws IOException
* @throws PackageManager.NameNotFoundException ...
* @throws IOException ...
*/
public static List<String> getAllClasses() throws PackageManager.NameNotFoundException, IOException {
List<String> classNames = new ArrayList<>();
Expand All @@ -105,7 +115,10 @@ public static List<String> getAllClasses() throws PackageManager.NameNotFoundExc
classNames.add(dexEntries.nextElement());
}
} catch (IOException e) {
throw new IOException("Error at loading dex file '" + path + "'");
Log.e( "MultiDexHelper", "Error at loading dex file '" + path + "'");
Log.e( "MultiDexHelper", e.getMessage() );

// throw new IOException("Error at loading dex file '" + path + "'");
}
}
return classNames;
Expand Down
8 changes: 8 additions & 0 deletions library/src/main/java/com/orm/util/ReflectionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@
import java.util.Collections;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public final class ReflectionUtil {

Expand Down Expand Up @@ -320,6 +323,11 @@ private static List<String> getAllClasses() throws PackageManager.NameNotFoundEx
for (String classString : allClasses) {
if (classString.startsWith(packageName)) classNames.add(classString);
}

// remove duplicates
Set<String> hs = new LinkedHashSet<>(classNames);
classNames.clear();
classNames.addAll(hs);
} catch (NullPointerException e) {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Enumeration<URL> urls = classLoader.getResources("");
Expand Down
Loading