title: How to sign android app with platform keys using gradle?
body: <p>I saw couple similar questions like:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/4247818/android-after-building-platform-source-how-to-sign-arbitrary-apk-with-platform">Android: After building platform source, how to sign arbitrary APK with platform key?</a></li>
  <li><a href="https://stackoverflow.com/questions/18328730/how-to-create-a-release-signed-apk-file-using-gradle">How to create a release signed apk file using Gradle?</a></li>
  <li><a href="https://stackoverflow.com/questions/20453249/apk-signing-error-failed-to-read-key-from-keystore">APK signing error : Failed to read key from keystore</a></li>
  </ul>
  
  <p>but I feel my problem is different.</p>
  
  <p>First of all I use:</p>
  
  <blockquote>
    <p>android:sharedUserId="android.uid.system"</p>
  </blockquote>
  
  <p>so I need to sign my app with platform key. I'm <strong>able to do that</strong> in this way:</p>
  
  <pre><code>cd $ANDROID_ROOT/out/host/linux-x86/framework
  java -Djava.library.path=$ANDROID_ROOT/out/host/linux-x86/lib64 -jar signapk.jar $ANDROID_ROOT/build/target/product/security/platform.x509.pem $ANDROID_ROOT/build/target/product/security/platform.pk8 $APP_DIR/app/build/outputs/apk/debug/app-debug.apk $APP_DIR/MyApp-signed.apk
  </code></pre>
  
  <p>However I want to do signing from gradle, so I have generated jks file in <a href="https://github.com/getfatday/keytool-importkeypair" rel="nofollow noreferrer">this</a> way:</p>
  
  <pre><code>./keytool-importkeypair -k my_keystore.jks -p my_password -pk8 $ANDROID_ROOT/build/target/product/security/platform.pk8 -cert $ANDROID_ROOT/build/target/product/security/platform.x509.pem -alias platform
  </code></pre>
  
  <p>and I've modified app/build.gradle to have:</p>
  
  <pre><code> signingConfigs {
       release {
           storeFile file("my_keystore.jks")
           storePassword "my_password"
           keyAlias "platform"
           keyPassword "my_password"
       }
   }
  
   buildTypes {
       release {
           minifyEnabled false
           proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
  
           signingConfig signingConfigs.release
       }
   }
  </code></pre>
  
  <p>I've checked that my_keystore.jks has platform alias with:</p>
  
  <pre><code>keytool -list -v -keystore my_keystore.jks | grep Alias
  Alias name: platform
  </code></pre>
  
  <p>but when I try to do:</p>
  
  <pre><code>./gradlew assembleRelease
  </code></pre>
  
  <p>or:</p>
  
  <pre><code>./gradlew signingReport
  </code></pre>
  
  <p>I get:</p>
  
  <blockquote>
    <p>Failed to read key platform from store "(...)/my_keystore.jks": Invalid keystore format</p>
  </blockquote>
  
  <p><strong>Update:</strong>
  I've tried following <strong>dr_g</strong> tips and I'm able to sign app using Android Studio (Build -> Generate Signed APK), so I guess keystore is ok, but still I get the same error when using assembleRelease.
  I've also tried generating my own keystore as suggested by <strong>deadfish</strong> and indeed keystore generated by Android Studio is fine for gradle and assembleRelease works, but it's not platform key, so I can't use it unfortunately.</p>
  
  <p><strong>Issue solved:</strong>
  It turned out that my problem was indeed different than the ones I've mentioned. It was related with keytool used for generating keys (not gradle), and it was because although my default java was 8, my default keytool was from java 10 … When I’ve switched to keytool from java 8 everything started to work fine.</p>