Categories
discuss

What does Android app signing means?

After developing android app, I have to sign it in order to publish it to Play store.

What does singing process actually do?

I thought it related to encryption, but I am able to uncompress the apk file without any need to my app’s signing keys.

Answer

Android requires that all apps be digitally signed with a certificate before they can be installed. Android uses this certificate to identify the author of an app, and the certificate does not need to be signed by a certificate authority. Android apps often use self-signed certificates. The app developer holds the certificate’s private key.

The basics behind protecting your Android app is to use a generated certificate and digital “key” which provides a unique, encrypted, and reasonably un-hackable signature. This proves that the app came from you, not some other suspicious source.

On Android, this is done via a keystore. The keystore is a simple file with a really large block of encrypted data. This file can be stored anywhere on your computer, and this is generally the first problem that developers encounter.
There are two types of keystores that you should be aware of: debug and release.
Keystore files are also protected by a pair of passwords: one for the keystore file itself and another for each keystore/alias pair within the file. While these passwords should ideally be unique, most developers use the same password for both.

You can sign an app in debug or release mode. You sign your app in debug mode during development and in release mode when you are ready to distribute your app. The Android SDK generates a certificate to sign apps in debug mode. To sign apps in release mode, you need to generate your own certificate.

How to sign your apps in Android Studio

Categories
discuss

How to get database configurations using data source object in java

I’m stuck in a issue related to data source objects in java. I have set data source connection parameters in data source object(org.apache.tomcat.jdbc.pool.DataSource). I wants to get those parameters from data source object before i call getConnection method log meaningful debug info inside catch if it catches exception.

Following is the code so far i have tried. I can get all the connection parameters from metadata as follows[ex:- connection.getMetaData().getURL()], but i wants to catch exception and log url,password,username as a log if getConnection() throws exception. Therefore, i need to get those information from data source object before it tries to creates db connection.

try {
        // try to get the lookup name. If error empty string will be returned
        jndiLookupName = connectionProperties.getProperty(RDBMSConstants.PROP_JNDI_LOOKUP_NAME);
        datasource = InitialContext.doLookup(jndiLookupName);

        connection = datasource.getConnection(); // WHEN THIS THROWS EXCEPTION...

        logger.info(connection.getMetaData().getURL()); // these won't work since exception already thrown.
        logger.info(connection.getMetaData().getUserName());
        logger.info(connection.getMetaData().getDriverName());
        logger.info(connection.getMetaData().getDriverVersion());

        isConnected = true; // if no errors
        logger.info("JDBC connection established with jndi config " + jndiLookupName);

    } catch (SQLException e) {

        //...I WANT ALL CONNECTION PARAMETERS (URL,PASSWORD,USERNAME) HERE

        throw new SQLException("Connecting to database failed with jndi lookup", e);
    }

When i remote debug i get data source object as follows..

org.apache.tomcat.jdbc.pool.DataSource@d47feb3{ConnectionPool[defaultAutoCommit=null; defaultReadOnly=null; defaultTransactionIsolation=-1; defaultCatalog=null; driverClassName=com.mysql.jdbc.Driver; maxActive=100; maxIdle=8; minIdle=0; initialSize=0; maxWait=30000; testOnBorrow=false; testOnReturn=false; timeBetweenEvictionRunsMillis=5000; numTestsPerEvictionRun=0; minEvictableIdleTimeMillis=60000; testWhileIdle=false; testOnConnect=false; password=********; url=jdbc:mysql://localhost/wso2_mb_1; username=a; validationQuery=null; validationQueryTimeout=-1; validatorClassName=null; validationInterval=30000; accessToUnderlyingConnectionAllowed=true; removeAbandoned=false; removeAbandonedTimeout=60; logAbandoned=false; connectionProperties=null; initSQL=null; jdbcInterceptors=ConnectionState;StatementFinalizer;org.wso2.carbon.ndatasource.rdbms.ConnectionRollbackOnReturnInterceptor;; jmxEnabled=true; fairQueue=true; useEquals=true; abandonWhenPercentageFull=0; maxAge=0; useLock=false; dataSource=null; dataSourceJNDI=null; suspectTimeout=0; alternateUsernameAllowed=false; commitOnReturn=false; rollbackOnReturn=false; useDisposableConnectionFacade=true; logValidationErrors=false; propagateInterruptState=false; ignoreExceptionOnPreLoad=false; }

I can see all the url, username and password parameters are there but i can not get those. Is there a way to get these values from data source object.

Answer

Cast to the concrete implementation of the DataSource – the tomcat pooled datasource provides access to username, url etc. (see DataSource for details):

if (dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource) {
    org.apache.tomcat.jdbc.pool.DataSource tcDataSource = (org.apache.tomcat.jdbc.pool.DataSource)dataSource;
    logger.info(tcDataSource.getUrl());
    logger.info(tcDataSource.getUsername());
    ...
}
Categories
discuss

Dagger2 dependency – Gradle

I’m trying to add Dagger2 to my project in Android Studio but I can’t find proper dependency to paste in build.gradle. Could you help and send me the proper line?

Answer

Installing Dagger 2 on Android Studio 2

// Application build.gradle
dependencies {
    compile 'com.google.dagger:dagger:2.4'
    annotationProcessor "com.google.dagger:dagger-compiler:2.4"
}

Maven Repositories:

Find the latest versions of the above dependencies in the Maven Repository:


Notes: Android Studio < 2.2

Older versions of Android Studio need android-apt for annotation processing.

// Project build.gradle
buildscript {
    dependencies {
        // Assists in working with annotation processors for Android Studio.
        // No longer needed with Android Studio 2.2+
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
    }
}
apply plugin: 'com.neenbedankt.android-apt'

And

// Application build.gradle
dependencies {
    compile 'com.google.dagger:dagger:2.4'
    apt "com.google.dagger:dagger-compiler:2.4"
}

Notes: Dagger < 2.1

For Dagger < 2.1-SNAPSHOT the javax.annotation is needed for the @Generated annotation used in Dagger generated code (see github.com/google/dagger/issues/95). The annotation is not included in the Android API jar, so you’ll need to use one of these libraries (see differences):

// Application build.gradle
dependencies {
    compile 'javax.annotation:jsr250-api:1.0'
}
Categories
discuss

make sure class name exists, is public, and has an empty constructor for public fragment with class name and empty constructor

I’ve just got a crash report from one of the users with the following error trace :

Unable to instantiate fragment packageName.Wizard$WizardFirstPage: make sure class name exists, is public, and has an empty constructor that is public   

Here are the classes declarations :

public class Wizard extends Other

public abstract class Other extends BaseActivity

public abstract class BaseActivity extends ActionBarActivity

All classes are public, named and doesn’t have user defined constructor.

As for the fragment WizardFirstPage : (defined in Wizard)

public class WizardFirstPage extends Fragment

No user defined constructor either.

What am I missing ?

Answer

public class WizardFirstPage extends Fragment

That is an inner class of packageName.Wizard. That will only work if the class is declared as static, as indicated by Blackbelt:

public static class WizardFirstPage extends Fragment

When the Wizard activity undergoes a configuration change, or is re-created after process termination, Android is going to try to create an instance of Wizard$WizardFirstPage. With your current approach, Android cannot do this, as only instances of Wizard can create instances of Wizard$WizardFirstPage. Changing WizardFirstPage to be static will fix this.

Categories
discuss

Android build tools 1.1.0, unit test folder?

I recently installed the latest tools from google to my android project:

buildscript {
      repositories {
         jcenter()
         mavenCentral()
      }
      dependencies {
         classpath 'com.android.tools.build:gradle:1.1.0'
      }
}

allprojects {
    repositories {
        jcenter()
    }
}

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    defaultConfig {
        applicationId "com.xxx"
        minSdkVersion 10
        targetSdkVersion 21
        versionCode 200
        versionName "2.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    signingConfigs {
        debug {
            ...
        }
        release {
            ...
        }
    }
    buildTypes {
        release {
            ...
        }
        debug {
            ...
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'

    // ---- Tests with robolectric
    testCompile 'com.google.guava:guava:14.0.1'
    testCompile 'junit:junit:4.+'
    testCompile 'org.robolectric:robolectric:2.4'
    testCompile 'org.mockito:mockito-all:2.0.2-beta'

    // ---- Tests with Espresso
    androidTestCompile ('com.android.support.test.espresso:espresso-core:2.0') {
        exclude module: 'hamcrest-core'
    }
    androidTestCompile 'org.hamcrest:hamcrest-core:1.1'
    androidTestCompile 'org.hamcrest:hamcrest-integration:1.1'
    androidTestCompile 'org.hamcrest:hamcrest-library:1.1'
    androidTestCompile ('com.android.support.test:testing-support-lib:0.1') {
        exclude module: 'hamcrest-core'
    }
    androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0'
    androidTestCompile('junit:junit-dep:4.10') {
        exclude module: 'hamcrest-core'
    }
}

Where before that I used to use com.github.jcandksolutions.gradle:android-unit-test:2.1.1 to run my robolectric tests in the jvm. As google says for their new build tools: “New source folders recognized as unit tests: src/test/java, src/testDebug/java, src/testMyFlavor/java etc.” But as you can see below, my test folder isn’t recognised as a source folder. It worked with com.github.jcandksolutions.gradle:android-unit-test:2.1.1, but no more with the new build tools:

enter image description here

What I’m missing here? Thank you

Answer

I found the solution which is to switch between Test Artifacts in the left corner of the IDE. On this screen only “Android Instrumentation Tests” is available because I downgraded my android tools but with tools 1.1.0+ you should see different types of test to get the IDE recognize them as source folders.

enter image description here

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..